a question about mysqldb
Evan
xdicry at gmail.com
Thu Aug 14 11:26:45 EDT 2008
a simple problem but I do not know why...:(, could anyone help me?
MySQLdb nominally uses just the %s placeholder style, in my script, i
got error if you want to use placeholder(%s) for table name:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>>> str="select tID,tNote from %s where tID=1" <-------- check here
>>>
>>> e=["tmp"]
>>> s.dbptr.execute(str,e)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line
166, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line
35, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your
SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near ''tmp') where tID=1' at line
1")
>>>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
But sql worked but the I got no query result:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>>> str="select tID,tNote from tmp where %s = %s" <----------check here
>>> e=["tID",int(1)]
>>> s.dbptr.execute(str,e)
0L <------------------ check here
>>>
>>> s.dbptr.fetchall()
()
>>>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
And then, it worked if I do:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>>> str="select tID,tNote from %s where %s = %s" % ("tmp","tID",1)
>>>
>>> str
'select tID,tNote from tmp where tID = 1'
>>> s.dbptr.execute(str)
1L
>>>
>>> s.dbptr.fetchall()
({'tID': 1L, 'tNote': 'kao'},)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
mysql> desc tmp
-> ;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| tID | int(11) | NO | PRI | NULL | auto_increment |
| tDate | date | YES | | NULL | |
| tSID | int(11) | NO | | NULL | |
| tCom | varchar(15) | YES | | NULL | |
| tNote | text | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
mysql>
mysql>
Thanks,
More information about the Python-list
mailing list