garg10may
garg10may

Reputation: 6179

sqllite3 table not available even after comitting, Operational Error: no such table

I created a test db as below, which works fine in the same python script.

import sqlite3 as db

conn = db.connect('test.db')
cursor = conn.cursor()
cursor.execute("drop table if exists temps")
cursor.execute('create table temps(date text, temp int)')
cursor.execute('insert into temps values("12/1/2011",35)')
cursor.execute('insert into temps values("13/1/2011",45)')
cursor.execute('insert into temps values("14/1/2011",42)')
cursor.execute('insert into temps values("15/1/2011",39)')
cursor.execute('insert into temps values("16/1/2011",41)')
conn.commit()

conn.row_factory = db.Row
cursor.execute('select * from temps')

rows= cursor.fetchall()
for row in rows:
    print ("%s %s" % (row[0], row[1]))

conn.close()

But When I access the same DB over a different script, I am able to open a connection but it shows error when I try to access the temps table.

import sqlite3 as db
conn = db.connect('test.db')
cursor = conn.cursor()
conn.row_factory = db.Row
cursor.execute("select * from temps")
rows=cursor.fetchall()
for row in rows:
    print ("%s %s" % (row[0], row[1]))

Error:

    cursor.execute('select * from temps')
sqlite3.OperationalError: no such table: temps

Upvotes: 0

Views: 798

Answers (1)

Noelkd
Noelkd

Reputation: 7896

You need to supply the correct path to your database, the first script creates a database in the same folder as the one it is ran in.

For ease we will assume your first script is running in C:\DataBase, when it runs you will now see a file test.db in the DataBase folder. This file is your sqlite3 database. To access this from another python script on the same machine you would need to:

import sqlite3 as db
conn = db.connect('C:\DataBase\test.db')
cursor = conn.cursor()
conn.row_factory = db.Row
cursor.execute("select * from temps")
rows=cursor.fetchall()
for row in rows:
    print ("%s %s" % (row[0], row[1]))

Otherwise as you may have noticed the second script will just create a new database file test.db in the folder it was ran from.

Upvotes: 1

Related Questions