MySQL INSERT数据没有存储在正确的db中,只是临时的? [英] MySQL INSERT data does not get stored in proper db, only temporary?

查看:110
本文介绍了MySQL INSERT数据没有存储在正确的db中,只是临时的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用MySQL或Python时遇到问题,似乎无法找出问题所在. INSERT似乎只在脚本运行的最后,并且没有存储在数据库中.

I'm having trouble with MySQL or Python and can't seem to isolate the problem. INSERTs only seem to last the run of the script and are not stored in the database.

我有这个脚本:

import MySQLdb
db = MySQLdb.connect(host="localhost", user="user", passwd="password", db="example")
dbcursor = db.cursor()

dbcursor.execute("select * from tablename")
temp = dbcursor.fetchall()
print 'before: '+str(temp)

dbcursor.execute('INSERT INTO tablename (data1, data2, data3) VALUES ("1", "a", "b")')

dbcursor.execute("select * from tablename")
temp = dbcursor.fetchall()
print 'after: '+str(temp)

我第一次运行它时,会得到预期的输出:

The first time I run it I get the expected output:

>>> 
before: ()
after: ((1L, 'a', 'b'),)

问题是,如果我再次运行它,则当before应该已经有条目时,它就会空出,并且之后不会中断(数据1是主键).

The problem is that if I run it again, the before comes out empty when it should already have the entry in it and the after doesn't break (data 1 is primary key).

>>> 
before: ()
after: ((1L, 'a', 'b'),)
>>> 
before: ()
after: ((1L, 'a', 'b'),)
>>> 
before: ()
after: ((1L, 'a', 'b'),)

如果我尝试在同一脚本中运行两次插入命令,它将中断(重复输入PRIMARY KEY")

If I try running the insert command twice in the same script it will break ("Duplicate entry for PRIMARY KEY")

你知道这里会发生什么吗?

Any idea what might be happening here?

推荐答案

您没有提交交易.

conn = MySQLdb.connect (host = "localhost",
                        user = "testuser",
                        passwd = "testpass",
                        db = "test")
cursor = conn.cursor()

cursor.execute(...)
conn.commit()

参考

这篇关于MySQL INSERT数据没有存储在正确的db中,只是临时的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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