python3連接MySQL8.0的兩種方式
1、下載MySQL官方的mysql-connector-python-8.0.17-py3.7-windows-x86-64bit.msi,直接點(diǎn)擊安裝;
2、安裝完畢后直接可以導(dǎo)入mysql.connnector模塊
連接方式一:
import mysql.connector cnx = mysql.connector.connect(user='scott', password='password', host='127.0.0.1', database='employees') cnx.close()
連接方式二:
from mysql.connector import (connection) cnx = connection.MySQLConnection(user='scott', password='password', host='127.0.0.1', database='employees') cnx.close()
用try~except獲取錯(cuò)誤代碼:
import mysql.connector from mysql.connector import errorcode try: cnx = mysql.connector.connect(user='scott', database='employ') except mysql.connector.Error as err: if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: print("Something is wrong with your user name or password") elif err.errno == errorcode.ER_BAD_DB_ERROR: print("Database does not exist") else: print(err) else: cnx.close()
3、獲取數(shù)據(jù)庫(kù)數(shù)據(jù):
import mysql.connector myconn=mysql.connector.connect(host="localhost",user="lucy",passwd="123455",database="holiday") mycursor=myconn.cursor() my_cmd_sql="select * from birthday" a=mycursor.execute(my_cmd_sql) #執(zhí)行SQL命令 for a in mycursor: #展示請(qǐng)求數(shù)據(jù) print(a) mycursor.close() #指針必須關(guān)閉 myconn.close() #連接必須關(guān)閉
4、插入、更改和刪除數(shù)據(jù)
跟上面一樣的方法執(zhí)行插入、更改和數(shù)據(jù)命令,這里有一點(diǎn)區(qū)別,execute語(yǔ)句后必須調(diào)用連接的.commit()方法確認(rèn)執(zhí)行。
另外:指針一定要關(guān)閉,否則容易引起
mysql 2014 error (2014) Commands out of sync; You can't run this command now
總結(jié)
以上所述是小編給大家介紹的python3連接MySQL8.0的兩種方式,希望對(duì)大家有所幫助,也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Python連接mysql方法及常用參數(shù)
- Python連接mysql數(shù)據(jù)庫(kù)及簡(jiǎn)單增刪改查操作示例代碼
- python連接mysql有哪些方法
- Python3連接Mysql8.0遇到的問(wèn)題及處理步驟
- python針對(duì)mysql數(shù)據(jù)庫(kù)的連接、查詢、更新、刪除操作示例
- 使用python連接mysql數(shù)據(jù)庫(kù)之pymysql模塊的使用
- Python 使用 PyMysql、DBUtils 創(chuàng)建連接池提升性能
- python遠(yuǎn)程連接MySQL數(shù)據(jù)庫(kù)
- 詳解Python連接MySQL數(shù)據(jù)庫(kù)的多種方式
- Python實(shí)現(xiàn)連接MySql數(shù)據(jù)庫(kù)及增刪改查操作詳解
- Python 連接 MySQL 的幾種方法
相關(guān)文章
Python數(shù)據(jù)結(jié)構(gòu)之棧、隊(duì)列的實(shí)現(xiàn)代碼分享
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)之棧、隊(duì)列的實(shí)現(xiàn)代碼分享,具有一定參考價(jià)值,需要的朋友可以了解下。2017-12-12python實(shí)現(xiàn)Virginia無(wú)密鑰解密
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)Virginia無(wú)密鑰解密,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03python面試題Python2.x和Python3.x的區(qū)別
這篇文章主要介紹了python面試題Python2.x和Python3.x的區(qū)別 ,在面試中也經(jīng)常會(huì)問(wèn)到,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05解決Python內(nèi)層for循環(huán)如何break出外層的循環(huán)的問(wèn)題
今天小編就為大家分享一篇解決Python內(nèi)層for循環(huán)如何break出外層的循環(huán)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06Python2實(shí)現(xiàn)的圖片文本識(shí)別功能詳解
這篇文章主要介紹了Python2實(shí)現(xiàn)的圖片文本識(shí)別功能,結(jié)合實(shí)例形式分析了Python pytesser庫(kù)的安裝及使用pytesser庫(kù)識(shí)別圖片文字相關(guān)操作技巧,需要的朋友可以參考下2018-07-07python return實(shí)現(xiàn)匯率轉(zhuǎn)換器教程示例
這篇文章主要為大家介紹了python return實(shí)現(xiàn)匯率轉(zhuǎn)換器教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06