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計(jì)算程序運(yùn)行時(shí)間的方法
這篇文章主要介紹了Python計(jì)算程序運(yùn)行時(shí)間的方法,分別記錄起始時(shí)間與結(jié)束時(shí)間,計(jì)算兩者之間的差值來(lái)獲得程序的運(yùn)行時(shí)間,需要的朋友可以參考下2014-12-12python+appium實(shí)現(xiàn)自動(dòng)化測(cè)試的示例代碼
appium是一個(gè)開(kāi)源的測(cè)試自動(dòng)化框架,可以與原生的、混合的和移動(dòng)的web應(yīng)用程序使用,本文主要介紹了python+appium實(shí)現(xiàn)自動(dòng)化測(cè)試的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01python神經(jīng)網(wǎng)絡(luò)編程之手寫(xiě)數(shù)字識(shí)別
這篇文章主要介紹了python神經(jīng)網(wǎng)絡(luò)編程之手寫(xiě)數(shù)字識(shí)別,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python神經(jīng)網(wǎng)絡(luò)編程的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05Python實(shí)現(xiàn)iOS自動(dòng)化打包詳解步驟
這篇文章主要介紹了Python實(shí)現(xiàn)iOS自動(dòng)化打包詳解步驟,文中通過(guò)示例代碼以及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10提升?Python?代碼運(yùn)行速度的6個(gè)技巧
本文分享了提升?Python?代碼運(yùn)行速度的6個(gè)技巧,Python?比我們想象的運(yùn)行的要快。我們之所以有先入為主的認(rèn)為Python運(yùn)行慢,可能是我們平常的誤用和缺乏使用技巧知識(shí)。接下來(lái)讓我們看看如何用一些簡(jiǎn)單的Trick來(lái)提高我們程序的運(yùn)行性能,需要的朋友可以參考一下2022-01-018個(gè)實(shí)用的Python程序你知道幾個(gè)
這篇文章主要為大家詳細(xì)介紹了8個(gè)實(shí)用的Python程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助<BR>2022-02-02Python?庫(kù)?PySimpleGUI?制作自動(dòng)化辦公小軟件的方法
Python?在運(yùn)維和辦公自動(dòng)化中扮演著重要的角色,PySimpleGUI?是一款很棒的自動(dòng)化輔助模塊,讓你更輕松的實(shí)現(xiàn)日常任務(wù)的自動(dòng)化,下面通過(guò)本文給大家介紹下Python?庫(kù)?PySimpleGUI?制作自動(dòng)化辦公小軟件的過(guò)程,一起看看吧2021-12-12