I am using sqlite3 with python, and after connecting to the database and creating a table, sqlite3 shows an error when I try to execute a SELECT statment on the table with the name of the databse in it :
con = sqlite3.connect("my_databse")
cur = con.cursor()
cur.execute('''CREATE TABLE my_table ... ''')
cur.execute("SELECT * FROM my_database.my_table") # this works fine without the name of the database before the table name
but I get this error from sqlite3 : no such table : my_database.my_table
Is there a way to do a SELECT statment with the name of the database in it ?
2 Answers 2
The short answer is no you can't do this with SQLite. This is because you already specify the database name with sqlite3.connect() and SQLite3 doesn't allow multiple databases in the same file.
Comments
Make sure of the database is in the same directory with the python script. In order to verify this you can use os library and os.listdir() method. After connecting the database and creating the cursor, you can query with the table name.
cur.execute('SELECT * FROM my_table')
"my_databse". Also, since you've already connected to that database, why are you trying to use the database name in the select statement?sqlitefile contains only one database. There is no point in prefixing it to the table name. If you are doing this because you plan to migrate your code to a multi-user database at some point, it might be better to do your development on a database that is more like the intended target. Naming the tables will be the least of your problems migrating fromsqlite.