首页 > 数据库技术 > 详细

python查询mysql

时间:2019-07-21 14:55:06      阅读:84      评论:0      收藏:0      [点我收藏+]

使用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` (`id`, `phoneNum`, `location`) VALUES ('1', '18104025555', '辽宁,沈阳');
#!/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()

python查询mysql

原文:https://www.cnblogs.com/outsrkem/p/11221003.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!