最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Python對MySQL進行數(shù)據(jù)分析

 更新時間:2026年02月24日 09:05:29   作者:hhzz  
這篇文章主要介紹了如何在Python中搭建環(huán)境、連接MySQL數(shù)據(jù)庫、執(zhí)行基本的數(shù)據(jù)庫操作,并使用Python實現(xiàn)統(tǒng)計昨天用戶增長量的腳本,需要的朋友可以參考下

1,前期準備

1.1,Python環(huán)境搭建

  1. 執(zhí)行如下命令,使用yum install命令安裝Python的依賴包:
  1. 下載Python安裝包,顯示100%即為下載成功:
wget 'https://clouder-labfileapp.oss-cn-hangzhou.aliyuncs.com/database/Python-3.9.10.tar.xz'
  1. 編譯安裝,請依次執(zhí)行以下命令
# 解壓安裝包
tar -xvJf Python-3.9.10.tar.xz

# 進入安裝包目錄
cd Python-3.9.10

# 配置安裝目錄
./configure prefix=/usr/local/python3

# 編譯,make工程管理器就是一個“自動編譯管理器”,這里的“自動”是指它能夠根據(jù)文件時間戳自動發(fā)現(xiàn)更新過的文件而減少工作量,同時,他通過讀入makefile文件的內(nèi)容來執(zhí)行大量編譯工作。
make
#說明:此過程耗時較長,請您耐心等待。
#編譯安裝
make install

# 給Python和包管理器創(chuàng)建軟連接
ln -fs /usr/local/python3/bin/python3 /usr/bin/python3
ln -fs /usr/local/python3/bin/pip3 /usr/bin/pip3

  1. 執(zhí)行以下命令,安裝Python虛擬環(huán)境。
#創(chuàng)建目錄
mkdir -p /apps && cd $_

#創(chuàng)建虛擬環(huán)境
python3 -m venv venv
  1. 執(zhí)行以下命令,應(yīng)用Python虛擬環(huán)境。
# 通常情況下一個應(yīng)用服務(wù)一個虛擬環(huán)境,每個虛擬環(huán)境可以安裝不同版本的不同模塊
source /apps/venv/bin/activate
  1. 執(zhí)行以下命令,配置pip源。
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip config set install.trusted-host mirrors.aliyun.com
  1. 執(zhí)行以下命令,安裝python模塊
# 1. 升級 pip
pip install --upgrade pip

# 2. 升級 setuptools 和 wheel
pip install --upgrade setuptools wheel

# 3. 安裝 pandas(指定稍舊但穩(wěn)定的版本)
pip install pandas==2.2.3

pip install pymysql sqlalchemy matplotlib pyyaml
  1. 執(zhí)行以下命令,驗證Python模塊安裝成功。
pip list

  1. 執(zhí)行以下命令,驗證Python安裝成功。
python --version
  1. 執(zhí)行以下命令,安裝MySQL。
yum  install mysql -y

1.2,安裝連接MySQL的Python模塊:

pip install pymysql

安裝完成以后,我們需要在服務(wù)器上創(chuàng)建一個Python文件,用于編寫代碼內(nèi)容,代碼里面的內(nèi)容包括:連接MySQL,創(chuàng)建游標,通過游標執(zhí)行SQL。

Python腳本代碼:

#!/usr/bin/python
import pymysql


# 1、打開數(shù)據(jù)庫連接
db = pymysql.connect(host='localhost',user='root',password='test123',database='goods')

# 2、使用 cursor() 方法創(chuàng)建一個游標對象 cursor
cursor = db.cursor()
 
# 3、使用 execute()  方法執(zhí)行 SQL  
cursor.execute("SELECT VERSION()")

# 使用 fetchone() 方法獲取單條數(shù)據(jù).
data = cursor.fetchone()

# 打印結(jié)果
print(f"Database version : {data}")

# 4、關(guān)閉數(shù)據(jù)庫連接
db.close()

2,Python操作數(shù)據(jù)庫

2.1,Python連接MySQL數(shù)據(jù)庫

我們借助Python的pymysql模塊連接MSQL數(shù)據(jù)庫,pymysql是一個用于Python編程的第三方模塊,用于連接和操作MySQL數(shù)據(jù)庫。它提供了一個簡單而強大的接口,使開發(fā)者能夠輕松地在Python程序中執(zhí)行各種數(shù)據(jù)庫操作,如查詢、插入、更新和刪除數(shù)據(jù)等。

導(dǎo)入模塊

import pymysql

打開數(shù)據(jù)庫連接

注意:這里已經(jīng)假定存在數(shù)據(jù)庫aliyun,db指定了連接的數(shù)據(jù)庫,當(dāng)然這個參數(shù)也可以沒有

#打開數(shù)據(jù)庫連接
#注意:這里已經(jīng)假定存在數(shù)據(jù)庫aliyun,db指定了連接的數(shù)據(jù)庫,當(dāng)然這個參數(shù)也可以沒有
db = pymysql.connect(
host='127.0.0.1',  # mysql服務(wù)器地址
port=3306,         # mysql服務(wù)器端口號
user='root',       # 用戶名
passwd='aliyun',     # 密碼
db='aliyun',       # 數(shù)據(jù)庫名稱
charset='utf8')    # 連接編碼,存在中文的時候,連接需要添加charset='utf8',否則中文顯示亂碼

