import pymysql
class mysql_db():
def __init__(self,host,user,password,port,database):
self.con = pymysql.connect(
host=host,
user=user,
password=password,
port=port, # 默认即为3306
database=database,
charset='utf8',
)
self.cursor = self.con.cursor()
def select_sql(self,sql,type):
#传入查询类型,0查询一条数据,1查询所有数据
self.cursor.execute(sql)
if type==1:
result_all = self.cursor.fetchall()
self.cursor.close()
self.con.close()
return result_all
elif type==0:
result_one = self.cursor.fetchone()
self.cursor.close()
self.con.close()
return result_one
def insert_sql(self,sql):
try:
self.cursor.execute(sql)
self.con.commit()
except Exception as e:
self.con.rollback()
print(e)
finally:
self.cursor.close()
self.con.close()
def update_sql(self,sql):
try:
self.cursor.execute(sql)
self.con.commit()
except Exception as e:
self.con.rollback()
print(e)
finally:
self.cursor.close()
self.con.close()
def delete_sql(self,sql):
try:
self.cursor.execute(sql)
self.con.commit()
except Exception as e:
self.con.rollback()
print(e)
finally:
self.cursor.close()
self.con.close()
if __name__=="__main__":
host = '' # 默认127.0.0.1
user = ''
password = ''
port = 3308 # 默认即为3306
database = ""
con = mysql_db(host,user,password,port,database)
# sql = 'select * from tb_user limit 10;'
sql = "select b.liquidity from tb_cfd_position_ethusdt as a,tb_cfd_delegate_deal_record_ethusdt as b where b.order_id=a.order_id and a.id='333' and b.user_id='10024' and b.delegate_type in (1,2);"
s = con.select_sql(sql,0)[0]
print(s)
数据库操作
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- # MySQL数据库与数据表操作 + 数据库的操作 + 数据库创建 + 数据库删除 + 数据表的操作 + 数...
- 从单个元素获取连接改为单个分区获取连接,如把foreachRDD改为foreachPartition
- 解决方案: 1WindowsXP、Windows 2003用户请在“C:\Windows\Temp”目录添加一个有...
- 此文将以MYSQL数据库做为例子,pymysql库作为驱动进行学习 安装MYSQL数据库与pymysql第三方库 ...