简体   繁体   中英

SQL for creating tables in a database using Python sqlite3

I am trying to create a number of tables with different names (of course) but sharing the same schema. For this purpose, I am using executemany on a Cursor object as follows:

tables = ['Meanings', 'Synonyms', 'Antonyms', 'Examples', 'Phrases',
          'Pronunciations', 'Hyphenations']
create_table_query = '''CREATE TABLE (?) (
                                        id      INTEGER NOT NULL,
                                        text    TEXT,
                                        word_id INTEGER NOT NULL,
                                        PRIMARY KEY id,
                                        FOREIGN KEY(word_id) REFERENCES Word(id)
                                    )'''
cursor.executemany(create_table_query, tables)

When I execute this snippet, I get the following error message:

OperationalError: near "(": syntax error

I am having trouble fixing the bug here with my SQL since I find the error message to be not descriptive enough. I have tried the following queries but I am unable to develop the understanding of their success and my query's failure:

create_table_query_1 = '''CREATE TABLE {} (
                                         id INTEGER NOT NULL,
                                         text TEXT,
                                         word_id INTEGER NOT NULL,
                                         PRIMARY KEY id,
                                         FOREIGN KEY(word_id) REFERENCES Word(id)
                        )''' # Syntax error near "id"
create_table_query_2 = '''CREATE TABLE (?) (
                                         id INTEGER PRIMARY KEY,
                                         text TEXT,
                                         word_id INTEGER NOT NULL,
                                         FOREIGN KEY(word_id) REFERENCES Word(id)
                        )''' # Syntax error near "("

create_table_query_1 = '''CREATE TABLE {} (
                                         id INTEGER PRIMARY KEY,
                                         text TEXT,
                                         word_id INTEGER NOT NULL,
                                         FOREIGN KEY(word_id) REFERENCES Word(id)
                        )''' # works with string formatting

Also, what are other efficient(in terms of time) ways to achieve the same?

To put my comment into an answer and expand on it: you cannot parametrize table nor column names. I was unable to find any documentation on this...

In a couple of the other examples you have extra parens/brackets that SQLite doesn't need.

So the solution, as you've found, is to use string substitution for the table names as in your final example.

Here's an example with a loop over all of your tables:

for table in tables:
    cursor.execute('''CREATE TABLE {} (
                          id INTEGER PRIMARY KEY,
                          text TEXT,
                          word_id INTEGER NOT NULL,
                          FOREIGN KEY(word_id) REFERENCES Word(id)
                        )'''.format(table))

I am not completely clear on why you want different tables for the different types of words, though, as this would seem to go against the principles of database design.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM