使用pymysql
pip install pymysql
创建mysql测试表
CREATE TABLE `userinfo` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`phoneNum` char(11) NOT NULL,
`location` char(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`phoneNum`)
) ENGINE=InnoDB AUTO_INCREMENT=1124 DEFAULT CHARSET=utf8;
初始化测试数据
INSERT INTO `test`.`userinfo` (`phoneNum`) VALUES ('18104025555');
INSERT INTO `test`.`userinfo` (`phoneNum`) VALUES ('18104021213');
INSERT INTO `test`.`userinfo` (`phoneNum`) VALUES ('15012317149');
INSERT INTO `test`.`userinfo` (`phoneNum`) VALUES ('15094332241');
INSERT INTO `test`.`userinfo` (`phoneNum`) VALUES ('15097523141');
INSERT INTO `test`.`userinfo` (`phoneNum`) VALUES ('18950212235');
INSERT INTO `test`.`userinfo` (`phoneNum`) VALUES ('16529221357');
#!/usr/bin/python
#-*-coding:utf-8 -*-
import pymysql
dblink = pymysql.connect(
host="10.10.10.31",
user="abc",password="123456",
database="test",
charset="utf8")
def select(db):
cursor = db.cursor()
cursor.execute("select * from test.userinfo")
# 使用 fetchone() 方法获取单条数据.
data = cursor.fetchone()
#print data
return data
def install(db, data):
cursor = db.cursor()
sql = "update `test`.`userinfo` set location=%s where phoneNum = %s"
data = (data[1], data[0])
cursor.execute(sql, data)
db.commit()
def dictDate():
res = ('18104025555', u'\u6d52\u6c5f', u'\u6e19\u5dde')
return res
if __name__ == "__main__":
data = dictDate()
install(dblink, data)
aa=select(dblink)
print (aa[1])
print (aa[2])
dblink.close()