473,654 Members | 3,089 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a question about mysqldb

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.execu te(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.errorhandl er(self, exc, value)
File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py" , line
35, in defaulterrorhan dler
raise errorclass, errorvalue
_mysql_exceptio ns.ProgrammingE rror: (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.execu te(str,e)
0L <------------------ check here
>>>
s.dbptr.fetch all()
()
>>>
+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++++
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.execu te(str)
1L
>>>
s.dbptr.fetch all()
({'tID': 1L, 'tNote': 'kao'},)
+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++++

+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++++
mysqldesc 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,
Aug 14 '08 #1
3 1136
Evan wrote:
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:
Placeholders are supposed to be used for *values*, not other parts of
the SQL statement. To insert table names, column names and stuff like
that, use Python-level formatting.

try doing:

table = "tmp"
sql = "select tID,tNote from " + table + " where tID=%s"
param = [1]
s.dbptr.execute (sql, param)
But sql worked but the I got no query result:
+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++++
>>str="select tID,tNote from tmp where %s = %s"
e=["tID",int(1 )]
the string value "tID" doesn't match an integer with the value 1, so
that's expected.

</F>

Aug 14 '08 #2
Evan a écrit :
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:
db-api placeholders won't work for table names - or for anything that
isn't supposed to be a value FWIW. String args are quoted, so you end up
with you sql looking like:

select tID, tNote from 'tmp' where tID=1

instead of

select tID, tNote from tmp where tID=1
You may want to try this instead:

tablename = "tmp"
sql = "select tID, tNote from %s where tID=%%s" % tablename
args = (1,)

s.dbptr.execute (sql, args)
+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ +
>>>str="selec t tID,tNote from %s where tID=1" <-------- check here

e=["tmp"]
>>>s.dbptr.exec ute(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.errorhandl er(self, exc, value)
File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py" , line
35, in defaulterrorhan dler
raise errorclass, errorvalue
_mysql_exceptio ns.ProgrammingE rror: (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="selec t tID,tNote from tmp where %s = %s" <----------check here
e=["tID",int(1 )]
<ot>
- 1 is an int already, so make this e = ["tID", 1]
- str is a very bad choice for an identifier. It's not only
uninformative, but it will also shadow the builtin str type
</ot>
>>>s.dbptr.exec ute(str,e)
0L <------------------ check here
>>>s.dbptr.fetc hall()
()
+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++++
Same problem. Here you end up with something like:

select tID, tNote from tmp where 'tID'=1

You want:

field = "tID"
sql = "select tID,tNote from tmp where %%s = %s" % field
args = (1,)
>
And then, it worked if I do:
+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++++
>>>str="selec t tID,tNote from %s where %s = %s" % ("tmp","tID" ,1)

str
'select tID,tNote from tmp where tID = 1'
>>>s.dbptr.exec ute(str)
1L
>>>s.dbptr.fetc hall()
({'tID': 1L, 'tNote': 'kao'},)
+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++++
Since your not using the db-api quoting mechanism, this of course works
as you expect. *But* this is a potential security hole (perfect
candidate for an sql-injection attack). Use the db-api quoting mechanism
for args, use string formatting for anything else.
Aug 14 '08 #3
I also like to use escaped identifiers in cases like this:

sql = "select tID,tNote from %s where %s = %%s" % ("tmp","tID" )
cursor.execute( sql,1)

should work fine.
Aug 14 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5080
by: Chris Stromberger | last post by:
When issuing updates in mysql (in the console window), mysql will tell you if any rows matched and how many rows were updated (see below). I know how to get number of rows udpated using MySQLdb, but is there any way to get the number of rows matched? I want to find out, when rows updated = 0, if there were no updates because the row wasn't found (rows matched will = 0) or because the update would not have changed any data (rows matched =...
1
2581
by: Peter Nikolaidis | last post by:
Greetings, I am attempting to get MySQLdb 0.9.2 installed on Mac OS 10.2 with a Fink distribution of Python 2.2.2. I have seen only a few posts on the subject, some of them relate to "conflicting header files," but I don't know what to do about conflicting header files, or where I would find them, and once I found them, which ones to remove. I have compiled MySQL 4.1 and installed into /usr/local/mysql, but since have moved to a Fink...
1
2998
by: Derek Fountain | last post by:
I was trying to use MySQLdb to connect to a database. All is OK, except I can't figure out how to get the details of an error. Suppose I try to connect to a non existant server, or with the wrong password - how do I get a meaningful error message which I can present to my user?
2
5045
by: Tim Williams | last post by:
I'm trying to write a simple python program to access a MySQL database. I'm having a problem with using MySQLdb to get the results of a SQL command in a cursor. Sometimes the cursor.execute works, sometimes not. From mysql: mysql> show databases; +-----------+ | Database |
2
2187
by: ws Wang | last post by:
MySQLdb is working fine at command line, however when I tried to use it with mod_python, it give me a "server not initialized" error. This is working fine: ----------------------- testmy.py ------------------------------- #!/usr/bin/python import MySQLdb db = MySQLdb.connect(host="localhost", user="root", passwd="mypass", db="my_db") cursor = db.cursor()
5
1647
by: sinan , | last post by:
hi everybody, i have a small mysql connection code >>> import MySQLdb >>> db=MySQLdb.Connection(host="localhost",user="root",db="nux") Traceback (most recent call last): File "<stdin>", line 1, in ? File "/usr/lib/python2.3/site-packages/MySQLdb/__init__.py", line 66, in Connect return Connection(*args, **kwargs) File "/usr/lib/python2.3/site-packages/MySQLdb/connections.py", line
1
2735
by: Steve | last post by:
Darwin steve.local 8.3.0 Darwin Kernel Version 8.3.0: Mon Oct 3 20:04:04 PDT 2005; root:xnu-792.6.22.obj~2/RELEASE_PPC Power Macintosh powerpc MacOSX 10.4.3 mysql Ver 14.7 Distrib 4.1.14, for apple-darwin8.2.0 (powerpc) using readline 4.3 runing the software gives me steve:~/MySQL-python-1.2.0 steve$ python setup.py build
1
702
by: Yi Xing | last post by:
Hi, I met the following error when I tried to install MySQLdb. I had no problem installing numarray, Numeric, Rpy, etc. Does anyone know what's the problem? Thanks! running install running build running build_py creating build
0
314
by: Steve Holden | last post by:
Vaibhav.bhawsar wrote: imported The point here is that MySQLdb is a package, not a module. Some packages have their top-level __init__.py import the package's sub-modules or sub-packages to make them immediately available within the package namespace (which is why, for example, you can access os.path.* when you have imported os) and others don't. MySQLdb clearly doesn't need to import the cursors module for its own
0
1097
by: Edwin.Madari | last post by:
replace the name of table before calling *.execute. s.dbptr.execute(str % (e)) good luck. Edwin -----Original Message----- From: python-list-bounces+edwin.madari=verizonwireless.com@python.org On Behalf Of Evan Sent: Thursday, August 14, 2008 11:27 AM
0
8294
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8709
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8494
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8596
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2719
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1597
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.