我有一个名为member
的字典组成的列表,我正在这样的数据库中插入
# Executes query for each dictionary in member.
cursor.executemany("INSERT INTO `Citizens` (`Handle`,`Org`,`Role`, "
"`Rank`,`Visibility`,`Stars`,`Type`) VALUES "
"(%(handle)s,%(sid)s,%(roles)s,%(rank)s,"
"%(visibility)s,%(stars)s,%(type)s)", member)
# Commits changes to the database.
conn.commit()
它工作得很好,但是当我开始遇到重复成员的问题时,我决定添加一个ON DUPLICATE
子句。我们的想法是,如果成员是重复的,我们希望通过连接新数据来更新他的Org
列。所以我将代码更改为
# Executes query for each dictionary in member.
cursor.executemany("INSERT INTO `Citizens` (`Handle`,`Org`,`Role`, "
"`Rank`,`Visibility`,`Stars`,`Type`) VALUES "
"(%(handle)s,%(sid)s,%(roles)s,%(rank)s,"
"%(visibility)s,%(stars)s,%(type)s) ON DUPLICATE"
" KEY UPDATE `Org`=concat(ifnull(`Org`, \"\"), "
"\", \", %(sid)s);", member)
# Commits changes to the database.
conn.commit()
但是这样做会出现以下错误:
There was a problem inserting member(s): (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('real_lethality','000',0,'Master','visible',5,'affiliate') ON DUPLICATE KEY UPD' at line 3")
Would you like to see the failed SQL query?[Y/n]
b'INSERT INTO `Citizens` (`Handle`,`Org`,`Role`, `Rank`,`Visibility`,`Stars`,`Type`) VALUES \n(\'fullmetaljim\',\'000\',\'Founder\',\'Master\',\'visible\',5,\'main\') ON DUPLICATE KEY UPDATE `Org`=concat(ifnull(`Org`, ""), ", ", \'000\'),\n(\'real_lethality\',\'000\',0,\'Master\',\'visible\',5,\'affiliate\') ON DUPLICATE KEY UPDATE `Org`=concat(ifnull(`Org`, ""), ", ", \'000\')\n;'
我是否错误地实施了ON DUPLICATE
?我怎样才能正常工作?
示例member
:
[
{'roles': [],
'rank': 'No SCB account',
'type': 'main',
'stars': 2,
'visibility': 'visible',
'sid': 'imperium',
'handle': 'freakyeagle'
},
{'roles': [],
'rank': 'Fleet Member',
'type': 'main',
'stars': 1,
'visibility': 'visible',
'sid': 'imperium',
'handle': 'cadimus'},
{'roles': [],
'rank': 'Fleet Member',
'type': 'main',
'stars': 1,
'visibility': 'visible',
'sid': 'imperium',
'handle': 'belleal'}
]
编辑:因为看起来这可能是PyMySQL本身的一个错误,任何人都可以确认吗?
答案 0 :(得分:1)
查看您的代码并从错误中查询我同意executemany方法构造的查询不正确。我建议你以更加手动的方式构建插件,循环遍历参数,然后在不使用executemany方法的情况下执行它。
使用单引号时,MySQL AFAIK也是完全可以的,所以如果你用双引号括起来,可以使用单引号作为内部字符串限定符 - 这样你就不需要一直逃脱。
答案 1 :(得分:0)
我认为语法错误:
ON DUPLICATE KEY UPDATE `Org`=concat(ifnull(`Org`, ""), ", ",
您需要两个双引号(或两个单引号)
ON DUPLICATE KEY UPDATE `Org`=concat(ifnull(`Org`, ""), "", "",
请注意,这个问题似乎有两次,因为命令重复(??)
这是您发布的查询:
INSERT INTO `Citizens` (`Handle`,`Org`,`Role`, `Rank`,`Visibility`,`Stars`,`Type`)
VALUES \n(\'fullmetaljim\',\'000\',\'Founder\',\'Master\',\'visible\',5,\'main\')
ON DUPLICATE KEY UPDATE `Org`=concat(ifnull(`Org`, ""), ", ", \'000\'),\n(\'real_lethality\',\'000\',0,\'Master\',\'visible\',5,\'affiliate\')
ON DUPLICATE KEY UPDATE `Org`=concat(ifnull(`Org`, ""), ", ", \'000\')\n;
答案 2 :(得分:0)
我不知道问题是否已经解决,但我最近不得不运行查询
**INSERT INTO .... ON DUPLICATE KEY UPDATE**
// Assuming conn as a connection to a MySQL database and member a dict with the required variables
cursor = conn.cursor()
query = (member['handle'], member['org'], member['role'], member['rank'],
member['visibility'], member['stars'], member['type']) // Tuple object of the required values from the dict
cursor.execute("""
INSERT INTO Citizens
(Handle,Org,Role,Rank,Visibility,Stars,Type)
VALUES(%s,%s,%s,%s,%s,%s,%s)
Handle = VALUES(Handle)
Org= VALUES(Org)
Role= VALUES(Role)
Rank= VALUES(Rank)
Visibility= VALUES(Visibility)
Stars= VALUES(Stars)
Type= VALUES(Type)
""", query)
conn.commit()