简体   繁体   中英

Proper syntax in altering a table in sqlite3 Python

I'm trying to recreate a sqlite3 database from a pre-defined structure (structure_1). 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).

I wrote this little python code:

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:

[[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?

There might be an issue with the string that is passed into the SQL query. 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))

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