用python連線本地的mysql
import pymysql
# 1.獲取連線物件
conn = pymysql.connect(host="localhost",user="root",passwd="123456",db="myschool",charset="utf8")
#2.從連線物件中獲取遊標
cur =conn.cursor()
# 3.組裝sql語句
sql = "select * from stu_score"
# 4.執行sql語句
cur.execute(sql)
#5.如果sql語句是像建表、建庫,新增資料之類的需要提交sql,要commit
# conn.commit()
# 獲取一條資料
one = cur.fetchone()
print(one)
# 獲取多條記錄"
many = cur.fetchmany(3)
print(many)
# 獲取所有記錄
data = cur.fetchall()
print(data)
#迴圈獲取資料,列印成表格格式
for stu_score in data:
for j in stu_score:
print(j, end="\t")
print(end="\n")
#關閉遊標
cur.close()
#關閉連線
conn.close()
用python連線本地的mysql
import pymysql
# 1.獲取連線物件
conn = pymysql.connect(host="localhost",user="root",passwd="123456",db="myschool",charset="utf8")
#2.從連線物件中獲取遊標
cur =conn.cursor()
# 3.組裝sql語句
sql = "select * from stu_score"
# 4.執行sql語句
cur.execute(sql)
#5.如果sql語句是像建表、建庫,新增資料之類的需要提交sql,要commit
# conn.commit()
# 獲取一條資料
one = cur.fetchone()
print(one)
# 獲取多條記錄"
many = cur.fetchmany(3)
print(many)
# 獲取所有記錄
data = cur.fetchall()
print(data)
#迴圈獲取資料,列印成表格格式
for stu_score in data:
for j in stu_score:
print(j, end="\t")
print(end="\n")
#關閉遊標
cur.close()
#關閉連線
conn.close()