0

Newbie to sql and sqlite.

I'm trying to save a database, then copy the file.db to another folder and open it. So far I created the database, copy and pasted the file.db to another folder but when I try to access the database the output says that it is empty.

So far I have

from pysqlite2  import dbapi2 as sqlite

conn = sqlite.connect('db1Thu_04_Aug_2011_14_20_15.db')
c    = conn.cursor()
print c.fetchall()

and the output is

[]
tshepang
  • 12,111
  • 21
  • 91
  • 136
csta
  • 2,423
  • 5
  • 26
  • 34

2 Answers2

2

You need something like

c.execute("SELECT * FROM mytable")
for row in c:
    #process row
arunkumar
  • 32,803
  • 4
  • 32
  • 47
  • I tried the first line and I get, "OperationalError: no such table:" (I made sure to use the original table name) – csta Aug 04 '11 at 21:59
  • 1
    SELECT * FROM sqlite_master where type='table' – sampwing Aug 04 '11 at 22:24
  • Just to add to this comment above. Forget pysqlite for a minute and make sure this database has the tables and data that you expect. Use sqlite from the command line to connect to the database. `sqlite3 db1Thu_04_Aug_2011_14_20_15.db` . Then run the SELECT statement above to see what tables are there. Then do a SELECT * FROM one of the tables to see if the data you expect is there. – arunkumar Aug 05 '11 at 10:59
1

I will echo Mat and point out that is not valid syntax. More than that, you do not include any select request (or other sql command) in your example. If you actually do not have a select statement in your code, and you run fetchall on a newly created cursor, you can expect to get an empty list, which seems to be what you have.

Finally, do make sure that you are opening the file from the right directory. If you tell sqlite to open a nonexistent file, it will happily create a new, empty one for you.

TimothyAWiseman
  • 14,385
  • 12
  • 40
  • 47
  • Thanks. I tried "c.execute("SELECT * FROM dataB")" and get "OperationalError: no such table: dataB", can you suggest something specific? – csta Aug 04 '11 at 22:03
  • Also I opened the file.db and saw that there is a lot of the data I'm trying to access so I don't think I've created another file, unless there is something else I'm not understanding. – csta Aug 04 '11 at 22:07