国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Python基礎(chǔ)之?dāng)?shù)據(jù)庫(kù)操作

這篇具有很好參考價(jià)值的文章主要介紹了Python基礎(chǔ)之?dāng)?shù)據(jù)庫(kù)操作。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

一、安裝第三方庫(kù)PyMySQL

1、在PyCharm中通過 【File】-【setting】-【Python Interpreter】搜索 PyMySQL進(jìn)行安裝

Python基礎(chǔ)之?dāng)?shù)據(jù)庫(kù)操作,學(xué)習(xí)python,python,開發(fā)語(yǔ)言

2、通過PyCharm中的 Terminal 命令行 輸入: pip install PyMySQL

注:通過pip安裝,可能會(huì)提示需要更新pip,這時(shí)可執(zhí)行:pip install --upgrade pip 進(jìn)行更新pip

二、mysql數(shù)據(jù)庫(kù)查詢(SELECT)

1、pymysql.connect()連接數(shù)據(jù)庫(kù)

import pymysql
connect = pymysql.connect(host='101.37.246.001', # 數(shù)據(jù)庫(kù)地址
                          port= 3306,            # 數(shù)據(jù)庫(kù)端口,mysql一般默認(rèn)為3306
                          user='xxxx',       # 用戶名
                          password='xxxx',   # 登錄密碼
                          database='movie_cat',  # 要連接的數(shù)據(jù)庫(kù)名
                          charset='utf8')        # 數(shù)據(jù)庫(kù)編碼,一般默認(rèn)utf8

?2、使用 cursor( ) 創(chuàng)建游標(biāo)

import pymysql
connect = pymysql.connect(host='101.37.246.001', # 數(shù)據(jù)庫(kù)地址
                          port= 3306,            # 數(shù)據(jù)庫(kù)端口,mysql一般默認(rèn)為3306
                          user='xxxx',       # 用戶名
                          password='xxxx',   # 登錄密碼
                          database='movie_cat',  # 要連接的數(shù)據(jù)庫(kù)名
                          charset='utf8')        # 數(shù)據(jù)庫(kù)編碼,一般默認(rèn)utf8
with connect.cursor() as cursor: # 創(chuàng)建游標(biāo)

3、使用 execute( ) 執(zhí)行sql語(yǔ)句

?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-821314.html

import pymysql
connect = pymysql.connect(host='101.37.246.001', # 數(shù)據(jù)庫(kù)地址
                          port= 3306,            # 數(shù)據(jù)庫(kù)端口,mysql一般默認(rèn)為3306
                          user='xxxx',       # 用戶名
                          password='xxxx',   # 登錄密碼
                          database='movie_cat',  # 要連接的數(shù)據(jù)庫(kù)名
                          charset='utf8')        # 數(shù)據(jù)庫(kù)編碼,一般默認(rèn)utf8
with connect.cursor() as cursor: # 創(chuàng)建游標(biāo) # 創(chuàng)建游標(biāo)
    sql = 'SELECT * FROM movie2 '
    cursor.execute(sql) # 執(zhí)行sql語(yǔ)句

4、執(zhí)行sql語(yǔ)句后,需要調(diào)用 fetchone() 或 fetchall() 方法來(lái)獲得查詢的返回結(jié)果

  • fetchone(): 該方法獲取第一個(gè)查詢結(jié)果集。結(jié)果集是一個(gè)對(duì)象,連續(xù)多次執(zhí)行將依次取得下一條結(jié)果,直到為空;
  • fetchall():?接收全部的返回結(jié)果行
import pymysql
connect = pymysql.connect(host='101.37.246.001', # 數(shù)據(jù)庫(kù)地址
                          port= 3306,            # 數(shù)據(jù)庫(kù)端口,mysql一般默認(rèn)為3306
                          user='xxxx',       # 用戶名
                          password='xxxx',   # 登錄密碼
                          database='movie_cat',  # 要連接的數(shù)據(jù)庫(kù)名
                          charset='utf8')        # 數(shù)據(jù)庫(kù)編碼,一般默認(rèn)utf8
												# cursorclass=pymysql.cursors.DictCursor 設(shè)置游標(biāo)返回結(jié)果為字典
with connect.cursor() as cursor: # 創(chuàng)建游標(biāo)
    sql = 'SELECT * FROM movie2 '
    cursor.execute(sql) # 執(zhí)行sql語(yǔ)句
    data = cursor.fetchall() # 接收全部返回的結(jié)果,返回一個(gè)元祖類型
    print(f"數(shù)據(jù)庫(kù)查詢數(shù)據(jù):{data}")
    print(type(data))
connect.close() # 關(guān)閉數(shù)據(jù)庫(kù)連接

