Python:向MySQL数据库插文件

烂漫一生 提交于 2020-03-24 11:06:48
关于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])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!