简体   繁体   English

使用Python sqlite3在数据库中创建表的SQL

[英]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: 为此,我在Cursor对象上使用executemany ,如下所示:

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. 由于我发现错误消息的描述性不足,因此无法通过SQL修复此错误。 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. 在其他两个示例中,您还有SQLite不需要的多余的括号/括号。

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. 但是,我不清楚您为什么要针对不同类型的单词使用不同的表,因为这似乎与数据库设计原则背道而驰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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