2015-03-02 140 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