【问题标题】:Changes made using pymysql don't "persist" to database使用 pymysql 所做的更改不会“持续”到数据库
【发布时间】:2014-11-19 16:47:54
【问题描述】:

我正在尝试使用 pymysql 插入 mysql 数据库。在插入和调用提交之后,作为演示,我选择并打印所有行。新行被打印,但是 - 仅显示(打印)最后添加的行,并且 auto_incremented Id 字段递增,但无论我运行代码多少次,都只显示一行。在 mysql 命令行选择或 Sequel Pro“内容”选项卡中看不到任何行。我相信我已经查看了所有相关的“类似问题”

谢谢。

import pymysql

connCost = pymysql.connect(host='127.0.0.1', port=3306, user='*****', passwd='********', db='CU_COST')
testCur = connCost.cursor()
vF1 = 321
vF2 = '/zero#'
# Transaction implictily started?
query = "Insert Into testInsert (F1, F2) Values(" + str(vF1) + ", '" + vF2 +"')"
testCur.execute(query)
connCost.commit
testCur.execute("Select * From testInsert")
rows = testCur
for row in rows:
   print row

testCur.close
connCost.close

打印结果(代码运行9次后):

2.7.5 (v2.7.5:ab05e7dd2788, May 13 2013, 13:18:45) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
Python Type "help", "copyright", "credits" or "license" for more information.
[evaluate testInsert2.py]
(9, 321, '/zero#')

【问题讨论】:

  • 我无意中删除了这行:"vF1 = 321" oops...
  • 我添加了一个明确的“启动事务”和“提交”(使用 .execute("Start Transaction"))并且更改似乎持续存在。我从研究中假设 MySql 正在隐式启动事务,并且使用 .commit 会完成相同的操作。这似乎对我不起作用。如果我错过了一些我很想听的东西。谢谢

标签: mysql pymysql


【解决方案1】:

提交是一个函数。因此,您必须这样称呼它:

connCost.commit()

这应该可以解决它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-22
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    相关资源
    最近更新 更多