返回信息流代码如下:
import os, sys
import MySQLdb
try:
conn=MySQLdb.connect(host='localhost',user='root',passwd='',db='my_test')
except Exception,e:
print e
sys.exit()
cursor=conn.cursor()
sql='insert into info(name, address) values(%s, %s)'
values=(("zhangsan","haidian"),("lisi","haidian"))
try:
cursor.executemany(sql,values)
except Exception, e:
print e
sql="select * from info"
cursor.execute(sql)
data=cursor.fetchall()
if data:
for x in data:
print x[0],x[1]
cursor.close()
conn.close()
-------------------------------
运行结果:
zhangsan haidian
lisi haidian
我的问题:
这段代码在info表中插入两条记录,然后把它们读出来。但是在查看Mysql时,这个表中却没有这两条记录,这是什么原因?
这是一条镜像帖。来源:北邮人论坛 / database / #7341同步于 2013/2/13
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Database机器人发帖
Python连接数据库Mysql.
IDoCare
2013/2/13镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
http://stackoverflow.com/questions/384228/database-does-not-update-automatically-with-mysql-and-python
Starting with 1.2.0, MySQLdb disables autocommit by default, as required by the DB-API standard (PEP-249). If you are using InnoDB tables or some other type of transactional table type, you'll need to do connection.commit() before closing the connection, or else none of your changes will be written to the database.
Conversely, you can also use connection.rollback() to throw away any changes you've made since the last commit.
Important note: Some SQL statements -- specifically DDL statements like CREATE TABLE -- are non-transactional, so they can't be rolled back, and they cause pending transactions to commit.
----------------------------
简而言之就是没有自动commit~
----------------------------
如果只是用的话,建议使用SqlAlchemy
【 在 leyzby 的大作中提到: 】
: http://stackoverflow.com/questions/384228/database-does-not-update-automatically-with-mysql-and-python
: Starting with 1.2.0, MySQLdb disables autocommit by default, as required by the DB-API standard (PEP-249). If you are using InnoDB tables or some other type of transactional table type, you'll need to do connection.commit() before closing the connection, or else none of your changes will be written to the database.
: Conversely, you can also use connection.rollback() to throw away any changes you've made since the last commit.
: ...................
多谢了,是这个原因。
没事,第一反应就是可能没有commit,然后正好google到了~
建议搜索的时候用Google,百度搜索技术问题不到位。
另外,也可以site:stackoverflow.com yr-problem,stackoverflow很好使。
【 在 IDoCare 的大作中提到: 】
:
: 多谢了,是这个原因。
【 在 leyzby 的大作中提到: 】
: 没事,第一反应就是可能没有commit,然后正好google到了~
: 建议搜索的时候用Google,百度搜索技术问题不到位。
: 另外,也可以site:stackoverflow.com yr-problem,stackoverflow很好使。
: ...................
yr-problem是什么意思?问题描述?