python 插入数据库的时候 如果字符串里有 % 提示"TypeError: not enough arguments for format string

xueshi 2012-06-06 02:19:22
请教大家一个问题 python 插入数据库的时候 如果字符串里有 % 可能会提示插入失败 请问如何解决
sql = "INSERT INTO total VALUES ('http://search.sina.com.cn/?from=news&c=news&q=%B0%A2%C0%EF%D4%C6','','2012-06-11 12:34');"
print sql
print "abc"
print db.execute(sql)

错误信息
File "c:\Python26\lib\site-packages\MySQLdb\cursors.py", line 151, in execute
query = query % db.literal(args)
TypeError: not enough arguments for format string


请问这样的情况,如果处理

谢谢
...全文
15722 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
honghongbp 2012-06-07
  • 打赏
  • 举报
回复
conn = ....
cur = conn.cursor()
sql = "INSERT INTO total VALUES (%s, '', '2012-06-11 12:34')"
cur.execute(sql, ['http://search.sina.com.cn/?from=news&c=news&q=%%B0%%A2%%C0%%EF%%D4%%C6'])
尽量不要去自己格式化sql语句,还麻烦,还容易出错


http://mysql-python.sourceforge.net/MySQLdb.html
===================================================
To perform a query, you first need a cursor, and then you can execute queries on it:

c=db.cursor()
max_price=5
c.execute("""SELECT spam, eggs, sausage FROM breakfast
WHERE price < %s""", (max_price,))
In this example, max_price=5 Why, then, use %s in the string? Because MySQLdb will convert it to a SQL literal value, which is the string '5'. When it's finished, the query will actually say, "...WHERE price < 5".
Rlay_2 2012-06-07
  • 打赏
  • 举报
回复
要插入%, 你应该写成%%, 否则系统会认为你是字符串的格式化

sql = "INSERT INTO total VALUES ('http://search.sina.com.cn/?from=news&c=news&q=%%B0%%A2%%C0%%EF%%D4%%C6','','2012-06-11 12:34');"
angel_su 2012-06-06
  • 打赏
  • 举报
回复
args里的空字串''改None试试...

或者改格式化字串,文档里提到有5种:
'qmark' Question mark style,
e.g. '...WHERE name=?'
'numeric' Numeric, positional style,
e.g. '...WHERE name=:1'
'named' Named style,
e.g. '...WHERE name=:name'
'format' ANSI C printf format codes,
e.g. '...WHERE name=%s'
'pyformat' Python extended format codes,
e.g. '...WHERE name=%(name)s'
xueshi 2012-06-06
  • 打赏
  • 举报
回复
To 2楼

Traceback (most recent call last):
File "abc.py", line 25, in <module>
print db.execute(sql, args)
File "c:\Python26\lib\site-packages\tornado-2.2.1-py2.6.egg\tornado\database.p
y", line 130, in execute
return self.execute_lastrowid(query, *parameters)
File "c:\Python26\lib\site-packages\tornado-2.2.1-py2.6.egg\tornado\database.p
y", line 136, in execute_lastrowid
self._execute(cursor, query, parameters)
File "c:\Python26\lib\site-packages\tornado-2.2.1-py2.6.egg\tornado\database.p
y", line 198, in _execute
return cursor.execute(query, parameters)
File "c:\Python26\lib\site-packages\MySQLdb\cursors.py", line 151, in execute
query = query % db.literal(args)
TypeError: not all arguments converted during string formatting
angel_su 2012-06-06
  • 打赏
  • 举报
回复
query = query % db.literal(args)
execute()里把你的sql语句当格式化字串看待,所以一般不要自己费事组合好,这样反而会出问题...

试试:
sql = "INSERT INTO total VALUES(?, ?, ?)"
args = ('http://search.sina.com.cn/?from=news&c=news&q=%B0%A2%C0%EF%D4%C6','','2012-06-11 12:34')
...
print db.execute(sql, args)
bugs2k 2012-06-06
  • 打赏
  • 举报
回复
sql = r"INSERT INTO total VALUES ('http://search.sina.com.cn/?from=news&c=news&q=%B0%A2%C0%EF%D4%C6','','2012-06-11 12:34');"

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