數(shù)據(jù)庫(kù)查詢數(shù)據(jù):((1, 'My Neighbor Totoro', '1988'), (2, 'Dead Poets Society', '1989'), (3, 'A Perfect World', '1993'), (4, 'Leon', '1994'), (5, 'Mahjong', '1996'), (6, 'Swallowtail Butterfly', '1996'), (7, 'King of Comedy', '1999'), (8, 'Devils on the Doorstep', '1999'), (9, 'WALL-E', '2008'), (10, 'The Pork of Music', '2012'), (12, 'huawei', '2020'))
<class 'tuple'>

二、mysql數(shù)據(jù)庫(kù)更新數(shù)據(jù)(UPDATE)?

import pymysql
connect = pymysql.connect(host='101.37.246.001', # 數(shù)據(jù)庫(kù)地址
                          port= 3306,            # 數(shù)據(jù)庫(kù)端口,mysql一般默認(rèn)為3306
                          user='xxxx',       # 用戶名
                          password='xxxx',   # 登錄密碼
                          database='movie_cat',  # 要連接的數(shù)據(jù)庫(kù)名
                          charset='utf8')        # 數(shù)據(jù)庫(kù)編碼,一般默認(rèn)utf8
with connect.cursor() as cursor: # 創(chuàng)建游標(biāo)
    sql = 'UPDATE movie2 SET year = 1998 WHERE id = 1 '
    cursor.execute(sql) # 執(zhí)行sql語(yǔ)句
    connect.commit() # 提交數(shù)據(jù)到數(shù)據(jù)庫(kù)
connect.close() # 關(guān)閉數(shù)據(jù)庫(kù)連接

在cursor( ) 創(chuàng)建游標(biāo)后通過execute( ) 執(zhí)行sql,需要通過connect實(shí)例調(diào)用commit( ) 進(jìn)行數(shù)據(jù)提交

三、mysql數(shù)據(jù)庫(kù)插入數(shù)據(jù)(INSERT)

import pymysql
connect = pymysql.connect(host='101.37.246.001', # 數(shù)據(jù)庫(kù)地址
                          port= 3306,            # 數(shù)據(jù)庫(kù)端口,mysql一般默認(rèn)為3306
                          user='xxxx',       # 用戶名
                          password='xxxx',   # 登錄密碼
                          database='movie_cat',  # 要連接的數(shù)據(jù)庫(kù)名
                          charset='utf8')        # 數(shù)據(jù)庫(kù)編碼,一般默認(rèn)utf8
with connect.cursor() as cursor: # 創(chuàng)建游標(biāo)
    sql = "INSERT INTO movie2(title, year) VALUES ('firstday', '2021');"
    cursor.execute(sql) # 執(zhí)行sql語(yǔ)句
    connect.commit() # 提交數(shù)據(jù)到數(shù)據(jù)庫(kù)
connect.close() # 關(guān)閉數(shù)據(jù)庫(kù)連接

?四、mysql數(shù)據(jù)庫(kù)刪除數(shù)據(jù)(DELETE)

import pymysql
connect = pymysql.connect(host='101.37.246.001', # 數(shù)據(jù)庫(kù)地址
                          port= 3306,            # 數(shù)據(jù)庫(kù)端口,mysql一般默認(rèn)為3306
                          user='xxxx',       # 用戶名
                          password='xxxx',   # 登錄密碼
                          database='movie_cat',  # 要連接的數(shù)據(jù)庫(kù)名
                          charset='utf8')        # 數(shù)據(jù)庫(kù)編碼,一般默認(rèn)utf8
with connect.cursor() as cursor: # 創(chuàng)建游標(biāo)
    sql = "DELETE FROM movie2 WHERE id = 13;"
    cursor.execute(sql) # 執(zhí)行sql語(yǔ)句
    connect.commit() # 提交數(shù)據(jù)到數(shù)據(jù)庫(kù)
connect.close() # 關(guān)閉數(shù)據(jù)庫(kù)連接

注:insert/delete/update后都需要調(diào)用commit( )提交數(shù)據(jù)到數(shù)據(jù)庫(kù),完成事務(wù)提交

封裝一個(gè)數(shù)據(jù)庫(kù)操作類

class ConnectDB:

    config = {
          "host":'47.103.126.208',
          "user":'siyuan',
          "password":'123456',
          "database":'mall',
          "charset":'utf8',
          #"cursorclass":"pymysql.cursors.DictCursor"

    }

    def __init__(self):
        self.connect = pymysql.connect(**self.config)


    def select_datas(self, sql):
        with self.connect.cursor(pymysql.cursors.DictCursor) as cur:
            cur.execute(sql)
            data = cur.fetchall()
            print(data)
            print(type(data))
        return data

    def charge_datas(self, sql):
        pass

    def connect_close(self):
        self.connect.close()

    def __call__(self, act=None, sql=None, connect=True):
        if connect:
            if act == 'select':
                datas = self.select_datas(sql)
                return datas
            elif act in ['update', 'insert', 'delete']:
                self.charge_datas(sql)
                return self
        else:
            self.connect_close()

