Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python MySql查询静默失败_Python_Mysql - Fatal编程技术网

Python MySql查询静默失败

Python MySql查询静默失败,python,mysql,Python,Mysql,我有一个MySql数据库(使用stock Docker映像运行),它包含一个定义如下的表: CREATE TABLE `Edits` ( `id` int(11) NOT NULL AUTO_INCREMENT, `context` varchar(255) NOT NULL, `field` varchar(255) NOT NULL, `value` text, `username` varchar(255) DEFAULT NULL, `requested` dat

我有一个MySql数据库(使用stock Docker映像运行),它包含一个定义如下的表:

CREATE TABLE `Edits` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `context` varchar(255) NOT NULL,
  `field` varchar(255) NOT NULL,
  `value` text,
  `username` varchar(255) DEFAULT NULL,
  `requested` datetime DEFAULT NULL,
  PRIMARY KEY (`id`,`context`,`field`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=latin1
import contextlib
import MySQLdb

mysql = MySQLdb.connect(host = 'host', db = 'db')

def _cursor():
  mysql.ping(True)
  return contextlib.closing(mysql.cursor())

def _exec(sql, params = None):
  with _cursor() as c:
    c.execute(sql, params)

def save_edits(id, context, field, value, username):
  return _exec('REPLACE INTO Edits SET id = %(id)s, context = %(context)s, 
    field = %(field)s, value = %(value)s, username = %(username)s, 
    requested = UTC_TIMESTAMP()', {
      'id': id,
      'context': context,
      'field': field,
      'value': value,
      'username': username,
  })
我在WSGI应用程序中连接到它,我的连接代码如下所示:

CREATE TABLE `Edits` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `context` varchar(255) NOT NULL,
  `field` varchar(255) NOT NULL,
  `value` text,
  `username` varchar(255) DEFAULT NULL,
  `requested` datetime DEFAULT NULL,
  PRIMARY KEY (`id`,`context`,`field`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=latin1
import contextlib
import MySQLdb

mysql = MySQLdb.connect(host = 'host', db = 'db')

def _cursor():
  mysql.ping(True)
  return contextlib.closing(mysql.cursor())

def _exec(sql, params = None):
  with _cursor() as c:
    c.execute(sql, params)

def save_edits(id, context, field, value, username):
  return _exec('REPLACE INTO Edits SET id = %(id)s, context = %(context)s, 
    field = %(field)s, value = %(value)s, username = %(username)s, 
    requested = UTC_TIMESTAMP()', {
      'id': id,
      'context': context,
      'field': field,
      'value': value,
      'username': username,
  })
当我调用
save\u edits
函数时,它不会抛出异常,但无法更新数据库。此外,尝试将查询
REPLACE运行到编辑集id=1,context='x',field='y',value='z',username='foo',requested=UTC_TIMESTAMP()通过
mysql
shell之后失败,出现
错误1205(HY000):超过锁等待超时;尝试重新启动事务
错误。但是,如果我等待大约5分钟,错误就会消失,我可以再次对MySql数据库运行
REPLACE-INTO
查询


发生什么事了?如何修复此错误?

事实证明,默认情况下,
autocommit
false
,在不关闭连接的情况下关闭光标将保持事务打开。我将方法更改为:

def _exec(sql, params = None):
  with _cursor() as c:
    c.execute(sql, params)
    mysql.commit()

这就解决了问题。

是否需要显式提交更新?我要尝试的第一件事是运行查询
SET SQL\u SAFE\u update=0有时安全更新会捕获内容,不允许您更新