利用mysql插件 pymysql;写insert语句直接插入到数据库
安装:pip install pymysql。
代码:excute_sql方法是执行更新,插入 *** 作。get_datasset方法是查询。
# coding: utf-8import pymysql.cursors
def execute_sql(sql):
conn = pymysql.connect(host='127.0.0.1',port = 3306,user='root',passwd='123456',db ='db',charset="utf8")
try:
with conn.cursor() as cursor:
cursor.execute(sql)
conn.commit()
finally:
conn.close()
def get_dataset(sql):
conn = pymysql.connect(host='127.0.0.1',port = 3306,user='root',passwd='123456',db ='db',charset="utf8")
try:
with conn.cursor() as cursor:
cursor.execute(sql)
return cursor.fetchall()
finally:
conn.close()
去装一个 pymsql包然后
import pymysql #导入pymsql模块 #链接数据库,注意port是int型,不是str,所以不要用引号conn = pymysql.connect(user='root',password='root',host='127.0.0.1',port=3306,database='test_demo',use_unicode=True,charset="utf8")#获取游标cursor = conn.cursor()#插入数据,注意看有变量的时候格式cursor.execute("INSERT INTO tieba_user(`uuid`,`user_name`,`user_rank`,`user_level`,`level_status`,`tieba_name`) VALUES (%s,%s,%s,%s,%s,%s)", (user_id, user_name, user_rank, user_level, level_statu, tieba_name))#提交conn.commit()#关闭连接cursor.close()
类似这样
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)