为什么SQLite3INSERT语句不添加行?

2024-10-06 12:28:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个表,skills,尽管我试图向其中添加行,但它现在是空的。我在CGI脚本中有以下Python代码:

open('/tmp/skills', 'a').write('Reached 1!\n')
if get_cgi('nous2dianoia'):
    open('/tmp/skills', 'a').write('Reached 2!\n')
    #if (get_cgi('previous') and get_cgi('name') and get_cgi('previous') !=
      #get_cgi('name')):
        #cursor.execute('DELETE FROM skills WHERE name = ?;',
          #(get_cgi('previous'),))
    cursor.execute('''INSERT INTO skills (name, nous2dianoia,
      hereandnow2escapist, nf2nt, social2individual, ithou2iit,
      slow2quick) VALUES (?, ?, ?, ?, ?, ?, ?);''',
      (get_cgi('name'), get_cgi('nous2dianoia'),
      get_cgi('hereandnow2escapist'), get_cgi('nf2nt'),
      get_cgi('social2individual'), get_cgi('ithou2iit'),
      get_cgi('slow2quick'),))
    open('/tmp/skills', 'a').write('Reached 3!\n')

当我加载一个页面时,/tmp/skills有一个新附加的:

Reached 1!
Reached 2!
Reached 3!

然而,这张桌子仍然空着。(脚本的其余部分运行时不会崩溃,并显示如果在未传递任何CGI变量的情况下调用脚本时预期显示的内容。)

我还没有启动一个事务;SQL操作不是特别高级或复杂。你知道吗

关于如何在没有报告错误的情况下运行此程序,但数据库中的skills表为空,有什么见解吗?你知道吗

谢谢你


Tags: andname脚本getifopencursortmp
1条回答
网友
1楼 · 发布于 2024-10-06 12:28:26

insert语句不会自动提交。从^{}上的文档:

commit()

This method commits the current transaction. If you don’t call this method, anything you did since the last call to commit() is not visible from other database connections. If you wonder why you don’t see the data you’ve written to the database, please check you didn’t forget to call this method.

要自动提交,use the connection as a context manager

# connection.commit() is called automatically upon exit of context manager
# unless an exception is encountered, then connection.rollback() is called.
with connection:
    connection.execute(insert_statment)

相关问题 更多 >