python3 - 常用的操作数据库
# 连接数据库 import pymysql con = pymysql.connect(host='114***144', port=3306, user='root', passwd='z***8', db='c***f', charset='utf8') cursor = con.cursor() # 插入数据 sql = "INSERT INTO stock_price (ne, spot) VALUES ('%s','%s')" % ( ne, spot) cursor.execute(sql) con.commit()
# 获取手机号数据表的中的数据
sql2 = 'SELECT shoujihao FROM shoujihao' self.cursor.execute(sql2) sjh_dates = self.cursor.fetchall() self.con.commit() sjh_lists = [] for i in sjh_dates: sjh_date = str(i).replace("',)", "").replace("('", "") sjh_lists.append(sjh_date)
# 存在的数据忽略,不存在的插入 sql4 = "INSERT ignore INTO wzsjh (userId, userAccount, userName, telephone, ip) VALUES ('%s','%s','%s','%s','%s')" % (userId, userAccount, userName, telephone, ip) self.cursor.execute(sql4) self.con.commit()