当前位置:   article > 正文

Python 连接 Doris pymysql游标cursor.excute(sql) 并且添加事务和自动重连操作_python 连接doris

python 连接doris

 pymysql.cursors 普通连接:

  1. import pymysql.cursors
  2. conn = pymysql.connect(host="127.0.0.1",port=9030, user="root", password="", database="ssb", charset='utf8')
  3. mycursor = conn.cursor()
  4. mycursor.execute("select * from lineorder_rt limit 1")
  5. result = mycursor.fetchall()
  6. for data in result:
  7. print(data)
  8. # 使用完后,要把游标和 连接都要关闭掉
  9. cursor.close()
  10. conn.close()

问题:为了避免出现连接超时中断错误
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError)(2013,'Lost connection to MySQL server during query')

添加事务操作后的连接:

  1. import pymysql.cursors
  2. connect = pymysql.connect(host="127.0.0.1", port=8090, user=self.user_doris_prd, password=self.password_doris_prd, database="ads", charset='utf8')
  3. while True:
  4. try:
  5. with connect.cursor() as cursor: # 获取游标
  6. cursor.execute(sql)
  7. connect.commit()
  8. result = cursor.fetchone()
  9. # 使用完后,要把游标和 连接都要关闭掉
  10. cursor.close()
  11. connect.close()
  12. for data in result:
  13. print(data)
  14. break
  15. except Exception:
  16. connect.ping(True)

补充:cursor 提供了 fetchone()、fetchall() 和 fetchmany() 函数

参考:python中查询数据库用到的 fetchone()、fetchall()和fetchmany() 函数_cur.fetchall()-CSDN博客

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号