Python練習(xí)之操作MySQL數(shù)據(jù)庫
文章介紹內(nèi)容:
操作MySQL數(shù)據(jù)庫:
- 創(chuàng)建MySQL數(shù)據(jù)表;
- 向表中插入記錄;
- 其他數(shù)據(jù)庫操作。
面試題:
- 如何創(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ù)庫,指定連接的庫為test庫。 :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ù)庫,指定連接的庫為test庫。 :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ù)庫,指定連接的庫為test庫。 :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ù)一一對應(yīng)該怎么做呢?
- 這里就需要用到兩個很常用的函數(shù)dict()和zip(),如下:
# coding=utf-8 import json from pymysql import * def connectDB(): ''' 連接本地MySQL數(shù)據(jù)庫,指定連接的庫為test庫。 :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來操作MySQL數(shù)據(jù)庫,該模塊需要單獨(dú)安裝
到此這篇關(guān)于Python練習(xí)之操作MySQL數(shù)據(jù)庫的文章就介紹到這了,更多相關(guān)Python 操作MySQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python基礎(chǔ)之操作MySQL數(shù)據(jù)庫
- 教你怎么用Python操作MySql數(shù)據(jù)庫
- Python連接Postgres/Mysql/Mongo數(shù)據(jù)庫基本操作大全
- python中的mysql數(shù)據(jù)庫LIKE操作符詳解
- Python接口自動化淺析pymysql數(shù)據(jù)庫操作流程
- 利用python中pymysql操作MySQL數(shù)據(jù)庫的新手指南
- Python操作MySQL MongoDB Oracle三大數(shù)據(jù)庫深入對比
- Python MySQL數(shù)據(jù)庫基本操作及項(xiàng)目示例詳解
- python?實(shí)現(xiàn)?pymysql?數(shù)據(jù)庫操作方法
相關(guān)文章
python+appium實(shí)現(xiàn)自動化測試的示例代碼
appium是一個開源的測試自動化框架,可以與原生的、混合的和移動的web應(yīng)用程序使用,本文主要介紹了python+appium實(shí)現(xiàn)自動化測試的示例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01python神經(jīng)網(wǎng)絡(luò)編程之手寫數(shù)字識別
這篇文章主要介紹了python神經(jīng)網(wǎng)絡(luò)編程之手寫數(shù)字識別,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python神經(jīng)網(wǎng)絡(luò)編程的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05Python實(shí)現(xiàn)iOS自動化打包詳解步驟
這篇文章主要介紹了Python實(shí)現(xiàn)iOS自動化打包詳解步驟,文中通過示例代碼以及圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10Python?庫?PySimpleGUI?制作自動化辦公小軟件的方法
Python?在運(yùn)維和辦公自動化中扮演著重要的角色,PySimpleGUI?是一款很棒的自動化輔助模塊,讓你更輕松的實(shí)現(xiàn)日常任務(wù)的自動化,下面通過本文給大家介紹下Python?庫?PySimpleGUI?制作自動化辦公小軟件的過程,一起看看吧2021-12-12