国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Python接入MySQL實(shí)現(xiàn)增刪改查的實(shí)戰(zhàn)記錄

瀏覽:5日期:2022-06-26 10:52:31

前言

我們經(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 pymysqldb = 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)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 亚洲精品98久久久久久中文字幕 | 亚洲视频1区 | 2020黄网| 日韩不卡一区二区三区 | 日韩欧美自拍 | 福利社色| 亚洲天堂视频在线观看免费 | 美女扒开腿被男人猛视频 | 日韩久久免费视频 | 亚洲精品国产福利 | 日韩免费高清 | 91亚洲自偷手机在线观看 | 九九视频在线观看视频 | 亚洲精品中文字幕字幕 | 偷拍第1页 | 午夜影院免费入口 | 国产人成精品综合欧美成人 | 国产精品成人久久久久久久 | 2022久久免费精品国产72精品 | 在线a视频网站 | 啪啪一级片 | 国产亚洲一级精品久久 | 亚洲天堂在线视频播放 | 久久久一本精品99久久精品66 | 久久精品高清 | 美女张开腿黄网站免费国产 | 国产精品区一区二区免费 | 日本一级aaaa特黄毛片 | 日本美女视频韩国视频网站免费 | 韩国视频一区 | 国产精品高清一区二区 | 99国产精品高清一区二区二区 | 亚洲 欧美 日韩 在线 中文字幕 | 在线观看国产一区二区三区99 | 网站免费满18成年在线观看 | 久久亚洲网 | 国产一区亚洲 | 日本a级特黄三级三级三级 日本a一级片 | 国产精品免费久久 | 久久99久久99精品 | 欧美黑粗特黄午夜大片 |