1

Trying to set up some basic data I/O scripts in python that read and write from a local sqlite db. I'd like to use the command line to verify that my scripts work as expected, but they don't pick up on any of the databases or tables I'm creating.

My first script writes some data from a dict into the table, and the second script reads it and prints it.

Write:

# first part of script creates a dict called 'totals'

import sqlite3 as lite

con = lite.connect('test.db')

with con:
    cur = con.cursor()

    cur.execute("DROP TABLE IF EXISTS testtbl")    

    cur.execute("CREATE TABLE testtbl(Date TEXT PRIMARY KEY, Count INT, AverageServerTime REAL, TotalServerTime REAL, AverageClientTime REAL, TotalClientTime REAL)")
    cur.execute('INSERT INTO testtbl VALUES("2012-09-08", %s, %s, %s, %s, %s)' % (float(totals['count()']), float(totals['serverTime/count()']), float(totals['serverTime']), float(totals['totalLoadTime/count()']), float(totals['totalLoadTime'])))

Read:

import sqlite3 as lite

con = lite.connect('test.db')

with con:    

    cur = con.cursor()    
    cur.execute("SELECT * FROM testtbl")

    rows = cur.fetchall()

    for row in rows:
        print row

These scripts are separate and both work fine. However, if I navigate to the directory in the command line and activate sqlite3, nothing further works. I've tried '.databases', '.tables', '.schema' commands and can't get it to respond to this particular db. I can create dbs within the command line and view them, but not the ones created by my script. How do I link these up?

Running stock Ubuntu 12.04, Python 2.7.3, SQLite 3.7.9. I also installed libsqlite3-dev but that hasn't helped.

6
  • 2
    Are you putting the DB file name in the command ? sqlite3 test.db Commented Sep 10, 2012 at 22:25
  • what do you mean by "activate sqlite3"? Commented Sep 10, 2012 at 22:27
  • you need to do con.commit() after changing anything in python Commented Sep 10, 2012 at 22:28
  • @JoranBeasley launch the sqlite3 env by typing sqlite3 Commented Sep 10, 2012 at 22:29
  • @JoranBeasley using the 'with' construction you don't need to, as is my understanding. FWIW, that didn't work when I tried it. Commented Sep 10, 2012 at 22:30

1 Answer 1

2

Are you putting the DB file name in the command ?

$ sqlite3 test.db

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.