Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.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_Insert - Fatal编程技术网

Python MySQL将如何传递不同格式变量的值插入表中

Python MySQL将如何传递不同格式变量的值插入表中,python,mysql,insert,Python,Mysql,Insert,我想从python脚本在数据库的表中插入一行。列fiels值保存在不同格式的变量中:字符串、整数和浮点。我在论坛上搜索,我尝试了不同的选择,但没有人在工作 我尝试了以下选项: cursor.execute('INSERT INTO table(device, number1, number2) VALUES (%s,%d,%f)',(device_var,number1_var, number2_var)) 我还尝试: cursor.execute('INSERT INTO table(de

我想从python脚本在数据库的表中插入一行。列fiels值保存在不同格式的变量中:字符串、整数和浮点。我在论坛上搜索,我尝试了不同的选择,但没有人在工作

我尝试了以下选项:

cursor.execute('INSERT INTO table(device, number1, number2) VALUES (%s,%d,%f)',(device_var,number1_var, number2_var))
我还尝试:

 cursor.execute('INSERT INTO table(device, number1, number2) VALUES ({0},{1},{2})',(device_var,number1_var, number2_var))

错误:操作错误:(1054,“字段列表”中的未知列“设备变量内容”)

我也试过这样做,看看表中是否有问题,但这样做可以:

cursor.execute('INSERT INTO table(device,number1,number2)值(“dev1”,1,2.4)'

谢谢你的时间

已解决:

 cursor.execute('INSERT INTO table(device, number1, number2) VALUES ("{}",{},{})'.format (string_var,number1_var, number2_var))

感谢您的帮助,您的答案为我提供了继续查找的途径。

您可以使用参数绑定。您不需要担心传递的变量的数据类型

cursor.execute('INSERT INTO table(device, number1, number2) VALUES (?, ?, ?)', ("dev1",1,2.4))

检查您是在查询执行后调用commit(),还是可以为连接启用自动提交

connection_obj.commit()
首先,

cursor.execute('INSERT INTO table(device, number1, number2) VALUES (%s,%d,%f)',(device_var,number1_var, number2_var))
似乎是错误的。同样,第二个版本也是错误的。第三个版本使用。格式是正确的

那么为什么查询没有运行?原因如下:

从1.2.0开始,MySQLdb根据DB-API标准(PEP-249)的要求,默认情况下禁用自动提交。如果您使用的是InnoDB表或其他类型的事务表类型,则需要在关闭连接之前执行connection.commit(),否则您的任何更改都不会写入数据库

相反,您也可以使用connection.rollback()放弃自上次提交以来所做的任何更改

在关闭连接之前提交

  • db=mysql.connect(user=“root”,passwd=”“,db=“some_db”,unix_socket=“/opt/lampp/var/mysql/mysql.sock”)
  • cursor=db.cursor()
  • cursor.execute(“插入到我的表中…”)
  • db.commit()
  • 列fiels值保存在不同格式的变量中:字符串、整数和浮点。我在论坛中搜索,尝试了不同的选项,但没有人工作

    你读过db api规范吗?查询中的占位符不是python格式说明,而是普通占位符-db连接器将负责类型转换、转义,甚至一点清理。FWIW很遗憾MySQLdb作者选择了python字符串格式占位符(
    %s
    )因为它会引起很多混乱……如果他们选择像“?”这样的东西,这会更清楚

    无论如何-编写查询的“正确方法”是:

    cursor.execute(
      'INSERT INTO table(device, number1, number2) VALUES (%s,%s,%s)',
       (device_var,number1_var, number2_var)
       )
    

    当然,别忘了在某个时候提交您的事务…

    可能重复的SQL查询不使用字符串格式。如果您不知道原因:再次感谢您提醒我。我知道并且从未使用过它。我现在意识到我不应该在这里给出建议。我从不同的角度写下了这一点查看并完全忽略了这样一个事实,即它不仅是一个字符串格式问题,而且是一个数据库查询。我使用commit with.format,但我遇到的错误是:OperationalError:(1054,“字段列表”中的未知列“3816F3”),其中“3816F3”是devID_var@user3278790您发布的代码无法生成此类错误。如果您发布的代码不是导致您遇到问题的代码,则没有人可以提供帮助。实际上,MySQLdb连接器希望将
    %s
    作为占位符。
    cursor.execute(
      'INSERT INTO table(device, number1, number2) VALUES (%s,%s,%s)',
       (device_var,number1_var, number2_var)
       )