Mike
Mike

Reputation: 1

SQLITE with python : Adding the database name in SELECT statement

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 ?

Upvotes: 0

Views: 134

Answers (2)

Esad Akçam
Esad Akçam

Reputation: 36

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

Upvotes: 0

Code-Apprentice
Code-Apprentice

Reputation: 83607

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.

Upvotes: 1

Related Questions