1

尝试在 python 中设置一些从本地 sqlite 数据库读取和写入的基本数据 I/O 脚本。我想使用命令行来验证我的脚本是否按预期工作,但它们不会处理我正在创建的任何数据库或表。

我的第一个脚本将 dict 中的一些数据写入表中,第二个脚本读取并打印它。

写:

# 脚本的第一部分创建一个名为 'totals' 的字典

将 sqlite3 导入为 lite

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

与骗局:
    cur = con.cursor()

    cur.execute("如果存在 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(总计['serverTime/count()'])、float(totals['serverTime'])、float(totals['totalLoadTime/count()'])、float(totals['totalLoadTime'])))

读:

将 sqlite3 导入为 lite

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

与骗局:    

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

    行 = cur.fetchall()

    对于行中的行:
        打印行

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.

4

1 回答 1

2

Are you putting the DB file name in the command ?

$ sqlite3 test.db

于 2012-09-10T22:37:04.593 回答