Python:向MySQL数据库插文件

关于python 插mysql数据库的。
提醒自己注意下conn.commit()#对于对数据库中的结果发生变化的操作,需要用conn.commit()进行提交
忘了写提交的代码,刚才看了半天,我说怎么就插入不进去呢,唉。
#!/usr/bin/python
# -*- coding: cp936 -*-
#!Filename:Add_username.py
# -*- coding: utf-8 -*-
import MySQLdb
try:
    conn =MySQLdb.connect(host ='localhost',user='root',passwd ='123@qwe',db = 'test',port = 3306)
    cur = conn.cursor()
    values=[]
   
    for i in range(20,30):
        values.append((i,'just'+str(i)))
    print values
    cur.executemany('insert into tester (id,user) values (%s,%s)',values)
    conn.commit()
    #对于对数据库中的结果发生变化的操作,需要用conn.commit()进行提交
    cur.close()
    conn.close()
except MySQLdb.Error,e:
    print "MySQL error %s:%s "%(e.args[0],e.args[1])
原文地址:https://www.cnblogs.com/tangdouguard/p/3838611.html