sql 语句使用和转换json数据

1 连接mysql

import pymysql
import concurrent
coon=pymysql.connect(host='localhost',user='root',password='123456',db='worlflow',charset='utf8',port=3306)
cursor=coon.cursor()
sql="insert into worker (worker_img) values('%s') where id=10"%str(encodesstr,'utf-8')
cursor.execute(sql)
coon.commit()
cursor.close()# 尽量关闭影响性能
coon.close() #

2  sql 语句查询 和转换json格式  

import pymysql
# Create your tests here.
def dictfetchall(cursor):
	desc=cursor.description
	# print(desc)
	return [
		dict(zip([col[0] for col in desc],row))
		for row in cursor.fetchall()
	]
def get_sql(sql):
	eng = pymysql.connect(host='localhost',user='root',password='1',db='loonviewdata',charset='utf8')
	cursor = eng.cursor()
	cursor.execute(sql)
	# data = cursor.fetchone()
	# data = cursor.fetchall()
	rsult=dictfetchall(cursor)
	return rsult

  

  

posted @ 2020-11-07 15:25  睁yan-ii  阅读(986)  评论(0编辑  收藏  举报