文章内容

2019/12/9 16:31:30,作 者: 黄兵

MySQLdb fetchone() 获取单条记录

使用MySQLdb 获取单条记录,使用的是fetchone()方法。

返回单个的元组,也就是一条记录(row),如果没有结果 , 则返回 None

代码示例:

# Using a while loop
cursor.execute("SELECT * FROM employees")
row = cursor.fetchone()
while row is not None:
  print(row)
  row = cursor.fetchone()

# Using the cursor as iterator
cursor.execute("SELECT * FROM employees")
for row in cursor:
  print(row)

如果要获取某个字段,代码示例:

get_shopping_cart_one = cur.fetchone()
if get_shopping_cart_one is not None:
    # FIXME:购买的时候检查库存
    user_id = get_shopping_cart_one[2]
    phone_number_section_id = get_shopping_cart_one[9]
    phone_number_use_time_id = get_shopping_cart_one[10]


参考资料:

1、10.5.8 MySQLCursor.fetchone() Method

2、【Python】fetchone()和fetchall()


黄兵个人博客原创。

转载请注明出处:黄兵个人博客 - MySQLdb fetchone() 获取单条记录

分享到:

发表评论

评论列表