Reputation: 174
I'm trying to rename all table names in an SQLite database with sqlite3 in python3.
The code below is a cleaned up version of what I got so far, which produces this error:
"sqlite3.OperationalError: near "?": syntax error"
Any help is greatly appreciated!
dbpath = "C:/...."
db_list = []
db = sqlite3.connect(dbpath)
c = db.cursor()
c.execute("SELECT name FROM sqlite_master WHERE type='table';")
for i in c.fetchall():
db_list.append(str(i))
for i in range(len(db_list)):
if db_list[i][:4] == "east":
prefix = "A1_"
elif db_list[i][:4] == "west":
prefix = "A2_"
db_name_edit = prefix + db_list[i]
c.execute("ALTER TABLE ? RENAME TO ?", (str(db_list[i]), str(db_name_edit)))
db.commit()
db.close()
Upvotes: 0
Views: 554
Reputation: 6520
Placeholders (named or qmark style) are not allowed for table or column names. Perhaps build a query string in the loop using python string tools and send it as argument to execute
. EG:
query_string = "ALTER TABLE " + old_name + " RENAME TO " + new_name
Upvotes: 1