`
莫激动
  • 浏览: 43736 次
社区版块
存档分类
最新评论

python操作mysql数据库

阅读更多

python操作mysql需要用到MySQLdb这个库。

 

1.下载MySQLdb,点击这里下载(可能被墙请自带梯子)

 

2.使用

 

import MySQLdb
try:
    con = MySQLdb.connect(host='192.168.1.100', user='myuser', passwd='123456', port='3306', db='mydb',charset='utf8')
    c = con.cursor()
    c.execute('select * from user');
    rows=c.fetchall() #一次读取所有数据,返回的数据结构[(?,?,...),(?,?,..),..]
    for row in rows:
        for column in row:
            print column
except Exception,e:
    print e
finally:
    c.close()
    con.close()
 

 

 

import MySQLdb
try:
    con = MySQLdb.connect(host='192.168.1.100', user='myuser', passwd='123456', port='3306', db='mydb',charset='utf8')
    c = con.cursor()
    #使用占位符传递参数,参数是一个tuple
    c.execute('select * from user where user=%s',('root'));
    rows=c.fetchall() #一次读取所有数据,返回的数据结构[(?,?,...),(?,?,..),..]
    for row in rows:
        for column in row:
            print column
except Exception,e:
    print e
finally:
    c.close()
    con.close()
 

 

 

import MySQLdb
try:
    con = MySQLdb.connect(host='192.168.1.100', user='myuser', passwd='123456', port='3306', db='mydb',charset='utf8')
    c = con.cursor()
    #使用占位符传递参数,参数是一个tuple
    c.execute('update user set host=%s where user=%s',('root','192.168.1.200'));
    con.commit() #更新操作记得提交事物,否则更改不会生效
except Exception,e:
    print e
finally:
    c.close()
    con.close()

 

 

 

import MySQLdb
try:
    con = MySQLdb.connect(host='192.168.1.100', user='myuser', passwd='123456', port='3306', db='mydb',charset='utf8')
    c = con.cursor()
    vs=[]
    vs.append(('root','192.168.1.100'))
    vs.append(('db','192.168.1.101'))
    vs.append(('pdb','192.168.1.101'))
    #使用占位符传递参数,参数是一个list tuple
    c.executemany('insert into user(user,host)values(%s,%s)',vs);
    con.commit() #更新操作记得提交事物,否则更改不会生效
except Exception,e:
    print e
finally:
    c.close()
    con.close()
 

3.注意事项

 

咋看一下其中占位符和字符串格式化差不多,天真的以为数字用%d,浮点数用%f,字符串用%s,那你可以就要悲剧了

 

事实上这里的占位符只能是%s

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics