小編給大家分享一下Python接入MySQL如何實(shí)現(xiàn)增刪改查,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
10年積累的成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有柳南免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。前言
我們經(jīng)常需要將大量數(shù)據(jù)保存起來(lái)以備后續(xù)使用,數(shù)據(jù)庫(kù)是一個(gè)很好的解決方案。在眾多數(shù)據(jù)庫(kù)中,MySQL數(shù)據(jù)庫(kù)算是入門比較簡(jiǎn)單、語(yǔ)法比較簡(jiǎn)單,同時(shí)也比較實(shí)用的一個(gè)。本文主要介紹了Python接入MySQL實(shí)現(xiàn)增刪改查的相關(guān)內(nèi)容,下面話不多說(shuō),一起來(lái)看看詳細(xì)的介紹吧
打開(kāi)數(shù)據(jù)庫(kù)連接,創(chuàng)建數(shù)據(jù)庫(kù)和表
基本語(yǔ)法如下:
execute(query, args=None) # query為字符串類型的sql語(yǔ)句 # args:可選的序列或映射,用于query的參數(shù)值。 # 如果args為序列,query中必須使用%s做占位符; # 如果args為映射,query中必須使用%(key)s做占位符
案例:數(shù)據(jù)庫(kù)名learning,表名houses,字段name house_location purchasing_year
import pymysql db = pymysql.connect('localhost', 'root', "password") # 打開(kāi)數(shù)據(jù)庫(kù)連接,password替換為本機(jī)數(shù)據(jù)庫(kù)密碼 cursor = db.cursor() cursor.execute('drop database learning;') cursor.execute('create database learning;') cursor.execute('use learning') sql_create = """create table houses (name VARCHAR(100) NOT NULL, house_location VARCHAR(100) NOT NULL, purchasing_year VARCHAR(100) NOT NULL);""" cursor.execute(sql_create)
插入
# 插入 sql_insert = """insert into houses values(%s,%s,%s);""" cursor.execute(sql_insert,('夢(mèng)璃','南天門',1995)) # 插入單條數(shù)據(jù) cursor.executemany(sql_insert,[('紫英','蜀山',1996),('天河','石沉',1997),('菱紗','溪洞',1998)]) # 插入多條數(shù)據(jù)
查詢
sql_select = """select * from houses""" # 單條查詢 cursor.execute(sql_select) while 1: result = cursor.fetchone() if result is None: # 取完所有結(jié)果 break print(result) # 多條查詢,取3條數(shù)據(jù) cursor.execute(sql_select) Result = cursor.fetchmany(3) for res in Result: print(res) # 多條查詢,取所有數(shù)據(jù) cursor.execute(sql_select) Result = cursor.fetchall() for res in Result: print(res)
更新
# 更新一條數(shù)據(jù) sql_update = """update houses set purchasing_year=2000 where name='菱紗';""" cursor.execute(sql_update) cursor.execute(sql_select) Result = cursor.fetchall() for res in Result: print(res) # 更新多條數(shù)據(jù) sql_update = """update houses set purchasing_year=%s where name=%s;""" cursor.executemany(sql_update,[(2018,'夢(mèng)璃'),(2019,'紫英')]) cursor.execute(sql_select) Result = cursor.fetchall() for res in Result: print(res) # 回滾事務(wù) db.rollback() cursor.execute(sql_select) Result = cursor.fetchall() for res in Result: print(res)
刪除
# 刪除1條數(shù)據(jù) sql_delete = """delete from houses where name='夢(mèng)璃';""" cursor.execute(sql_delete) cursor.execute(sql_select) Result = cursor.fetchall() for res in Result: print(res) # 刪除多條數(shù)據(jù) sql_delete = """delete from houses where name=%s;""" cursor.executemany(sql_delete,[('天河'),('紫英')]) cursor.execute(sql_select) Result = cursor.fetchall() for res in Result: print(res)
關(guān)閉游標(biāo),關(guān)閉數(shù)據(jù)庫(kù)連接
cursor.close() # 關(guān)閉游標(biāo) db.commit() db.close() # 關(guān)閉數(shù)據(jù)庫(kù)連接 print('sql執(zhí)行成功')
看完了這篇文章,相信你對(duì)“Python接入MySQL如何實(shí)現(xiàn)增刪改查”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)站名稱:Python接入MySQL如何實(shí)現(xiàn)增刪改查-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)URL:http://bm7419.com/article24/dgdice.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、自適應(yīng)網(wǎng)站、品牌網(wǎng)站建設(shè)、網(wǎng)站收錄、網(wǎng)站設(shè)計(jì)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)