带有预准备语句的python MySQLDb插入 [英] python MySQLDb insert with prepared Statements

查看:71
本文介绍了带有预准备语句的python MySQLDb插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有字段的表

TABLE_PASTE(
      user_text longtext NOT NULL,
      number integer NOT NULL
 )

我正在尝试使用MySQLDb驱动程序从python的此表TABLE_PASTE中插入一行.

I am trying to insert a row in this table TABLE_PASTE from python using MySQLDb driver.

text="large_text"
value = 1
cursor.execute("Update TABLE_PASTE set user_text = ? where number = ?",(text,value))

我收到此错误

query = query % db.literal(args)
TypeError: not all arguments converted during string formatting

推荐答案

对于MySQLdb,您想使用 parameterized (技术上说,准备好的语句在python中不存在)语句类似于字符串格式. Python 2.x.

With MySQLdb you want to use parameterized (prepared statements technically don't exist in python) statements similar to string formatting in Python 2.x.

cursor.execute("Update TABLE_PASTE set user_text = %s where number = %s",(text,value))

请注意,即使您的valueint,您仍然必须使用%s在查询中设置其格式.

Note that even though your value is an int you still must use %s to format it in your query.

您可以在MySQLdb 此处找到很好的教程.

You can find a good tutorial on MySQLdb here.

这篇关于带有预准备语句的python MySQLDb插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