python - 我无法让 INSERT INTO tablename 等在 Python 3.8 pyodbc MySQL 中工作

标签 python mysql sql-insert pyodbc python-3.8

我相当确定与数据库的连接已经建立。 这是我的代码...

#
# *************************************************************************************************************************************
# * ADDKEYWORD processing
# *************************************************************************************************************************************
#    
def addkeyword(ktyp, kword, kweight):
    tbkeywordtype = str(ktyp)
    tbkeyword = str(kword)
    tbweighting = float(kweight)
#    
    tbkeywordkey = 1   # temporary work around
#
# Prepare string for inserting new tbkeyword record
#    
    insertString = str
    insertString = "\"INSERT INTO tbkeyword (KeywordKey, KeywordType, Keyword, Weighting, Added, Updated) VALUES (%s,'%s','%s',%s,'%s','%s');\"" % (tbkeywordkey,tbkeywordtype,tbkeyword,tbweighting,formatted_date,formatted_date)
    print(insertString)
#
# Insert new tbkeyword record
#
    mycursor.execute(insertString)
#
# Check status code to go here
#
    mydb.commit

这是结果。请注意,第一行是打印 mycursor.execute() 括号之间的字符串的结果。

“INSERT INTO tbkeyword(KeywordKey、KeywordType、Keyword、Weighting、Added、Updated)VALUES (1,'K','free',0.6,'2019-11-19 09:12:08','2019- 11-19 09:12:08');"

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Tony\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "C:\Users\Tony\AppData\Local\Programs\Python\Python38\MainMenuGUI.py", line 185, in <lambda>
    command=lambda: addkeyword(ektyp.get(),ekword.get(),eweight.get()))
  File "C:\Users\Tony\AppData\Local\Programs\Python\Python38\MainMenuGUI.py", line 103, in addkeyword
    mycursor.execute(insertString)
pyodbc.ProgrammingError: ('42000', '[42000] [MySQL][ODBC 8.0(a) Driver][mysqld-8.0.18]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 \'"INSERT INTO tbkeyword (KeywordKey, KeywordType, Keyword, Weighting, Added, Upda\' at line 1 (1064) (SQLExecDirectW)')

我根本看不出我做错了什么。帮助!!!!

最佳答案

问题是您在查询字符串中插入了“。只需删除它们(在开头和结尾):

   insertString = str
   insertString = "INSERT INTO tbkeyword (KeywordKey, KeywordType, Keyword, Weighting, Added, Updated) VALUES (%s,'%s','%s',%s,'%s','%s');" % (tbkeywordkey,tbkeywordtype,tbkeyword,tbweighting,formatted_date,formatted_date)
   print(insertString)
   #
   # Insert new tbkeyword record
   #
   mycursor.execute(insertString)

但是如果您还使用准备好的语句并删除不需要的行,如下所示:

insertString = "INSERT INTO tbkeyword (KeywordKey, KeywordType, Keyword, Weighting, Added, Updated) 
                VALUES (?,?,?,?,?,?);"     
# print(insertString)
#
# Insert new tbkeyword record
#
mycursor.execute(insertString,(tbkeywordkey,tbkeywordtype,tbkeyword,tbweighting,formatted_date,formatted_date))

关于python - 我无法让 INSERT INTO tablename 等在 Python 3.8 pyodbc MySQL 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58930708/

相关文章:

php - 如何使用 mysqli 和 PHP 使用代表另一行 id 的变量更新两列的不同行?

mysql - 从日期时间获取单个日期或日期范围下的详细信息的 SQL 查询问题

mysql - 在 mysql 中,如果不为空或另一个表列,是否可以使用表的一列对结​​果进行排序

PHP 购物车多个值在一个 id

mysql:从同一个表中选择和插入

python - 在 CSV 中找到的匹配总和

python - 查找元组中列表的数量

php - 在 PHP PDO MYSQL 中插入多行的最佳方法是什么?

python - 带有 cython 的 numpy 数组

python - QFileDialog 中的多个文件和文件夹选择?