python
  • database
  • sqlite3
  • 2012-08-04 18 views 3 likes 
    3

    我有这样的代码在Python:的Python,SQLite的未保存的文件结果

    conn = sqlite3.connect("people.db") 
    cursor = conn.cursor() 
    
    sql = 'create table if not exists people (id integer, name VARCHAR(255))' 
    cursor.execute(sql) 
    conn.commit() 
    
    sql = 'insert into people VALUES (3, "test")' 
    cursor.execute(sql) 
    conn.commit() 
    
    sql = 'insert into people VALUES (5, "test")' 
    cursor.execute(sql) 
    conn.commit() 
    
    print 'Printing all inserted' 
    cursor.execute("select * from people") 
    for row in cursor.fetchall(): 
        print row 
    
    cursor.close() 
    conn.close() 
    

    ,但似乎永远不会保存到数据库中,总是存在于数据库相同的元素,如果它是不节能任何东西。

    在另一边。如果我试图访问通过源码它的数据库文件,我得到这个错误:

    Unable to open database "people.db": file is encrypted or is not a database 
    

    我发现了一些其他的答案,使用conn.commit代替conn.commit()但没有改变的结果。

    有什么想法?

    +0

    你的代码工作完美的我 – 2012-08-04 10:13:42

    +1

    似乎是现在的代码工作...任何想法上的错误无法打开数据库“people.db” :文件是加密的还是不是数据库? – 2012-08-04 10:30:19

    回答

    4

    这似乎为我(“在数据库中”在每次运行时增加)工作好吗:

    import random, sqlite3 
    
    conn = sqlite3.connect("people.db") 
    cursor = conn.cursor() 
    
    sql = 'create table if not exists people (id integer, name VARCHAR(255))' 
    cursor.execute(sql) 
    
    for x in xrange(5): 
        cursor.execute('insert into people VALUES (?, "test")', (random.randint(1, 10000),)) 
    conn.commit() 
    
    cursor.execute("select count(*) from people") 
    print "In database:", cursor.fetchone()[0] 
    
    0

    可以打开数据库与像SQLite的管理员工具?这将证明db-format是可以的。 如果我搜索该错误,解决方案指向sqlite和所使用的db-driver之间的版本问题。也许你可以改变你的版本,或者AKX可以发布工作组合。

    关于,khz

    +0

    嗨hhz,是Ubuntu的服务器,所以我没有用户界面。感谢你的回答! – 2012-08-04 11:28:35

    +0

    用ui将它scp给客户端。可能吗?只要确定.db文件本身是否有效即可。 – khz 2012-08-04 12:25:32

    1

    宾果!人!我有同样的问题。其中很简单的原因之一。 I`am使用Debian Linux的,错误是

    Unable to open database "people.db": file is encrypted or is not a database

    数据库文件在同一目录不是我的Python脚本 连接线被
    conn = sqlite3.connect('./testcases.db')

    我改变了这种

    conn = sqlite3.connect('testcases.db')

    !没有点和斜线。 错误已修复。所有作品

    如果有人认为它是有用的,你`重新欢迎

    +0

    这是非常好的一点。总是检查你的路径,即。你运行脚本作为cron作业等 – 2016-08-25 06:59:34

    相关问题