Python MySql插入无法正常工作 [英] Python MySql Insert not working

查看:77
本文介绍了Python MySql插入无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python MySQL API从python程序连接到Mysql数据库.从几天开始我就面临一个问题.我无法将记录插入数据库,也不知道是什么原因.这是我连接数据库并将记录插入数据库的方法.

I am using python MySQL API to connect to Mysql database from python program. I am facing a problem from few days. I am unable to insert records into the database and dont know whats the reason. Here is the way i connect and insert records into the database.

db = MySQLdb.connect("localhost","root","padmaramulu","pdfsearch" )
cursor = db.cursor()
#cursor.execute("""CREATE TABLE IF NOT EXISTS documents (docid INT NOT NULL ,PRIMARY KEY(docid),docname CHAR(30)) engine=innodb""")
temp = "hello";number = 2;
cursor.execute( 'insert into documents(docid,docname) values("%d","%s")' % (number,temp) )
db.close()

为什么会这样?

推荐答案

在关闭连接之前,您应添加db.commit().

Before closing the connection, you should add db.commit().

这篇关于Python MySql插入无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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