PyMySQL – 如何在’INSERT’语句中使用通配符?

huangapple go评论60阅读模式
英文:

PyMySQL - How to use wildcards in an 'INSERT' statement?

问题

抱歉,你的要求是只提供翻译,不回答问题。以下是你提供的内容的翻译:

我对SQL、MySQL和PyMySQL都很陌生,所以如果这是一个简单的问题,我很抱歉。

我试图将一个新的记录插入到我的数据库表中。我正在使用以下代码:

resultValues = ['2022-11-14 00:00:00', 1018, '247 Voice', 18, 81, 93, 1, 9, 'Workload was significantly above expectations, with a 100% increase.', 0, 1, 0, 1, 0, None, 'replace', 'replace', '2022-12-01 14:19:45', 1]
sql3 = """INSERT INTO poi_template_text_locale (Time_Interval, QueueID, NameStr, TemplateID, SectionID, ContentSection, PageSection, LineNum, Sentence, Graph1ID, Graph1Placement, Graph2ID, Graph2Placement, TableID, TablePlacement, Graph1Text, Graph2Text, TSTIMESTAMP, LocaleID) VALUES (%s);"""
cursor.execute(sql3 % (tuple(list(map(str, resultValues)))))
connection.commit()

我的目标是将resultValues列表中的所有数据作为一条记录插入到sql3查询中列出的每个列中。我已确保列的顺序与列表数据的顺序相同。我在sql3查询的末尾放置了%s通配符,但有几个问题我不确定。对于整个数据列表,我是否只需要一个通配符,还是每个列表项都需要一个通配符?将数据放在列表还是元组中更好?

此外,列表中有两个'replace'项。这是因为我不知道如何将值转换为NULL。我尝试在Python中将值转换为None,但似乎不起作用,它返回了一个错误。

当前的代码给我带来了错误:

TypeError: not all arguments converted during string formatting

如果我更改代码,使%s占位符的数量与列表项的数量相同,我会收到相同的错误:

TypeError: not all arguments converted during string formatting

如果我删除tuple函数,只留下list(map(str, resultValues))作为cursor.execute()的参数,然后我会收到以下错误:

pymysql.err.ProgrammingError: (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 '[...'2022-11-14 00:00:00', '1018', '247 Voice', '18', '81', '93', '1', '9', 'Worklo' at line 1")

我认为这个错误是因为它不知道如何处理列表中的方括号。

我已经尝试解决这个问题将近3天了,在Stack Overflow、Reddit、Discord和普通网页文章上进行了无数次的研究 - 因此,任何帮助都将不胜感激,谢谢。

英文:

I'm really new to SQL, MySQL, and PyMySQL so I'm sorry if this is an easy question.

I'm trying to insert a new record back into a table inside my database. I'm using the following code:

resultValues = ['2022-11-14 00:00:00', 1018, '247 Voice', 18, 81, 93, 1, 9, 'Workload was significantly above expectations, with a 100% increase.', 0, 1, 0, 1, 0, None, 'replace', 'replace', '2022-12-01 14:19:45', 1]
sql3 = """INSERT INTO poi_template_text_locale (Time_Interval, QueueID, NameStr, TemplateID, SectionID, ContentSection, PageSection, LineNum, Sentence, Graph1ID, Graph1Placement, Graph2ID, Graph2Placement, TableID, TablePlacement, Graph1Text, Graph2Text, TSTIMESTAMP, LocaleID) VALUES (%s);"""
cursor.execute(sql3 % (tuple(list(map(str, resultValues)))))
connection.commit()

My aim is to insert all of the data in the 'resultValues' list into each column listed in the 'sql3' query as one record. I've made sure that the order of columns is in the same order as the list data. I got the %s wildcard at the end of the 'sql3' query but I'm not sure about a few things. For a whole list of data, do I need only one wildcard for the whole list, or a wildcard for each item of the list. Is it better for the data to be in a list of a tuple?

Also there are 2 'replace' items in the list. Those are there because I'm not sure how to cast a value as NULL. I tried casting the value as None in python, but that didn't seem to work as it returned an error.

This current code gives me the error:

TypeError: not all arguments converted during string formatting

If I change the code so there are as many %s placeholders as there are list items, I get the same error:

TypeError: not all arguments converted during string formatting

If I remove the tuple function, leaving just list(map(str, resultValues)) as the argument for cursor.execute(), I then recieve this error:

pymysql.err.ProgrammingError: (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 '['2022-11-14 00:00:00', '1018', '247 Voice', '18', '81', '93', '1', '9', 'Worklo' at line 1")

I believe this error is because it doesn't know what to do with the square brackets from the list.

I've been trying to tackle this problem for almost 3 days, and I've done countless research on stack overflow, reddit, discord, and regular web articles - so any and all help would be much appreciated, Thank you

答案1

得分: 1

我花了一些时间,但我弄清楚了!我将所有的19个“%s”字符自己放入了'sql3'提示中,并将'cursor.execute()'函数更改为简单地'cursor.execute(sql3, (resultValues))',而不是我之前的'cursor.execute(sql3 % (tuple(list(map(str, resultValues)))))'。

对于未来可能需要帮助的人 - 请确保你有与要插入单个记录的项目数量相同的“%s”字符。

英文:

It took some time but I figured it out!

sql3 = """INSERT INTO poi_template_text_locale (Time_Interval, QueueID, NameStr, TemplateID, SectionID, ContentSection, PageSection, LineNum, Sentence, Graph1ID, Graph1Placement, Graph2ID, Graph2Placement, TableID, TablePlacement, Graph1Text, Graph2Text, TSTIMESTAMP, LocaleID) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);"""
cursor.execute(sql3, (resultValues))
connection.commit()

I put all 19 %s characters into the 'sql3' prompt myself and changed the cursor.execute() function to simply just cursor.execute(sql3, (resultValues)) instead of my previous cursor.execute(sql3 % (tuple(list(map(str, resultValues))))).

To future people who may need this for help - make sure you have the same amount of '%s' characters as you do items to insert into a single record.

huangapple
  • 本文由 发表于 2023年6月26日 18:26:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定