Python Select From
- 上一页 MySQL Insert
- 下一页 MySQL Where
အချက်အလက်များ မှ ရယူ
MySQL အချက်အလက်များ မှ ရယူရန် ရှိသော အမည်များ ကို အသုံးပြုပါ:
实例
customers အချက်အလက်များ အား ရယူပြီး အမှတ်အသား ပြသည်:
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", passwd="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() mycursor.execute("SELECT * FROM customers") myresult = mycursor.fetchall() for x in myresult: print(x)
ဖော်ပြချက်ကျွန်တော်တို့သည် အသုံးပြုခဲ့သည် fetchall()
မူကြေတ် သို့မဟုတ် နောက်ဆုံး အမည်များ မှ အားလုံး သုံးစွဲသည်。
选取列
如需只选择表中的某些列,请使用 "SELECT" 语句,后跟列名:
实例
仅选择名称和地址列:
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", passwd="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() mycursor.execute("SELECT name, address FROM customers") myresult = mycursor.fetchall() for x in myresult: print(x)
使用 fetchone() 方法
如果您只对一行感兴趣,可以使用 fetchone()
方法。
fetchone()
方法将返回结果的第一行:
实例
仅获取一行:
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", passwd="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() mycursor.execute("SELECT * FROM customers") myresult = mycursor.fetchone() print(myresult)
- 上一页 MySQL Insert
- 下一页 MySQL Where