表一:
表二:
需求:
從表一中取出cheated_user,為十一位手機號碼,取出前七位,到表二中查詢歸屬地省和市,插入表一。
**:
#coding=utf-8
import mysqldb
conn = mysqldb.connect(
host='localhost',
port=3306,
user='root',
passwd='root',
db='chinaunicom',
charset='utf8',
)cur = conn.cursor()
sql = "select substring(cheated_user,1,7) from t_main_fraud"
cur.execute(sql)
cheated_user = cur.fetchall()
i = 0
for ch in cheated_user:
i = i + 1
sql2 = "select province,city from b_tel_ownership where tel = " + str(ch[0])
cur.execute(sql2)
province_city =cur.fetchall()
if province_city:
sql3 = "update t_main_fraud set cheated_province = '" + province_city[0][0].encode('utf-8') +"'," + "cheated_city ='" + province_city[0][1].encode('utf-8') + "' where id =" + str(i)
print sql3
cur.execute(sql3)
cur.close()
conn.commit()
conn.close()
主要問題:
cur取出的資料是long型,並不是具體資料,所以需要對資料進行處理,通過ide來一步一步斷點除錯。
python連線MySQL資料庫
模組功能 connect 方法 connect 方法用於連線 資料庫,返回乙個資料庫連線物件。如果要連線乙個位於host.remote.com伺服器上名為fourm的mysql資料庫,連線串可以這樣寫 db mysqldb.connect host remote.com user user pass...
python連線mysql資料庫
看自己的機器有沒有python root localhost zn python v 會進入python pythontest。py檔案內容 usr bin python imoprt mysql module import mysqldb connect to the database db my...
python連線mysql資料庫
1 python3.5 連線mysql資料庫需要安裝pymysql外掛程式 參考教程 import pymysql conn pymysql.connect host localhost port 3306,user root passwd rusky db mysql charset utf8 c...