48

作者 | Python語(yǔ)音識(shí)別
來(lái)源 | 深度學(xué)習(xí)與python(ID:PythonDC)
不管是機(jī)器學(xué)習(xí)、web開發(fā)或者爬蟲,數(shù)據(jù)庫(kù)都是繞不過去的。那么今天我們就來(lái)介紹Python如何MySQL數(shù)據(jù)庫(kù)進(jìn)行連接以及數(shù)據(jù)的交換。主要分為以下幾個(gè)方面:
-
什么是數(shù)據(jù)庫(kù)?
-
什么是MySQLdb?
-
Python如何連接數(shù)據(jù)庫(kù)?
-
創(chuàng)建數(shù)據(jù)庫(kù)數(shù)據(jù)庫(kù)操作-CRUD
數(shù)據(jù)庫(kù)基本上是結(jié)構(gòu)化數(shù)據(jù)的集合,通過數(shù)據(jù)庫(kù)可以用各種方式輕松地檢索,管理和訪問數(shù)據(jù)。最簡(jiǎn)單的數(shù)據(jù)庫(kù)形式之一是文本數(shù)據(jù)庫(kù)。目前關(guān)系數(shù)據(jù)庫(kù)是最流行的數(shù)據(jù)庫(kù)系統(tǒng),目前主流的關(guān)系數(shù)據(jù)庫(kù)主要由以下幾個(gè):
- MySQL
- Oracle Database
- SQL server
- Sybase
- Informix
- IBM db2
- NO SQL
其中MySQL是最容易使用的數(shù)據(jù)庫(kù),也是我們這次所要介紹的。
什么是MySQLdb
MySQLdb是一個(gè)開源免費(fèi)的關(guān)系數(shù)據(jù)庫(kù)管理系統(tǒng),它使用結(jié)構(gòu)化查詢語(yǔ)言。SQL(結(jié)構(gòu)化查詢語(yǔ)言)是關(guān)系數(shù)據(jù)庫(kù)的標(biāo)準(zhǔn)語(yǔ)言,允許用戶對(duì)數(shù)據(jù)進(jìn)行各種操作,如操作,創(chuàng)建,刪除等。簡(jiǎn)而言之,SQL允許您對(duì)數(shù)據(jù)執(zhí)行任何操作。
Python如何連接數(shù)據(jù)庫(kù)
Python連接數(shù)據(jù)庫(kù)的方法非常簡(jiǎn)單,下圖表示Python與數(shù)據(jù)庫(kù)的基本數(shù)據(jù)交換原理。