if __name__ == '__main__':
    connect_db = ConnectDB()
    sql = "SELECT * FROM ls_user WHERE nickname LIKE '%思源%';"
    data1 = connect_db('select', sql)
    data2 = connect_db('select', sql)
    connect_db(connect=False)

?

?

?

?

?

?

?

?

?

?

到了這里,關(guān)于Python基礎(chǔ)之?dāng)?shù)據(jù)庫(kù)操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • python使用flask實(shí)現(xiàn)前后端分離&通過前端修改數(shù)據(jù)庫(kù)數(shù)據(jù)【全棧開發(fā)基礎(chǔ)】

    python使用flask實(shí)現(xiàn)前后端分離&通過前端修改數(shù)據(jù)庫(kù)數(shù)據(jù)【全棧開發(fā)基礎(chǔ)】

    完整代碼放到了最后,時(shí)間緊張的話直接拉到最后或點(diǎn)擊目錄【?? 完整代碼】看完整代碼 這里先提一下,我們運(yùn)行后端代碼之前需要先建立一個(gè)名字為 python 的數(shù)據(jù)庫(kù),而后在該數(shù)據(jù)庫(kù)下創(chuàng)建表 userinfo ,因?yàn)榭吹接械呐笥押蠖舜a拿過去后會(huì)運(yùn)行不起來(lái)或者就是直接報(bào)錯(cuò)了

    2023年04月09日
    瀏覽(23)
  • 一篇文章打好SQL基礎(chǔ),熟悉數(shù)據(jù)庫(kù)的基礎(chǔ)操作和方法,以及安裝MySQL軟件包和Python操作MySQL基礎(chǔ)使用

    一篇文章打好SQL基礎(chǔ),熟悉數(shù)據(jù)庫(kù)的基礎(chǔ)操作和方法,以及安裝MySQL軟件包和Python操作MySQL基礎(chǔ)使用

    SQL的全稱:Structured Query Language,結(jié)構(gòu)化查詢語(yǔ)言,用于 訪問和處理數(shù)據(jù)庫(kù)的標(biāo)準(zhǔn)計(jì)算機(jī)語(yǔ)言 。 SQL語(yǔ)言1974年有Boyce和Chamberlin提出的,并且首先在IBM公司研制的關(guān)系數(shù)據(jù)庫(kù)系統(tǒng)SystemR上實(shí)現(xiàn)。 經(jīng)過多年發(fā)展,SQL已經(jīng)成為數(shù)據(jù)庫(kù)領(lǐng)域同意的數(shù)據(jù)操作標(biāo)準(zhǔn)語(yǔ)言,可以說(shuō)幾乎市面上所

    2024年02月08日
    瀏覽(104)
  • 【100天精通python】Day44:python網(wǎng)絡(luò)爬蟲開發(fā)_爬蟲基礎(chǔ)(爬蟲數(shù)據(jù)存儲(chǔ):基本文件存儲(chǔ),MySQL,NoSQL:MongDB,Redis 數(shù)據(jù)庫(kù)存儲(chǔ)+實(shí)戰(zhàn)代碼)

    目錄 1 數(shù)據(jù)存儲(chǔ) 1.1 爬蟲存儲(chǔ):基本文件存儲(chǔ) 1.2 爬蟲存儲(chǔ):使用MySQL 數(shù)據(jù)庫(kù) 1.3 爬蟲 NoSQL 數(shù)據(jù)庫(kù)使用 1.3.1 MongoDB 簡(jiǎn)介

    2024年02月11日
    瀏覽(24)
  • python基礎(chǔ)學(xué)習(xí)6【DatatimeIndex與PeriodIndex函數(shù)+Timedelta類+連接數(shù)據(jù)庫(kù)+agg()函數(shù)和aggregate()函數(shù)】

    python基礎(chǔ)學(xué)習(xí)6【DatatimeIndex與PeriodIndex函數(shù)+Timedelta類+連接數(shù)據(jù)庫(kù)+agg()函數(shù)和aggregate()函數(shù)】

    轉(zhuǎn)換與處理時(shí)間序列數(shù)據(jù)?? 轉(zhuǎn)換字符串時(shí)間為標(biāo)準(zhǔn)時(shí)間: Timestamp類型:最基礎(chǔ)最常用。 如果超出時(shí)間戳最大值,最小值,時(shí)間戳存儲(chǔ)可能不成功 : DatatimeIndex與PeriodIndex函數(shù)【其實(shí)俺暫時(shí)不懂這倆能干啥嗚嗚嗚】 Timedelta類 不僅能夠使用正數(shù),也能使用負(fù)數(shù); 可以輕松實(shí)現(xiàn)在

    2024年02月10日
    瀏覽(22)
  • Python——數(shù)據(jù)庫(kù)操作

    Python——數(shù)據(jù)庫(kù)操作

    目錄 (1)安裝Flask-SQLAlchemy (2)使用Flask-SQLAlchemy操作數(shù)據(jù)庫(kù) (3)連接數(shù)據(jù)庫(kù) ??創(chuàng)建數(shù)據(jù)表 ?增加數(shù)據(jù) ?查詢數(shù)據(jù) ??更新數(shù)據(jù) ?刪除數(shù)據(jù) Flask-SQLAlchemy是Flask中用于操作關(guān)系型數(shù)據(jù)庫(kù)的擴(kuò)展包 ,該擴(kuò)展包內(nèi)部集成了SQLAlchemy,并簡(jiǎn)化了在Flask程序中使用SQLAlchemy操作數(shù)據(jù)

    2024年02月04日
    瀏覽(27)
  • python操作數(shù)據(jù)庫(kù)

    首先安裝數(shù)據(jù)插件 數(shù)據(jù)庫(kù)的插入

    2024年02月13日
    瀏覽(26)
  • Python --數(shù)據(jù)庫(kù)操作

    目錄 1, mysql 1-1, mysql驅(qū)動(dòng) 1-2, 連接mysql 1-3, 執(zhí)行sql語(yǔ)句 1-4, 數(shù)據(jù)表操作 1-4-1, 創(chuàng)建數(shù)據(jù)表 1-4-2, 查詢數(shù)據(jù)表 1-4-3, 修改數(shù)據(jù)表 1-4-4, 刪除數(shù)據(jù)表 1-5, 修改數(shù)據(jù)表內(nèi)容 1-5-1, 插入數(shù)據(jù) 1-5-2, 查詢數(shù)據(jù) 1-5-3, 獲取結(jié)果集 1-5-4, 更新數(shù)據(jù) 1-5-5, 刪除數(shù)據(jù) 1-6, 斷開mys

    2024年02月11日
    瀏覽(18)
  • Python之?dāng)?shù)據(jù)庫(kù)操作(連接數(shù)據(jù)庫(kù),增刪改查操作,易錯(cuò)點(diǎn)理解)

    Python之?dāng)?shù)據(jù)庫(kù)操作(連接數(shù)據(jù)庫(kù),增刪改查操作,易錯(cuò)點(diǎn)理解)

    文章目錄 前言 一、Python之?dāng)?shù)據(jù)庫(kù)操作 二、 pymysql 安裝 三、pymysql 包引入 ?連接數(shù)據(jù)庫(kù) 創(chuàng)建游標(biāo) 執(zhí)行sql數(shù)據(jù) - 增刪改查 要獲取查詢結(jié)果數(shù)據(jù) 關(guān)閉游標(biāo),關(guān)閉數(shù)據(jù)庫(kù)連接 總結(jié) 記錄:Python操作數(shù)據(jù)庫(kù)的步驟,不容易理解的地方。 學(xué)習(xí)地址: python與各大數(shù)據(jù)庫(kù)的連接: http:/

    2023年04月16日
    瀏覽(57)
  • Python——操作MySQL數(shù)據(jù)庫(kù)

    Python——操作MySQL數(shù)據(jù)庫(kù)

    ??學(xué)習(xí)永無(wú)止境,記得每天學(xué)習(xí)新的知識(shí)??! 在很多業(yè)務(wù)場(chǎng)景中,我們或多或少都要對(duì)數(shù)據(jù)庫(kù)上的數(shù)據(jù)進(jìn)行的一系列操作, 包括讀取數(shù)據(jù)、寫數(shù)據(jù)、更新或修改數(shù)據(jù)、刪除數(shù)據(jù) 等。這些操作可以通過編寫SQL語(yǔ)句來(lái)實(shí)現(xiàn),也可以通過使用數(shù)據(jù)庫(kù)管理系統(tǒng)提供的API接口來(lái)實(shí)現(xiàn)。

    2024年02月09日
    瀏覽(46)
  • python與數(shù)據(jù)庫(kù)連接操作

    python與數(shù)據(jù)庫(kù)連接操作

    下載命令: pip install PyMySQL 一般我們會(huì)使用鏡像下載,這樣會(huì)比較快,比如清華鏡像: pip install pymysql -i https://pypi.tuna.tsinghua.edu.cn/simple 連接數(shù)據(jù)庫(kù)之前,我們需要知道自己需要連接數(shù)據(jù)庫(kù)的用戶名,密碼,數(shù)據(jù)庫(kù)名等信息 步驟: 連接connect() 創(chuàng)建cursor()對(duì)象 使用excute()執(zhí)行S

    2024年02月08日
    瀏覽(27)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包