MySQL系列--4.使用Python3访问数据库
生活随笔
收集整理的這篇文章主要介紹了
MySQL系列--4.使用Python3访问数据库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、安裝MySQL驅動
pip install mysql-connector
安裝完成后進入命令行模式,導入驅動,如果不報錯,說明安裝成功
2、安裝MySQL
MySQL安裝請參考:https://www.cnblogs.com/webDepOfQWS/p/10685617.html
3、操作數據庫
MySQL操作數據庫的一般步驟如下:
a、建立連接
b、通過連接對象得到游標對象
c、執行SQL語句,獲取執行結果,如果執行的SQL語句會改變數據庫或表 ,需要提交,才會保存修改。
d、關閉游標對象,關閉連接對象。
創建表并插入數據
在rms數據庫中創建一張表:user_info并插入2條數據,創建表SQL語句如下:
create table user_info(id int(10) primary key,name char(20) not null,passwd char(40) not null,email char(20) not null,phone char(20) not null,role char(10) not null,sex char(10) not null,status int(10) not null,createAt datetime not null,exprAt datetime not null,validDays int(10) not null,delAt datetime )ENGINE=InnoDB DEFAULT CHARSET=utf8;Python3代碼:
#coding:utf-8 #導入驅動 import mysql.connector #建立連接 conn = mysql.connector.connect(host="127.0.0.1",user='root',database='rms',password='password') #獲游標標對象 cursor = conn.cursor() #SQL語句 SQL1='''create table user_info(id int(10) primary key,name char(20) not null,passwd char(40) not null,email char(20) not null,phone char(20) not null,role char(10) not null,sex char(10) not null,status int(10) not null,createAt datetime not null,exprAt datetime not null,validDays int(10) not null,delAt datetime )ENGINE=InnoDB DEFAULT CHARSET=utf8;''' SQL2='''insert into user_info values (1,"StephenWang7","123456","123@qq.com","15103887470","admin","male","200","20190412201130","20190419201130",30,null)''' try:#執行創建表的SQL語句cursor.execute(SQL1)#執行插入語句cursor.execute(SQL2)#提交conn.commit() except Exception as e:print(e) finally:#關閉游標對象cursor.close()#關閉連接conn.close連接數據庫查看結果:
mysql> select count(*) from user_info; +----------+ | count(*) | +----------+ | 2 | +----------+ 1 row in set (0.27 sec)mysql>查詢SQL執行結果
fetchone():返回一條結果。
fetchall():返回所有結果。
fetchmany([size]):返回size條結果。
示例1:
輸出1:
#返回的是一個tuple (2,)示例2:
查詢user_info表中所有的記錄。
運行示例2的代碼時,報錯:Unread result found,在連接數據庫時設置'buffered': True。
conn = mysql.connector.connect(host="127.0.0.1",user='root',database='rms',password='password',buffered=True)輸出2:
(1, 'StephenWang7', '123456', '123@qq.com', '15103887470', 'admin', 'male', 200, datetime.datetime(2019, 4, 12, 20, 11, 30), datetime.datetime(2019, 4, 19, 20, 11, 30), 30, None)更新和刪除
更新和刪除的代碼與創建表類似,需要說明的一點是執行語句之后需要提交(commmit)。
#coding:utf-8 #導入驅動 import mysql.connector #建立連接 conn = mysql.connector.connect(host="127.0.0.1",user='root',database='rms',password='password',buffered=True) #獲游標標對象 cursor = conn.cursor() try:#執行更新語句cursor.execute("update user_info set passwd=%s where id=%s",['py123456',1])#獲取執行結果result = cursor.fetchone()print(result)#提交conn.commit() except Exception as e:print(e) finally:#關閉游標對象cursor.close()#關閉連接conn.closeMySQL的占位符為%s,連接數據庫查看結果:
mysql> select passwd from user_info where id=1; +----------+ | passwd | +----------+ | py123456 | +----------+ 1 row in set (0.03 sec)轉載于:https://www.cnblogs.com/webDepOfQWS/p/10693105.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的MySQL系列--4.使用Python3访问数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 谷歌浏览器无法登陆禅道问题
- 下一篇: 热带雨林绘画软件测试,儿童画教程|色彩练