程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python可視化數據分析09、MySQL讀寫

編輯:Python

 ​編輯

Python可視化數據分析09、MySQL讀寫

前言

博客:【紅目香薰的博客_CSDN博客-計算機理論,2022年藍橋杯,MySQL領域博主】

本文由在下【紅目香薰】原創,首發於CSDN

2022年最大願望:【服務百萬技術人次】

Python初始環境地址:【Python可視化數據分析01、python環境搭建】 


環境需求

環境:win10

開發工具:PyCharm Community Edition 2021.2

數據庫:MySQL5.6

目錄

Python可視化數據分析09、MySQL讀寫

前言

環境需求

前置環境

數據庫

數據表

python鏈接MySQL

python操作MySQL增刪改查



前置環境

pip3 config set global.index-url https://repo.huaweicloud.com/repository/pypi/simplepip3 config listpip3 install --upgrade pippip3 install pymysql

​編輯

數據庫

​編輯

數據表

CREATE TABLE `users` ( `id` int(8) NOT NULL AUTO_INCREMENT, `userName` varchar(255) NOT NULL, `age` int(11) NOT NULL, `introduce` varchar(255) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;

python鏈接MySQL

import pymysql # 鏈接MySQLdb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='12345678', db='mytest', charset='utf8')print(db)

​編輯

python操作MySQL增刪改查

import pymysql # 鏈接MySQLdb = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='12345678', db='mytest', charset='utf8')cursor = db.cursor() # 編寫SQLsqlInsert1 = "insert into users values(0,'{0}',{1},'{2}')".format("雷靜", 22, "柔似一泓清水的雙眼")sqlInsert2 = "insert into users values(0,'{0}',{1},'{2}')".format("小鳳", 21, "小女孩")sqlInsert3 = "insert into users values(0,'{0}',{1},'{2}')".format("春夢", 20, "潇灑霸氣")sqlInsert4 = "insert into users values(0,'{0}',{1},'{2}')".format("刪除測試", 20, "待刪除數據")cursor.execute(sqlInsert1)cursor.execute(sqlInsert2)cursor.execute(sqlInsert3)cursor.execute(sqlInsert4)sqlUpdate = "update users set introduce='{0}' where userName='{1}'".format("潇灑的姑娘", "春夢")cursor.execute(sqlUpdate)sqlDelete = "delete from users where userName='{0}'".format("刪除測試")cursor.execute(sqlDelete)# 提交db.commit()sql = "select * from users" # 執行SQLcursor.execute(sql) # 回去返回集合data = cursor.fetchall() # 遍歷集合for item in data:print(data) # 關閉數據庫連接db.close()

​編輯

編輯

通過以上操作,基本的整個數據庫都搞完了,希望能對大家的學習有所價值。


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved