bigint of mysql-python的格式说明符

时间:2015-08-21 09:15:55

标签: python mysql types mysql-connector-python

我的主键声明为:

id bigint PRIMARY KEY

我想提取某个ID,并希望进一步使用它。

localid = cursor.fetchone()[0]
print type(localid)
query1 = ("Select * from table_name WHERE id= %d;")
cursor.execute(query1, localid)
query2 = ("Select * from table_name WHERE id= 1;")
cursor.execute(query2)
  1. type(localid)目前打印为int,其中获取的值仅为2或3或45。
  2. query2在query2不起作用时不起作用。
  3. %d是正确的说明符吗?我不这么认为。
  4. 如果提取的数字确实超出了正常int的范围,那么%d会是正确的吗?如果没有,该怎么用?
  5. 额外信息:使用Mysql-python连接器包。 Python 2.7

1 个答案:

答案 0 :(得分:1)

如果您使用的是MySQLdb,则%s功能中只需要execute

您的Mysql-python确实是MySQLdb

<强>解决方法1 `:

query1 = ("Select * from table_name WHERE id= %s;")
cursor.execute(query1, (localid,))

Note: If args is a sequence, then %s must be used as the
      parameter placeholder in the query. If a mapping is used,
      %(key)s must be used as the placeholder.

<强>溶液2

query1 = ("Select * from table_name WHERE id= %d;" % localid)
cursor.execute(query1)
Mysqldb.cursors

详细说明

class BaseCursor(__builtin__.object)
 |  A base for Cursor classes. Useful attributes:
 |  
 |  description
 |      A tuple of DB API 7-tuples describing the columns in
 |      the last executed query; see PEP-249 for details.
 |  
 |  description_flags
 |      Tuple of column flags for last query, one entry per column
 |      in the result set. Values correspond to those in
 |      MySQLdb.constants.FLAG. See MySQL documentation (C API)
 |      for more information. Non-standard extension.
 |  
 |  arraysize
 |      default number of rows fetchmany() will fetch
 |  
 |  Methods defined here:
 |  execute(self, query, args=None)
 |      Execute a query.
 |      
 |      query -- string, query to execute on server
 |      args -- optional sequence or mapping, parameters to use with query.
 |      
 |      Note: If args is a sequence, then %s must be used as the   #notice
 |      parameter placeholder in the query. If a mapping is used,
 |      %(key)s must be used as the placeholder.
 |      
 |      Returns long integer rows affected, if any
 |