Python接入MySQL實(shí)現(xiàn)增刪改查的實(shí)戰(zhàn)記錄
前言
我們經(jīng)常需要將大量數(shù)據(jù)保存起來以備后續(xù)使用,數(shù)據(jù)庫是一個(gè)很好的解決方案。在眾多數(shù)據(jù)庫中,MySQL數(shù)據(jù)庫算是入門比較簡(jiǎn)單、語法比較簡(jiǎn)單,同時(shí)也比較實(shí)用的一個(gè)。本文主要介紹了Python接入MySQL實(shí)現(xiàn)增刪改查的相關(guān)內(nèi)容,下面話不多說,一起來看看詳細(xì)的介紹吧
打開數(shù)據(jù)庫連接,創(chuàng)建數(shù)據(jù)庫和表
基本語法如下:
execute(query, args=None) # query為字符串類型的sql語句 # args:可選的序列或映射,用于query的參數(shù)值。 # 如果args為序列,query中必須使用%s做占位符; # 如果args為映射,query中必須使用%(key)s做占位符
案例:數(shù)據(jù)庫名learning,表名houses,字段name house_location purchasing_year
import pymysql db = pymysql.connect('localhost', 'root', "password") # 打開數(shù)據(jù)庫連接,password替換為本機(jī)數(shù)據(jù)庫密碼 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ù)庫連接
cursor.close() # 關(guān)閉游標(biāo) db.commit() db.close() # 關(guān)閉數(shù)據(jù)庫連接 print('sql執(zhí)行成功')
總結(jié)
到此這篇關(guān)于Python接入MySQL實(shí)現(xiàn)增刪改查的文章就介紹到這了,更多相關(guān)Python MySQL增刪改查內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)簡(jiǎn)單的HttpServer服務(wù)器示例
本篇文章主要介紹了Python實(shí)現(xiàn)簡(jiǎn)單的HttpServer服務(wù)器示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09Python完整實(shí)現(xiàn)俄羅斯方塊游戲全解
俄羅斯方塊是一個(gè)最初由阿列克謝帕吉特諾夫在蘇聯(lián)設(shè)計(jì)和編程的益智類視頻游戲。本文將利用python實(shí)現(xiàn)這一經(jīng)典的小游戲,需要的可以參考一下2022-03-03Python bsddb模塊操作Berkeley DB數(shù)據(jù)庫介紹
這篇文章主要介紹了Python bsddb模塊操作Berkeley DB數(shù)據(jù)庫介紹,這里簡(jiǎn)單介紹一些關(guān)于bsddb的使用方法,需要的朋友可以參考下2015-04-04pytorch 彩色圖像轉(zhuǎn)灰度圖像實(shí)例
今天小編就為大家分享一篇pytorch 彩色圖像轉(zhuǎn)灰度圖像實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01關(guān)于python變量的引用以及在底層存儲(chǔ)原理
Python的變量,簡(jiǎn)單來說有數(shù)值型,布爾型,字符串類型,列表,元組,字典等6大類。那么不同變量類型在底層是如何存儲(chǔ)的,關(guān)系到變量的引用,能否正確的掌握變量的相關(guān)操作?接下來小編就來為大家講解python變量的引用以及在底層存儲(chǔ)原理,需要的朋友可以參考一下2021-09-09