简体   繁体   English

在sqlite3 Python中更改表的正确语法

[英]Proper syntax in altering a table in sqlite3 Python

I'm trying to recreate a sqlite3 database from a pre-defined structure (structure_1). 我正在尝试从预定义的结构(structure_1)重新创建sqlite3数据库。 I create a db and then literately go through the structure_1 array to recreate every table, and every column I need (using the right types). 我创建了一个数据库,然后从结构上遍历了structure_1数组以重新创建我需要的每个表和每个列(使用正确的类型)。

I wrote this little python code: 我写了这个小python代码:

sqlite_file = 'newdb.db'    # name of the sqlite database file
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()

for table,field,field_type in structure_1:
    print table,field,field_type
    try: # if the table doesn't exist yet
        c.execute('CREATE TABLE {tn} ({nf} {ft});'.format(tn=table, nf=field, ft=field_type))
    except: # if the table exists already
        c.execute('ALTER TABLE {tn} ADD COLUMN {nf} {ft};'.format(tn=table, nf=field, ft=field_type))

With structure_1 looking like this: 使用structure_1看起来像这样:

[[u'countries', u'id', u'INTEGER'], [u'countries', u'iso', u'TEXT'], [u'countries', u'name', u'TEXT'], [u'countries', u'printable_name', u'TEXT'], [u'countries', u'iso3', u'TEXT'], [u'countries', u'numcode', u'NUMERIC'], [u'media_type', u'id', u'INTEGER'], [u'media_type', u'name', u'TEXT'], [u'space_types', u'id', u'INTEGER'], [u'space_types', u'name', u'TEXT'], [u'space_types', u'is_main_space', u'NUMERIC'], [u'users', u'guid', u'TEXT'], [u'users', u'id', u'INTEGER'], [u'users', u'nickname', u'TEXT'], [u'users', u'first_name', u'TEXT'], [u'users', u'last_name', u'TEXT'], [u'users', u'telephone', u'TEXT'], [u'users', u'address_1', u'TEXT'], [u'users', u'address_2', u'TEXT'], [u'users', u'region', u'TEXT'], [u'users', u'country', u'TEXT'], [u'users', u'language', u'TEXT'], [u'users', u'created_at', u'TEXT'], [u'users', u'updated_at', u'TEXT'], [u'attachment_spaces', u'guid', u'TEXT'], [u'attachment_spaces', u'ethics_url', u'TEXT'], [u'attachment_spaces', u'ethics_title', u'TEXT'], [u'attachment_spaces', u'file_url', u'TEXT'], [u'attachment_spaces', u'id', u'INTEGER'], [u'attachment_spaces', u'parent_id', u'TEXT'], [u'attachment_spaces', u'file_title', u'TEXT'], [u'human_spaces', u'guid', u'TEXT'], [u'human_spaces', u'id', u'INTEGER'], [u'human_spaces', u'parent_id', u'TEXT'], [u'human_spaces', u'first_name', u'TEXT'], [u'human_spaces', u'last_name', u'TEXT'], [u'human_spaces', u'date_of_birth', u'TEXT'], [u'human_spaces', u'address_1', u'TEXT'], [u'human_spaces', u'address_2', u'TEXT'], [u'human_spaces', u'telephone', u'TEXT'], [u'human_spaces', u'email', u'TEXT'], [u'human_spaces', u'first_name_birth', u'TEXT'], [u'human_spaces', u'last_name_birth', u'TEXT'], [u'human_spaces', u'other_surname', u'TEXT'], [u'human_spaces', u'gender', u'TEXT'], [u'human_spaces', u'city_of_birth', u'TEXT'], [u'human_spaces', u'country_of_birth', u'TEXT'], [u'human_spaces', u'date_of_death', u'TEXT'], [u'human_spaces', u'place_of_death', u'TEXT'], [u'interview_spaces', u'guid', u'TEXT'], [u'interview_spaces', u'id', u'INTEGER']]

Unfortunately, the code returns an error: 不幸的是,代码返回错误:

    c.execute('ALTER TABLE {tn} ADD COLUMN {nf} {ft};'.format(tn=table, nf=field, ft=field_type))
    sqlite3.OperationalError: near "COLUMN": syntax error

Where could this error come from? 该错误从何而来? Am I missing something obvious in the syntax of a sqlite3 query? 我是否在sqlite3查询的语法中缺少明显的东西?

There might be an issue with the string that is passed into the SQL query. 传递给SQL查询的字符串可能存在问题。 Try using single quotes around them instead, by using this line: 尝试使用单引号将其引起来:

c.execute("ALTER TABLE '{tn}' ADD COLUMN '{nf}' '{ft}';".format(tn=table, nf=field, ft=field_type))

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

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