佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > Python接入MySQL實現(xiàn)增刪改查的實戰(zhàn)記錄

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

熱門標(biāo)簽:電話運營中心 企業(yè)做大做強(qiáng) 語音系統(tǒng) 呼叫中心市場需求 百度AI接口 客戶服務(wù) 硅谷的囚徒呼叫中心 Win7旗艦版

前言

我們經(jīng)常需要將大量數(shù)據(jù)保存起來以備后續(xù)使用,數(shù)據(jù)庫是一個很好的解決方案。在眾多數(shù)據(jù)庫中,MySQL數(shù)據(jù)庫算是入門比較簡單、語法比較簡單,同時也比較實用的一個。本文主要介紹了Python接入MySQL實現(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,('夢璃','南天門',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,'夢璃'),(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='夢璃';"""
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實現(xiàn)增刪改查的文章就介紹到這了,更多相關(guān)Python MySQL增刪改查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 解決python mysql insert語句的問題
  • python 在mysql中插入null空值的操作
  • Python+MySQL隨機(jī)試卷及答案生成程序的示例代碼
  • 詳解Python之Scrapy爬蟲教程NBA球員數(shù)據(jù)存放到Mysql數(shù)據(jù)庫
  • Python從MySQL數(shù)據(jù)庫中面抽取試題,生成試卷
  • python3 使用ssh隧道連接mysql的操作
  • Python批量刪除mysql中千萬級大量數(shù)據(jù)的腳本分享
  • python查詢MySQL將數(shù)據(jù)寫入Excel
  • Python操控mysql批量插入數(shù)據(jù)的實現(xiàn)方法
  • Python下的Mysql模塊MySQLdb安裝詳解
  • python中MySQLdb模塊用法實例
  • Python中操作mysql的pymysql模塊詳解
  • MySQL和Python交互的示例

標(biāo)簽:山西 濟(jì)南 海南 安康 長沙 山西 崇左 喀什

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python接入MySQL實現(xiàn)增刪改查的實戰(zhàn)記錄》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    金乡县| 丹棱县| 安平县| 石狮市| 兰州市| 开远市| 陆川县| 宣化县| 井研县| 河津市| 白玉县| 广水市| 南宫市| 将乐县| 富蕴县| 奈曼旗| 鲁山县| 南平市| 板桥市| 康乐县| 闻喜县| 辛集市| 达尔| 开远市| 饶平县| 赣州市| 土默特左旗| 筠连县| 肇庆市| 九台市| 麟游县| 肥城市| 霍州市| 聂荣县| 莆田市| 子长县| 凤城市| 大田县| 赫章县| 乾安县| 凤台县|