pymysql执行插入操作不成功且无报错
在使用pymysql执行插入操作时,可能会遇到插入失败但没有收到任何错误提示的情况。这通常是由于代码中存在细微问题导致的。
例如,在上述代码中,插入语句中使用的是.作为占位符,而pymysql要求使用%s。此外,cursor.execute()方法中应该传入一个元组作为参数,而不是一个列表。
以下是对代码的修正:
import pymysql def save_db(cons): db = pymysql.connect(host='127.0.0.1', user='root', password='root', port=3306) cursor = db.cursor() sql = 'INSERT INTO quote(jy_time,open_price,high_price,low_price,over_price,zhangdiee,zhangdief,chengjiaol,chengjiaoj,zhenfu,huanshoul) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)' print('插入中...') try: cursor.execute(sql, cons) print(cons) db.commit() except: print('失败了啊') db.rollback() db.close()
以上就是为什么我的pymysql插入操作不成功却没有任何报错?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号