加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python查询mysql

发布时间:2020-12-20 13:28:16 所属栏目:Python 来源:网络整理
导读:使用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_

使用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'u6d52u6c5f',u'u6e19u5dde')
    return res

if __name__ == "__main__":
    data = dictDate()
    install(dblink,data)
    aa=select(dblink)
    print (aa[1])
    print (aa[2])
    dblink.close()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读