发上等愿,结中等缘,享下等福;择高处立,寻平处住,向宽处行

Brick walls are there for a reason :they let us prove how badly we want things

代码改变世界

Python:向MySQL数据库插文件

2014-07-11 22:06  糖兜guard  阅读(264)  评论(0编辑  收藏  举报
关于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])

It's not who you are underneath, it's what you do that defines you

Brick walls are there for a reason :they let us prove how badly we want things