創(chuàng)建游標對象cursor

#使用cursor方法創(chuàng)建一個游標
cursor = db.cursor()

2.2,數(shù)據(jù)庫基本操作

使用execute()方法來實現(xiàn)對數(shù)據(jù)庫的基本操作。
查詢數(shù)據(jù)庫版本

#查詢數(shù)據(jù)庫版本
cursor.execute("select version()")
data = cursor.fetchone()
print(f"Database version : {data}")

創(chuàng)建數(shù)據(jù)庫

#創(chuàng)建數(shù)據(jù)庫aliyun
cursor.execute("drop database if exists aliyun")  #如果數(shù)據(jù)庫已經(jīng)存在,

那么刪除后重新創(chuàng)建

sql = "create database aliyun"
cursor.execute(sql)

創(chuàng)建數(shù)據(jù)表

#創(chuàng)建數(shù)據(jù)庫表
cursor.execute("drop table if exists goods")  #如果數(shù)據(jù)表已經(jīng)存在,那么刪除后重新創(chuàng)建
sql = """
CREATE TABLE goods (
FIRST_NAME CHAR(20) NOT NULL,
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT )
"""
cursor.execute(sql)

查詢操作

#查詢數(shù)據(jù)表數(shù)據(jù)
sql = "select * from goods"
cursor.execute(sql)
data = cursor.fetchone()
print(data)

插入操作

#插入數(shù)據(jù)
sql = "insert into goods values ('小','明',20,'W',5000)"
cursor.execute(sql)
db.commit()
#查看插入后的結(jié)果
sql = "select * from goods"
cursor.execute(sql)
data = cursor.fetchone()
print(data)

指定條件查詢數(shù)據(jù)

#指定條件查詢數(shù)據(jù)表數(shù)據(jù)
sql = " select * from goods where income > '%d' " % (1000)
cursor.execute(sql)
data = cursor.fetchone()
print(data)

更新操作

#更新數(shù)據(jù)庫
sql = " update goods set age = age+1 where sex = '%c' " % ('W')
cursor.execute(sql)
db.commit()
#查看更新后的結(jié)果
sql = "select * from goods"
cursor.execute(sql)
data = cursor.fetchone()
print(data)

刪除操作

#刪除數(shù)據(jù)
sql = " delete from goods where age > '%d' " % (30)
cursor.execute(sql)
db.commit()
#查看更新后的結(jié)果
sql = "select * from goods"
cursor.execute(sql)
data = cursor.fetchone()
print(data)

關(guān)閉數(shù)據(jù)庫連接

db.close()

3,用Python實現(xiàn)統(tǒng)計昨天用戶增長量

腳本內(nèi)容如下:

#!/usr/bin/python3
from datetime import datetime, timedelta, date
from dateutil.relativedelta import relativedelta
import pymysql

# 今天、昨天日期
today = date.today()
yesterday = today - relativedelta(days=1)

# 將日期格式化為SQL中需要的格式
sf_today = datetime.strftime(today,"%Y-%m-%d")
sf_yesterday = datetime.strftime(yesterday,"%Y-%m-%d")

# 統(tǒng)計昨天、前天用戶數(shù)量的SQL
sql_usercount_yesterday = f"SELECT COUNT(*) FROM `users` WHERE register_time<'{sf_today}'"
sql_usercount_yesterdaybefore = f"SELECT COUNT(*) FROM `users` WHERE register_time<'{sf_yesterday}'"

# 建立連接
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd='test13579', db='goods',charset="utf8")

# 創(chuàng)建游標
cursor = conn.cursor()

# 執(zhí)行SQL
cursor.execute(sql_usercount_yesterday)
usercount_yesterday = cursor.fetchone()

cursor.execute(sql_usercount_yesterdaybefore)
usercount_yesterdaybefore  = cursor.fetchone()

# SQL執(zhí)行結(jié)果運算得到昨天用戶增長數(shù)
print("昨天用戶新增人數(shù): ",usercount_yesterday[0] - usercount_yesterdaybefore[0])


# 關(guān)閉連接
conn.close()

注意:dateutil需要安裝python-dateuti模塊(pip install python-dateuti)。

到此這篇關(guān)于使用Python對MySQL進行數(shù)據(jù)分析的文章就介紹到這了,更多相關(guān)Python將Word轉(zhuǎn)PDF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

瓮安县| 育儿| 卢氏县| 法库县| 山西省| 安阳市| 武威市| 鄂伦春自治旗| 东山县| 南漳县| 虞城县| 山东省| 凯里市| 彭州市| 桦南县| 正阳县| 两当县| 华宁县| 鄂托克旗| 富蕴县| 定远县| 师宗县| 肇源县| 大兴区| 磐石市| 贺州市| 通州区| 河北省| 静乐县| 永嘉县| 嘉鱼县| 全州县| 普安县| 苏尼特右旗| 青阳县| 临猗县| 阳江市| 清新县| 青河县| 长汀县| 江口县|