Home > OS >  SQLITE with python : Adding the database name in SELECT statement
SQLITE with python : Adding the database name in SELECT statement

Time:06-10

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 ?

CodePudding user response:

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.

CodePudding user response:

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')
  • Related