簡體   English   中英

在sqlite3 Python中更改表的正確語法

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

我正在嘗試從預定義的結構(structure_1)重新創建sqlite3數據庫。 我創建了一個數據庫,然后從結構上遍歷了structure_1數組以重新創建我需要的每個表和每個列(使用正確的類型)。

我寫了這個小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))

使用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']]

不幸的是,代碼返回錯誤:

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

該錯誤從何而來? 我是否在sqlite3查詢的語法中缺少明顯的東西?

傳遞給SQL查詢的字符串可能存在問題。 嘗試使用單引號將其引起來:

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