[Python] How to create a table in SQLITE to ask me what to call a table

Discussão em 'Python' iniciado por Stack, Janeiro 26, 2025.

  1. Stack

    Stack Membro Participativo

    E.g. After launching the program:

    1. It is in interactive mode We are asked what name we want to create the database with;
    2. After creating the database, the program asks us under what name to create the table in the database;
    3. In the next step, the program asks us how many columns the table should have;
    4. Enter the names of the mentioned number of columns and their types interactively;
    5. Finally, create a database and a table with the columns indicated in it;

    import sqlite3
    connection = sqlite3.connect(input("Enter the name for base: "))
    cursor = connection.cursor()

    table_name = input("Enter the name for table: ")
    columns_name = []
    columns_amount = int(input("Enter amount of coulms and name them: "))
    for item in range(columns_amount):
    item = input("input theme mane of column: ")
    columns_name.append(item)


    cursor.execute("DROP TABLE IF EXISTS "+table_name+"" )
    cursor.execute("CREATE TABLE IF NOT EXISTS "+ table_name +" ("+columns_name[0]+" INTEGER PRIMARY KEY AUTOINCREMENT ,Name TEXT, "+columns_name[1]+" TEXT, "+columns_name[2]+" TEXT, "+columns_name[3]+" TEXT )")

    connection.commit()

    Continue reading...

Compartilhe esta Página