RE: a question about mysqldb

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Edwin.Madari@VerizonWireless.com

    RE: a question about mysqldb

    replace the name of table before calling *.execute.
    s.dbptr.execute (str % (e[0]))

    good luck.
    Edwin
    -----Original Message-----
    From: python-list-bounces+edwin.m adari=verizonwi reless.com@pyth on.org
    [mailto:python-list-bounces+edwin.m adari=verizonwi reless.com@pyth on.org]
    On Behalf Of Evan
    Sent: Thursday, August 14, 2008 11:27 AM
    To: python-list@python.org
    Subject: 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" <----------checkhere
    >>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,
    --



    The information contained in this message and any attachment may be
    proprietary, confidential, and privileged or subject to the work
    product doctrine and thus protected from disclosure. If the reader
    of this message is not the intended recipient, or an employee or
    agent responsible for delivering this message to the intended
    recipient, you are hereby notified that any dissemination,
    distribution or copying of this communication is strictly prohibited.
    If you have received this communication in error, please notify me
    immediately by replying to this message and deleting it and all
    copies and backups thereof. Thank you.


Working...