简体   繁体   中英

python sqlite3 syntax error when copying a row

I'm having trouble copying a row from one table to another using sqlite3 in python (2.6.1 don't ask). I can specify one column, but if I add a second, it gives me an error.

import sqlite3

conn = sqlite3.connect("database.db")
cursor = conn.cursor()
#this works: cursor.execute("insert into table2 (name) select (name) from table1")
cursor.execute("insert into table2 (name, title) select (name, title) from table1") #this doesn't
conn.commit()
cursor.close()

Results in:

sqlite3.OperationalError: near ",": syntax error

What gives? I know the SQLite syntax is right, but sqlite3 won't take it. Forgive me if this has been asked before, commas tend to get filtered out of results, so it's hard to search for.

You shouldn't have the parentheses after select . It should be:

insert into table2 (name, title) select name, title from table1

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