mysql - Python MySQL 缓存在 Python 端
问题描述
大家,我真的不知道如何解决我的问题,我认为这是关于缓存(内存)错误。我正在测试本地运行 python 到 Google ClouSQL - MYSQL using CloudProxy 的连接
我遇到了一个问题,我的表的自动增量 ID 在本地 python 端出现故障,我对此很陌生。
我所做的是
- 从代码中删除了 connection.commit() 以测试它是否可以在没有 commit() 的情况下插入到 cloudSQL
- 运行python文件几次,数据显示在本地终端,但没有出现在云数据库中
- 将 connection.commit() 放回代码中并运行
- 不知何故,自动增量 ID 从 python 离开的地方开始,而不是从 cloudSQL 继续。
两边的userID都是11,我把connection.commit()去掉,然后点击了几次运行,它在本地显示了插入的数据库输出,然后我检查了cloudSQL没有收到任何插入,所以我把connection.commit()放回去到代码。我在玩 .commit()
当我使用 connection.commit() 再次单击运行时,输出显示用户 ID 在本地和 cloudSQL 中从 22 开始,但应该是 12。
我不知道如何解决这个问题,我尝试重新启动云代理和 python 程序,没有工作。如果我将 commit() 留在那里并且从不删除它,这不是一个大问题,但我只想知道发生了什么。
我截取了终端输出和 CloudSQL 数据库的屏幕截图,这是我的代码。
import mysql.connector
connection = mysql.connector.connect(host='127.0.0.1',
user='root',
password='root',
db='account')
con = connection.cursor()
firstname = "Test123"
lastname = "Test123"
phone = "1234567890"
email = "yi@gmail.com"
address1 = "RMIT Street"
address2 = "Melbourne"
postcode = "1234"
state = "VIC"
username = "test111"
password = "abc123"
#userid will auto increment
statement = "INSERT INTO user (firstname, lastname, phone, email, address1, address2, postcode, state, username, password) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
value = (firstname,lastname,phone,email,address1,address2,postcode,state,username,password)
con.execute(statement,value)
connection.commit()
con.execute("select * from user")
for a,b,c,d,e,f,g,h,i,j,k in con.fetchall():
print (a,b,c,d,e,f,g,h,i,j,k)
解决方案
这不是 Python 的问题,这是意料之中的。看起来你有一些失败的插入,而 InnoDB 实际上是一个事务引擎。
示例 - 会话 A 插入记录 1 会话 B 插入记录 2 会话 C 插入记录 3 会话 D 失败并回滚,现在插入的下一个 id 将是 4,但 3 不会存在..
参考 -
推荐阅读
- migration - 如何在计费系统迁移项目中处理用户故事?
- amazon-dynamodb - 如何正确使用 DynamoDB 中的创建表?
- php - 如何让用户根据他们所在的组订阅推送频道?
- node.js - 如何使用 elasticsearch.js 客户端列出所有索引
- kotlin - 使用 Int 创建 BehaviourSubject
- kubernetes - Kubernetes 中的一个 Traefik Pod 失败并出现错误:“命令 traefik 错误:未找到字段,节点:重定向”
- scala - swagger-akka-http 2.x 在响应中设置对象列表
- c - OpenGL着色器不适用于统一变量
- c# - 如何从 pdf 文件中获取下拉菜单的值?
- docker - docker-compose 无法加载 run.sh