简体   繁体   中英

Python 2.7: execute sql query

I am trying to execute a sql query to extract one cell from a table.

This is my code below:

def gPPT(self, rfId):
    with DBCursor(self.db) as cur:
                cur.execute("SELECT p_p_t " +
                            "FROM q_r_f_d WHERE r_f_i = ?", (rfId))

                pPT = cur.fetchone() 
    return pPT

This query will always return one string. When I run my python code I always get the following error message:

... an exception of type 'ValueError' occurred
Details:
    parameters are of unsupported type

Anyone know where the error is in my code? I tried executing the query directly on my database and it returned exactly what I expected.

The DB-API specifies that parameters should be a sequence. Note that the parens/brackets do not make the tuple; the comma does. Simply add the comma.

        cur.execute("SELECT p_p_t " +
                    "FROM q_r_f_d WHERE r_f_i = ?", (rfId,))
                                                       # ^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM