2015-03-02 67 views
0

存储我是新来使用MySQL和我想从我的数据库检索和可变打印/存储。我到目前为止已经做到了这一点:读/从MySQL使用变量蟒蛇

import mysql.connector 

conn = mysql.connector.connect(database = 'UserInfo') 

mycursor = conn.cursor() 


Username = "Max" 

Login = ("SELECT username, password FROM UserInfo.User " 
     "WHERE username = '%s'") 
mycursor.execute(Login, (Username)) 

print((username, password) in mycursor) 

mycursor.close() 
conn.close() 
+0

有什么问题吗? – ryanyuyu 2015-03-02 20:48:08

回答

1

您需要使用fetchone()fetchall()检索结果:

username = "Max" 
query = """ 
    SELECT 
     username, password 
    FROM 
     UserInfo.User 
    WHERE 
     username = %s 
""" 

mycursor.execute(query, (username,)) 
username, password = mycursor.fetchone() # here we can unpack the tuple returned from fetchone