在連接MySQL數(shù)據(jù)庫(kù)之前,請(qǐng)確保在計(jì)算機(jī)上安裝了MySQL應(yīng)用程序。也可使用遠(yuǎn)程數(shù)據(jù)庫(kù),MySQL應(yīng)用程序提供了一下數(shù)據(jù)工具:MySQL服務(wù)器、所有可用連接器、MySQL Workbench、MySQL通知程序、用于Excel和Microsoft Visual Studio的工具、MySQL示例數(shù)據(jù)庫(kù)、MySQL文檔。
安裝好應(yīng)用程序之后,我們還需要安裝python中的Mysql函數(shù)庫(kù)mysql.connector,這個(gè)可以直接使用pip進(jìn)行安裝。連接數(shù)據(jù)庫(kù)需要的基本參數(shù)是:
- 用戶名 -它只是您為MySQL服務(wù)器工作的用戶名,默認(rèn)用戶名為root。
- 密碼 -密碼由用戶在安裝MySQL數(shù)據(jù)庫(kù)時(shí)提供。我在這里給密碼'password123'
- 主機(jī)名 -這基本上是運(yùn)行MySQL的服務(wù)器名稱或IP地址,如果它是'localhost',那么你的IP地址是127.0.0.0
以下是Python連接數(shù)據(jù)庫(kù)的程序
importmysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="password123")
print(mydb)
運(yùn)行程序輸出為:
C:UsersHarshit_KantPycharmProjectstest1venvspython.exe C:/Users/Harshit_Kant/PycharmProjects/test1/venv/python-db-conn.py
<mysql.connector.connection_cext.CMySQLConnection object at 0x000001606D7BD6A0>
這里'mydb'只是一個(gè)示例。從輸出中可以清楚地看到Python已連接到數(shù)據(jù)庫(kù)。
創(chuàng)建數(shù)據(jù)庫(kù)
經(jīng)過上面一步,我們已經(jīng)成功建立數(shù)據(jù)庫(kù)連接,現(xiàn)在您就可以創(chuàng)建自己的數(shù)據(jù)庫(kù),它將充當(dāng)python和MySQL服務(wù)器之間數(shù)據(jù)交流的橋梁。建立數(shù)據(jù)庫(kù)代碼如下:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="password123")
mycursor=mydb.cursor
mycursor.execute("create database harshdb")
程序說明:
- 在上面的程序中使用了游標(biāo),它基本上是一個(gè)用于與整個(gè)MySQL服務(wù)器通信的對(duì)象,通過它我可以創(chuàng)建自己的數(shù)據(jù)庫(kù)。
- 您可以從輸出中看到創(chuàng)建了名為“harshdb”的數(shù)據(jù)庫(kù),該數(shù)據(jù)庫(kù)是自定義的,因?yàn)槟梢詾閿?shù)據(jù)庫(kù)指定任何名稱。
同時(shí),如果你想要查看服務(wù)器中已經(jīng)建立的數(shù)據(jù)庫(kù),可以使用以下代碼:
importmysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="password123")
mycursor=mydb.cursor
mycursor.execute("show databases")
fordb inmycursor:
print(db)
輸出示例為:
('harshdb')
('information_schema')
('mysql')
('performance_schema')
('sakila')
('sys')
('world')
數(shù)據(jù)庫(kù)操作
數(shù)據(jù)庫(kù)的基本操作包括:創(chuàng)建、讀取、更新和刪除,下面我通過示例程序來(lái)為大家演示基本使用方法。
創(chuàng)建操作:用于在表中創(chuàng)建記錄的SQL語(yǔ)句,或者可以說它用于創(chuàng)建表。代碼如下:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="password123",database=harshdb)
mycursor=mydb.cursor
mycursor.execute("create table employee(name varchar(250),sal int(20))")
程序說明:
- 在上面給出的程序中,我創(chuàng)建了一個(gè)表'employee'。
- 表員工有兩個(gè)字段'name'和'sal'。
- 這里,User id是“root”,Password是“password123”,用于訪問harshdb。
下面給出的屏幕截圖顯示了表'employee'并返回字段'name'和'sal'。

讀取寫入:用于從數(shù)據(jù)庫(kù)中獲取有用信息。代碼示例如下:
importmysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="password123",database="harshdb")
mycursor=mydb.cursor
sqlformula = "Insert into employee(name,sal) values(%s,%s)"//'values has placeholders
employees = [("harshit",200000),("rahul", 30000),("avinash", 40000),("amit", 50000),]//Created an array of emplpoyees
mycursor.executemany(sqlformula, employees)//Passing the data
mydb.commit//SQL statement used for saving the changes
在上面的代碼中,我通過在Python中編寫SQL語(yǔ)句寫入一組員工數(shù)據(jù)。寫入之后數(shù)據(jù)庫(kù)的屏幕截圖顯示如下:

更新 :用于更新表中的記錄或更新表。代碼如下:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="password123",database="harshdb")
mycursor=mydb.cursor
sql = "Update employee SET sal = 70000 WHERE name = 'harshit'"
mycursor.execute(sql)
mydb.commit
程序說明:我們?cè)谏厦娼o出的代碼中更新了harshit的行“sal”。下面給出的數(shù)據(jù)庫(kù)截圖顯示更新結(jié)果。

刪除 :用于刪除表格。代碼示例如下:
import mysql.connector
mydb = mysql.connector.connect(host =“localhost”,user =“root”,passwd =“password123”,database =“harshdb”)
mycursor = mydb.cursor
sql =“DELETE FROM employee WHERE name ='harshit'“
mycursor.execute(sql)
mydb.commit
程序說明:在上面的代碼中,我刪除了一條'harshit'的重復(fù)記錄。
Python連接數(shù)據(jù)庫(kù)還有一個(gè)pymysql函數(shù)包,該包也十分簡(jiǎn)單且方便的與數(shù)據(jù)庫(kù)進(jìn)行交互,大家可以嘗試一下。
參考
https://medium.com/edureka/python-database-connection-b4f9b301947c
(*本文僅代表作者觀點(diǎn),轉(zhuǎn)載請(qǐng)聯(lián)系原作者)






