前言
本篇用於紀錄使用Python操作MySQL
安裝MySQL
因為之前有下載MAMP,裡面已經安裝過MySQL。
安裝pysql
1 2 3
| pip install pymysql 或 pip3 install pymysql
|
安裝完成後建立名為crud_test的資料庫。
實作
引入pymysql
並創建cursor
1 2 3 4
| import pymysql db = pymysql.connect("127.0.0.1", port=8889 , user="root", password="root", db="crud_test", charset='utf8')
cursor = db.cursor()
|
cursor()
為獲得python執行MySQL語法的方法
查詢資料庫版本
sql查詢指令為SELECT VERSION()
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| sql = 'SELECT VERSION()'
cursor.execute(sql) print("Success!!!!")
data = cursor.fetchone()
print ("Database version : %s " % data)
db.close()
|
新增資料
先定義表格結構
insert
語法:
insert into 資料表名稱(欄位1, 欄位2, 欄位3....) values (欄位1的值,欄位2的值, 欄位3的值.... );
範例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import pymysql from datetime import datetime db = pymysql.connect("127.0.0.1", port=8889 , user="root", password="root", db="crud_test", charset='utf8')
cursor = db.cursor()
sql = "insert into student_record(sid, name, gender, age, score) values (1, 'Jean', 'female', '20', 100);"
try: cursor.execute(sql) db.commit() print('success') except: db.rollback() print('error')
db.commit()
data = cursor.fetchone()
|
務必記得提交commit,不然無法存新建或者修改的資料
查看結果:新增成功一筆資料
修改資料
update
語法:
update record set 欄位名 where 更新條件
set
後面接更新目標的欄位,where
後面接更新的條件
範例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import pymysql from datetime import datetime db = pymysql.connect("127.0.0.1", port=8889 , user="root", password="root", db="crud_test", charset='utf8')
cursor = db.cursor()
sql = "update record set name= 'Tony' where age = '30' " try: cursor.execute(sql) db.commit() print('success') except: db.rollback() print('error')
db.commit()
data = cursor.fetchone()
db.close()
|
查詢
select 目標欄位 from 資料表
,返回值的資料型態為tuple
範例(選取第一筆結果):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import pymysql from datetime import datetime db = pymysql.connect("127.0.0.1", port=8889 , user="root", password="root", db="crud_test", charset='utf8')
cursor = db.cursor()
sql = "select * from student_record"
try: cursor.execute(sql) db.commit() print('success') except: db.rollback() print('error')
db.commit()
data = cursor.fetchone() print(data)
|
範例(選取全部結果):
採用fetchall()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import pymysql from datetime import datetime db = pymysql.connect("127.0.0.1", port=8889 , user="root", password="root", db="crud_test", charset='utf8')
cursor = db.cursor()
sql = "select * from student_record"
try: cursor.execute(sql) db.commit() print('success') except: db.rollback() print('error')
db.commit()
data = cursor.fetchall() print(data)
|
範例(指定取回查詢筆數):
fetchmany(size= number)
,size
限制查詢筆數。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import pymysql from datetime import datetime db = pymysql.connect("127.0.0.1", port=8889 , user="root", password="root", db="crud_test", charset='utf8')
cursor = db.cursor()
sql = "select * from student_record"
try: cursor.execute(sql) db.commit() print('success') except: db.rollback() print('error')
db.commit()
data = cursor.fetchmany(size=2) print(data)
|
刪除
delete from 資料表名稱 where 刪除條件
範例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import pymysql from datetime import datetime db = pymysql.connect("127.0.0.1", port=8889 , user="root", password="root", db="crud_test", charset='utf8')
cursor = db.cursor()
sql = "delete from student_record where name= 'Jack' "
try: cursor.execute(sql) db.commit() print('success') except: db.rollback() print('error')
db.commit()
data = cursor.fetchmany(size=2) print(data)
|
結果
參考文章
Day22- Python X MySql 2