【python】在python中调用mysql

资料:http://www.runoob.com/python/python-mysql.html

 

例子:

假设有数据路HTMLHASH, 其中有表htmlhash, 表中包括两个varchar(10)类型数据。下面给出插入操作代码:

db = MySQLdb.connect("localhost","root","123","HTMLHASH")
cursor = db.cursor()

str1 = "123"
str2 = "456"

cursor.execute("insert into htmlhash values('%s', '%s')" % (str1, str2))

db.commit()

 

注意:

execute(): 执行单条语句,其中如果用到变量,则%s一定要用引号括起来!

如果修改了表,必须使用commit()提交,否则数据库不会变化的。

posted @ 2016-07-21 14:35  匡子语  阅读(755)  评论(0编辑  收藏  举报