如何使用python

时间:2018-02-19 08:13:15

标签: python mysql database

我想使用python从数据库表中获取值。 我发送一个查询并得到这样的值:

conn = pymysql.connect(rds_host, user=name, passwd=password,db=db_name, connect_timeout=10)
with conn.cursor() as cur:
    cur.execute("SELECT id FROM user_details WHERE email='{}'".format(email)
    for row in cur:
        id = row[0]

有没有办法在不使用for循环的情况下获取值。

1 个答案:

答案 0 :(得分:1)

找不到文档?

https://pymysql.readthedocs.io/en/latest/modules/cursors.html#pymysql.cursors.Cursor.fetchone

  

cursor.fetchone()

     
    

获取下一行

  

另外,你肯定不想使用字符串格式来构建你的查询(unless you're ok to have your app wide opened to sql injections of couse)。你想要使用准备好的查询:

cur.execute("SELECT id FROM user_details WHERE email=?", [email,])