使用pymysql pip install pymysqlhtml
建立mysql測試表python
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;
###初始化測試數據mysql
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()
####調用API接口,查詢手機號碼歸屬地(1)https://www.cnblogs.com/outsrkem/p/11219743.html ####調用API接口,查詢手機號碼歸屬地(2)https://www.cnblogs.com/outsrkem/p/11221003.html ####調用API接口,查詢手機號碼歸屬地(3)https://www.cnblogs.com/outsrkem/p/11221604.html ####調用phone庫,查詢手機號碼歸屬地(4)https://www.cnblogs.com/outsrkem/p/11236509.htmlsql