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
请问这样的情况,如果处理
谢谢
6 个解决方案
#1
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');"
#2
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)
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)
#3
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
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
#4
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'
或者改格式化字串,文档里提到有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'
#5
要插入%, 你应该写成%%, 否则系统会认为你是字符串的格式化
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');"
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');"
#6
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".
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".
#1
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');"
#2
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)
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)
#3
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
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
#4
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'
或者改格式化字串,文档里提到有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'
#5
要插入%, 你应该写成%%, 否则系统会认为你是字符串的格式化
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');"
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');"
#6
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".
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".