gpt4 book ai didi

python - 为什么这个 sqlite3 insert 语句不添加行?

转载 作者:行者123 更新时间:2023-11-30 23:25:54 25 4
gpt4 key购买 nike

我有一个表,技能,尽管我尝试向其中添加行,但目前该表还是空的。我的 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 操作并不是特别高级或复杂。

关于如何让它在不报告错误的情况下运行但数据库中技能表为空的任何见解?

谢谢

最佳答案

您的插入语句不会自动提交。来自 sqlite3.Connection 上的文档:

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)

关于python - 为什么这个 sqlite3 insert 语句不添加行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22788534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com