Python練習(xí)之操作MySQL數(shù)據(jù)庫(kù)
文章介紹內(nèi)容:
操作MySQL數(shù)據(jù)庫(kù):
- 創(chuàng)建MySQL數(shù)據(jù)表;
- 向表中插入記錄;
- 其他數(shù)據(jù)庫(kù)操作。
面試題:
- 如何創(chuàng)建MySQL數(shù)據(jù)表?
- 如何向MySQL表中插入數(shù)據(jù)?
- 如何查詢MySQL中的數(shù)據(jù)?
一、創(chuàng)建MySQL數(shù)據(jù)表
# coding=utf-8
from pymysql import *
def connectDB():
'''
連接本地MySQL數(shù)據(jù)庫(kù),指定連接的庫(kù)為test庫(kù)。
:return:
'''
db = connect(host='localhost', user='root', password='123456', port=3306, db='test')
return db
db = connectDB()
print(type(db))
def createTable(db):
c = db.cursor()
try:
c.execute('''create table persons
(id int primary key not null,
name text not null,
age int not null,
address char(100),
salary real);''')
db.commit()
db.commit()
return True
except:
db.rollback()
return False
if createTable(db):
print('create table success')
else:
print('create table failed')
使用navicat工具查看:

三、向MySQL表中插入數(shù)據(jù)
# coding=utf-8
from pymysql import *
def connectDB():
'''
連接本地MySQL數(shù)據(jù)庫(kù),指定連接的庫(kù)為test庫(kù)。
:return:
'''
db = connect(host='localhost', user='root', password='123456', port=3306, db='test')
return db
db = connectDB()
print(type(db))
def insertRecords(db):
cursor = db.cursor()
try:
cursor.execute("delete from persons")
cursor.execute('''
insert into persons(id,name,age,address,salary)
values(1, 'GuHanZhe', 18, 'China', 9999)
''')
cursor.execute('''
insert into persons(id,name,age,address,salary)
values(2, 'XiaoZhang', 55, 'China', 9)
''')
db.commit()
return True
except Exception as e:
print(e)
db.rollback()
return False
if insertRecords(db):
print("成功插入記錄")
else:
print("插入記錄失敗")
使用navicat工具查看:

三、查詢MySQL中的數(shù)據(jù)
# coding=utf-8
from pymysql import *
def connectDB():
'''
連接本地MySQL數(shù)據(jù)庫(kù),指定連接的庫(kù)為test庫(kù)。
:return:
'''
db = connect(host='localhost', user='root', password='123456', port=3306, db='test')
return db
db = connectDB()
def selectRecords(db):
cursor = db.cursor()
sql = 'select name,age,salary from persons order by age desc'
cursor.execute(sql)
results = cursor.fetchall()
print(results)
print(type(results)) # 打印發(fā)現(xiàn)是元組類型
selectRecords(db)
db.close()
- 我們發(fā)現(xiàn)查詢數(shù)據(jù)輸出類型是元組類型,如果我們想要將字段名和查詢出的數(shù)據(jù)一一對(duì)應(yīng)該怎么做呢?
- 這里就需要用到兩個(gè)很常用的函數(shù)dict()和zip(),如下:
# coding=utf-8
import json
from pymysql import *
def connectDB():
'''
連接本地MySQL數(shù)據(jù)庫(kù),指定連接的庫(kù)為test庫(kù)。
:return:
'''
db = connect(host='localhost', user='root', password='123456', port=3306, db='test')
return db
db = connectDB()
def selectRecords(db):
cursor = db.cursor()
sql = 'select name,age,salary from persons order by age desc'
cursor.execute(sql)
results = cursor.fetchall()
print(results)
print(type(results)) # 打印發(fā)現(xiàn)是元組類型
# 將字段名和查詢結(jié)果整合在一起
fields = ['name', 'age', 'salary']
records = []
for row in results:
records.append(dict(zip(fields, row)))
return json.dumps(records) # 輸出類型為JSON字符串
endresults = selectRecords(db)
print(endresults)
db.close()
總結(jié)
注意:我們使用的是pymysql模塊中的API來(lái)操作MySQL數(shù)據(jù)庫(kù),該模塊需要單獨(dú)安裝
到此這篇關(guān)于Python練習(xí)之操作MySQL數(shù)據(jù)庫(kù)的文章就介紹到這了,更多相關(guān)Python 操作MySQL內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python基礎(chǔ)之操作MySQL數(shù)據(jù)庫(kù)
- 教你怎么用Python操作MySql數(shù)據(jù)庫(kù)
- Python連接Postgres/Mysql/Mongo數(shù)據(jù)庫(kù)基本操作大全
- python中的mysql數(shù)據(jù)庫(kù)LIKE操作符詳解
- Python接口自動(dòng)化淺析pymysql數(shù)據(jù)庫(kù)操作流程
- 利用python中pymysql操作MySQL數(shù)據(jù)庫(kù)的新手指南
- Python操作MySQL MongoDB Oracle三大數(shù)據(jù)庫(kù)深入對(duì)比
- Python MySQL數(shù)據(jù)庫(kù)基本操作及項(xiàng)目示例詳解
- python?實(shí)現(xiàn)?pymysql?數(shù)據(jù)庫(kù)操作方法
相關(guān)文章
Python實(shí)現(xiàn)讀取文件中的特定行的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何在Python中實(shí)現(xiàn)讀取文件中的特定行,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
Python 把序列轉(zhuǎn)換為元組的函數(shù)tuple方法
今天小編就為大家分享一篇Python 把序列轉(zhuǎn)換為元組的函數(shù)tuple方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
python實(shí)現(xiàn)AES算法及AES-CFB8加解密源碼
這篇文章主要為大家介紹了python實(shí)現(xiàn)AES算法及AES-CFB8加解密的源碼示例,有需要朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02
pycharm命令終端運(yùn)行python文件以及傳遞參數(shù)方式
這篇文章主要介紹了pycharm命令終端運(yùn)行python文件以及傳遞參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Python獲取當(dāng)前時(shí)間日期的方法總結(jié)
在Python編程中,獲取當(dāng)前的日期和時(shí)間是一個(gè)常見的需求,它在許多應(yīng)用中都有重要作用,本文為大家詳細(xì)整理了一些Python中的常用方法,希望對(duì)大家有所幫助2024-01-01
python利用百度云接口實(shí)現(xiàn)車牌識(shí)別的示例
這篇文章主要介紹了python利用百度云接口實(shí)現(xiàn)車牌識(shí)別的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Python如何基于selenium實(shí)現(xiàn)自動(dòng)登錄博客園
這篇文章主要介紹了Python如何基于selenium實(shí)現(xiàn)自動(dòng)登錄博客園,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12

