【问题标题】:Unable to Insert into MySQL via Python and pymysql无法通过 Python 和 pymysql 插入 MySQL
【发布时间】:2013-04-18 12:30:02
【问题描述】:

我有一个有 9 列的表,前三列都是文本,后六列设置为 INT。

当我插入文本字段时,一切都按预期工作

conn = pymysql.connect(host='', port=, user='', passwd='', db='')
cur = conn.cursor()
cur.execute("INSERT INTO table(Name) VALUES ('Sample Name')")

该代码运行良好,并允许我将名称插入到我的数据库中。但是,在为我的整数内容运行插入语句时,我遇到了各种麻烦。

我已经尝试了以下所有方法:

cur.execute("INSERT INTO table(Left Avg) VALUES (5)")
cur.execute("INSERT INTO table(Left Avg) VALUES (%s)", (5))
cur.execute("INSERT INTO table(Left Avg) VALUES (%s)" % (5))
cur.execute("""INSERT INTO mlb_data(Left Avg) VALUES (%s)""" % (5))

基本上无论我在将整数插入表时尝试什么,都会收到以下错误。

Traceback (most recent call last):
File "testing.py", line 12, in <module>
cur.execute("""INSERT INTO mlb_data(Left Avg) VALUES (%s)""" % (5))
File "build/bdist.macosx-10.7-intel/egg/pymysql/cursors.py", line 117, in execute
File "build/bdist.macosx-10.7-intel/egg/pymysql/connections.py", line 189, in     defaulterrorhandler
pymysql.err.ProgrammingError: (1064, u"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 'Left   Avg) VALUES (5)' at line 1")

我尝试将 MySQl 平板电脑更改为 VarChar 而不是 INT,但无论何时我尝试插入数字都会出错。

我在这里错过了什么?

编辑: 我不知道我之前是否没有尝试过,或者我的格式不正确,但我想通了。

num = 5.23947824987
cur.execute("INSERT INTO mlb_data(LeftAvg) VALUES (%s)", (num,))

工作顺利。这是正确的,还是我很幸运?

【问题讨论】:

    标签: python mysql pymysql


    【解决方案1】:

    怎么样

    cur.execute("INSERT INTO table(`Left Avg`) VALUES (?)", (5,))
    

    假设 'Left Avg' 是列名,您应该尽量避免列名中的空格作为一般规则

    【讨论】:

    • 好吧,所以我尝试了(感谢您的回答),并将列名更改为 LeftAvg(删除了空格),但现在我收到以下错误。 File "testing.py", line 11, in &lt;module&gt; cur.execute("INSERT INTO mlb_data('LeftAvg') VALUES (?)", (5,)) File "build/bdist.macosx-10.7-intel/egg/pymysql/cursors.py", line 105, in execute TypeError: not all arguments converted during string formatting
    • 尝试删除列名周围的'。在 MySQL 中,转义字符是 `
    • 为什么是 (5,) ?不应该只是 (5) 吗?
    • 只是为了清楚起见......这使得它更清楚它是一个正在传递的元组
    【解决方案2】:

    回答你的最后一个问题:你很幸运,和/或你想出了或记得 Pythonic 的 SQL 查询方式。你可以在site-packages/pymysql/tests/test_basic.py看到类似的例子,比如:

                v = (True, -3, 123456789012, 5.7, "hello'\" world", u"Espa\xc3\xb1ol
    ", "binary\x00data".encode(conn.charset), datetime.date(1988,2,2), datetime.date
    time(2014, 5, 15, 7, 45, 57), datetime.timedelta(5,6), datetime.time(16,32), tim
    e.localtime())
                c.execute("insert into test_datatypes (b,i,l,f,s,u,bb,d,dt,td,t,st) 
    values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", v)
    

    【讨论】:

      【解决方案3】:

      在 Python 中 INSERT 的简单方法是使用连接字符串。像这样的:

      id_user = 1
      username = "Tudor"
      address = "Cluj-Napoca"
      sql = "INSERT INTO user(id_user,username,address) VALUES (" + id_user + ",'" + username + "','" + address + "');"
      cursor.execute(sql)
      

      但要注意 " 和 ' 符号。我强烈建议在执行查询之前打印。

      【讨论】:

      • 这是一个不好的做法,因为它会让你看到SQL Injections
      猜你喜欢
      • 2018-10-14
      • 1970-01-01
      • 2014-08-13
      • 2020-09-30
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      • 2015-08-30
      • 1970-01-01
      相关资源
      最近更新 更多