程序退出时删除 MySQL 行 [英] MySQL rows delete when program exits

查看:30
本文介绍了程序退出时删除 MySQL 行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 和 MySQLdb 向我的数据库添加行.似乎当我的脚本退出时,行会被删除.我在脚本退出之前的最后几行在表上执行select *",它显示了我的一行.当我重新运行脚本时,第一行(打开连接后)执行相同的选择 *"并返回零结果.我真的很茫然.我已经为此工作了大约 2 个小时,但无法理解可能会访问我的数据库的内容.

I'm using Python and MySQLdb to add rows to my database. It seems that when my script exits, the rows get deleted. My last lines before the script exits do a "select *" on the table, which shows my one row. When I re-run the script, the first lines (after opening the connection) do the same "select *" and return zero results. I'm really at a loss here. I've been working for about 2 hours on this, and can't understand what could be accessing my database.

此外,在运行脚本之间,我从终端手动运行select *",结果为零.

Also, between running the scripts, I run the "select *" manually from a terminal with zero results.

如果我从终端手动添加一行,它似乎会持续.

If I manually add a row from the terminal, it seems to last.

插入行的查询:

cursor.execute("INSERT INTO sessions(username, id, ip) VALUES (%s, %s, %s)", (username, SessionID, IP]))

我用来检查数据的查询:

The query I use to check the data:

cursor.execute("select * from sessions")
print cursor.fetchall()

这会显示程序退出前的行,然后再次运行程序时不显示任何内容.

This shows the row before the program exits, then shows nothing when the program is run again.

预先感谢所有帮助.

推荐答案

看起来你需要在执行查询后connection.commit()你的更改(替换connection> 使用您的数据库连接变量).

Looks like you need to connection.commit() your changes after you execute the query (replace connection with your DB connection variable).

http://docs.python.org/library/sqlite3.html

Connection.commit():此方法提交当前事务.如果不调用此方法,则自上次调用 commit() 以来所做的任何事情在其他数据库连接中都是不可见的.如果你想知道为什么你没有看到你写入数据库的数据,请检查你没有忘记调用这个方法.

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.

这篇关于程序退出时删除 MySQL 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