mysql插入语句有误,如何在MySQL Insert语句中使用python变量,我得到一个元组错误,请提示(How to use python variables in MySQL Insert s...

如何在MySQL Insert语句中使用python变量,我得到一个元组错误,请提示(How to use python variables in MySQL Insert statement, i am getting a tuple error, Please suggest)

import MySQLdb

name="XYZ"

number=(256, 34576312114897154715004917944343995880721156274004613128261928143013598386679L)

db=MySQLdb.Connect("localhost","root","12345","phone")

cursor=db.cursor()

sql= ("""INSERT INTO phonebook(number, Mobile) VALUES(%s,%s)""" , name,number)

cursor.execute(sql)

db.commit()

db.close()

“处理格式参数失败;%s”%错误)mysql.connector.errors.ProgrammingError:处理格式参数失败; Python'tuple'无法转换为MySQL类型

import MySQLdb

name="XYZ"

number=(256, 34576312114897154715004917944343995880721156274004613128261928143013598386679L)

db=MySQLdb.Connect("localhost","root","12345","phone")

cursor=db.cursor()

sql= ("""INSERT INTO phonebook(number, Mobile) VALUES(%s,%s)""" , name,number)

cursor.execute(sql)

db.commit()

db.close()

"Failed processing format-parameters; %s" % err) mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python 'tuple' cannot be converted to a MySQL type

原文:https://stackoverflow.com/questions/30522902

更新时间:2020-01-01 12:28

最满意答案

你的sql变量是一个元组

sql= ("""INSERT INTO phonebook(number, Mobile) VALUES(%s,%s)""" , name,number)

所以当它尝试执行时, cursor.execute(sql) (其中sql是元组)它给出错误Python'tuple'无法转换为MySQL类型

请使用字符串。

sql = """INSERT INTO phonebook(number, Mobile) VALUES(%s,%s)""" % (name,number)

Your sql variable is a tuple

sql= ("""INSERT INTO phonebook(number, Mobile) VALUES(%s,%s)""" , name,number)

So when it try to execute, cursor.execute(sql) (where sql is tuple) it give error Python 'tuple' cannot be converted to a MySQL type

Please use string.

sql = """INSERT INTO phonebook(number, Mobile) VALUES(%s,%s)""" % (name,number)

2018-07-07

相关问答

2件事。 我会从你的循环中取出if块并确保预先安装正确的SQL。 SQL3 = 'UPDATE table SET ' + " ".join(query) + ';' #don't forget WHERE clause as well...

或者如果您要插入...有效的INSERT语句 你在文件中遇到空行吗? 也许试试.... 而不是这个(在您的问题中没有正确格式化) for line in InFile:

if LineNumber > 0:

line = line.st

...

你这样做: add_word = ("INSERT INTO {table} "

"(name, surname) "

"VALUES (%s, %s)")

atable = 'second'

data_word = (name1, surname1)

cursor.execute(add_word.format(table=atable), data_word)

You do it like this: add_word = ("INSERT I

...

首先,只需正确输入INSERT 。 像这样使用_GET确实可以打开SQL INJECTIONS ... 看看MySQL准备的语句 。 命名您要插入数据的列也是一种很好的做法 。 这允许你在后面插入额外的列并保留应用程序逻辑。 INSERT INTO cos(rowName1, rowName2) VALUES(?, ?)

哪里? 将准备好陈述。 First and foremost, just type INSERT correctly. Using _GET like that really

...

我认为你应该使用%而不是? : add_contact = "INSERT INTO contacts (id, name, industry, phone, fax, url, pobox, emirate,ranking) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"

? 在sqlite3用作占位符。 在psycopg2和MySQLdb它是% 。 I think you should use % instead of ?: add_contact

...

你的sql变量是一个元组 sql= ("""INSERT INTO phonebook(number, Mobile) VALUES(%s,%s)""" , name,number)

所以当它尝试执行时, cursor.execute(sql) (其中sql是元组)它给出错误Python'tuple'无法转换为MySQL类型 请使用字符串。 sql = """INSERT INTO phonebook(number, Mobile) VALUES(%s,%s)""" % (name,number

...

您正在插入字符串 'NULL' ,而不是NULL值。 如果这些值来自Python结构,则需要使用其他内容映射到SQL中的NULL值。 你可以使用None ,只引用其他值: def sqlquote(value):

"""Naive SQL quoting

All values except NULL are returned as SQL strings in single quotes,

with any embedded quotes doubled.

""

...

好的Spike7这正是理想的, mycursor.execute("SELECT name FROM workers WHERE symbol=%s", (name,))

要么 mycursor.execute("SELECT name FROM workers WHERE symbol=?", (name,))

你给出的第一个链接上接受的答案解释了evertything。 Okay Spike7 this is exactly what is ideal, mycursor.execu

...

首先,将变量直接放在语句中是一个禁忌,导致一个非常好的问题,称为SQL注入 。 为避免这种情况,我们将更改您的命令以使用参数 ,稍后将使用这些参数填充这些参数 command.CommandText = "INSERT INTO userscores (Id, momentsandenergytestscore) VALUES (@Id,@score);";

然后,我们将添加这两行以使用您的变量填充参数 command.Parameters.AddWithValue("@Id", Id);

co

...

是的,你应该将DATE_FORMAT()所有% char加倍,以免将它们解释为预处理语句占位符。 但是你在错误的地方使用DATE_FORMAT()函数,你应该在VALUES(...)声明中使用它,所以要解决所有问题,请尝试: sql = """INSERT INTO tbl_celebrants(id, name, birthday, address)

VALUES(%s , %s, DATE_FORMAT(%s,'%%m/%%d/%%Y'), %s)"""

It's right, you sh

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值