'Why I receive sqlite3.OperationalError: <table name> unrecognized token on a table name that already exists?
I have checked similar errors but this one is different. I stored a table with name "28864540805361867_choosen_sources" and then I wanted to check if table exist and then drop it from database. However here when I first get list of all table names, I see that the table name exists. On the other hand using queries to drop table fails to error "unrecognized token" for table name. Here I include some codes to clarify what is happening.
#checking if table with name "28864540805361867_choosen_sources" exists:
con = sqlite3.connect(database=database_name)
cursor = con.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tablelist=cursor.fetchall()
print("last 3 tables in tuple:",tablelist[-3:])
tbl_name=tablelist[-1][0]
print("tbl_name is:",tbl_name)
Terminal:
last 3 tables in tuple: [('master_mat_reg_28864540805361867',), ('master_mat_reg',), ('28864540805361867_choosen_sources',)]
tbl_name is: 28864540805361867_choosen_sources From here it is clear that my last table is "28864540805361867_choosen_sources" Now let's check with another query:
query=f"""SELECT tableName FROM sqlite_master WHERE type='table' AND tableName={tbl_name}; """
listOfTables=cursor.execute(query).fetchall()
Terminal: Error
listOfTables=cursor.execute(query).fetchall()
sqlite3.OperationalError: unrecognized token: "28864540805361867_choosen_sources"
We see that that table name is unrecognized token. Now lets check another query:
query = """SELECT count(name) FROM sqlite_master WHERE type='table' AND name={table}""".format(table=tbl_name)
Terminal: Error
listOfTables=cursor.execute(query).fetchall()
sqlite3.OperationalError: unrecognized token: "28864540805361867_choosen_sources"
Again we have the same error. Now I try to drop the table:
cursor.execute(f"DROP TABLE {tbl_name};")
Terminal: Error
cursor.execute(f"DROP TABLE {tbl_name};")
sqlite3.OperationalError: unrecognized token: "28864540805361867_choosen_sources"
So, what is wrong here? and How should I drop that table? my python version is 3.9.10
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
