欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python使用pymysql模塊操作MySQL

 更新時(shí)間:2021年06月15日 17:31:20   作者:頭痛的可達(dá)鴨Z  
本文講述了python操作mysql基礎(chǔ)實(shí)例展示,包含pymysql的使用,tkinter的使用,感興趣的朋友可以參考下

實(shí)例一:插入數(shù)據(jù)

import pymysql
import tkinter as tk

conn = pymysql.connect(host='localhost', user='root', passwd='root', db='okzl', charset='utf8')

master = tk.Tk()
master.title("插入供應(yīng)商信息")
master.geometry('350x300')
tk.Label(master, text='cName').place(x=30,y=10)
tk.Label(master, text='address').place(x=30,y=40)
tk.Label(master, text='linkman').place(x=30,y=70)
tk.Label(master, text='linkPhone').place(x=30,y=100)
tk.Label(master, text='credit').place(x=30,y=130)
tk.Label(master, text='remark').place(x=30,y=160)
in1=tk.Entry(master, width=30).place(x=100,y=10)
in2=tk.Entry(master, width=30).place(x=100,y=40)
in3=tk.Entry(master, width=30).place(x=100,y=70)
in3=tk.Entry(master, width=30).place(x=100,y=100)
in3=tk.Entry(master, width=30).place(x=100,y=130)
in3=tk.Entry(master, width=30).place(x=100,y=160)

def insert():
    cur = conn.cursor()  # 伸出手
    sql1 = "insert into pro(cName,address,linkman,linkPhone,credit,remark) values(%s,%s,%s,%s,%s,%s)"
    temp2 = ( )
    cur.execute(sql1, temp2)
    conn.commit()
    cur.close()

tk.Button(master,text='插入',width=8,command=insert).place(x=140,y=220)

master.mainloop()
conn.close()

成功插入數(shù)據(jù)

實(shí)例二:獲取某個(gè)表全部數(shù)據(jù)

import pymysql

conn = pymysql.connect(host='localhost', user='root', passwd='root', db='okzl', charset='utf8')
cur = conn.cursor()

cur.execute('select * from pro')
data = cur.fetchall()

cur.close()
print(data)
conn.close()

實(shí)例三:根據(jù)cName模糊搜索

import pymysql
import tkinter as tk

conn = pymysql.connect(host='localhost', user='root', passwd='root', db='okzl', charset='utf8')  # 連接數(shù)據(jù)庫(kù)

master = tk.Tk()
master.title("搜索某客戶信息")
master.geometry('350x300')

e = tk.Entry(master)
e.pack(padx=20, pady=20)


def tosearch():
    cur = conn.cursor()
    temp2 = (e.get(), "%" + e.get() + "%")
    cur.execute("select * from pro where cName like %s or cName like %s ", temp2)
    data = cur.fetchall()
    cur.close()
    print(data)


tk.Button(master, text='搜索', width=8, command=tosearch).pack(padx=20, pady=50)

master.mainloop()

conn.close()

實(shí)例四:修改數(shù)據(jù)

根據(jù)數(shù)據(jù)庫(kù)自動(dòng)給數(shù)據(jù)生成的id來(lái)確認(rèn)目標(biāo)和修改數(shù)據(jù)

import pymysql
import tkinter as tk

conn = pymysql.connect(host='localhost', user='root', passwd='root', db='okzl', charset='utf8')

master = tk.Tk()
master.title("修改供應(yīng)商信息")
master.geometry('350x300')
tk.Label(master, text='cName').place(x=30,y=10)
tk.Label(master, text='address').place(x=30,y=40)
tk.Label(master, text='linkman').place(x=30,y=70)
tk.Label(master, text='linkPhone').place(x=30,y=100)
tk.Label(master, text='credit').place(x=30,y=130)
tk.Label(master, text='remark').place(x=30,y=160)
tk.Label(master, text='目標(biāo)id').place(x=30,y=190)
in1=tk.Entry(master, width=30)
in1.place(x=100,y=10)
in2=tk.Entry(master, width=30)
in2.place(x=100,y=40)
in3=tk.Entry(master, width=30)
in3.place(x=100,y=70)
in4=tk.Entry(master, width=30)
in4.place(x=100,y=100)
in5=tk.Entry(master, width=30)
in5.place(x=100,y=130)
in6=tk.Entry(master, width=30)
in6.place(x=100,y=160)
in7=tk.Entry(master, width=30)
in7.place(x=100,y=190)

def update():
    cur = conn.cursor()  # 伸出手
    sql1 = "update pro set cName=%s, address=%s,linkman=%s,linkPhone=%s,credit=%s,remark=%s where id=%s"
    temp2 = (in1.get(),in2.get(),in3.get(),in4.get(),in5.get(),in6.get(),in7.get())
    cur.execute(sql1, temp2)
    conn.commit()
    cur.close()


tk.Button(master,text='確認(rèn)修改',width=8,command=update).place(x=140,y=220)

master.mainloop()
conn.close()

實(shí)例五:刪除數(shù)據(jù)

這里是根據(jù)id刪除

sql1 = "delete from pro where id=%s"
temp1 = str(n)
cur.execute(sql1, temp1)
conn.commit()
cur.close()

上述實(shí)例均為基礎(chǔ)實(shí)現(xiàn)操作舉例,實(shí)際操作中可根據(jù)需求更改程序和sql語(yǔ)句實(shí)現(xiàn)目標(biāo)效果

以上就是python使用pymysql模塊操作MySQL的詳細(xì)內(nèi)容,更多關(guān)于python 用pymysql操作MySQL的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python異常處理操作實(shí)例詳解

    Python異常處理操作實(shí)例詳解

    這篇文章主要介紹了Python異常處理操作,結(jié)合實(shí)例形式分析了Python異常處理的相關(guān)原理、操作語(yǔ)句與使用技巧,需要的朋友可以參考下
    2018-05-05
  • Python實(shí)現(xiàn)求數(shù)列和的方法示例

    Python實(shí)現(xiàn)求數(shù)列和的方法示例

    這篇文章主要介紹了Python實(shí)現(xiàn)求數(shù)列和的方法,涉及Python數(shù)值運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • Flask 使用工廠模式

    Flask 使用工廠模式

    本文章向大家介紹flask工廠模式,主要包括flask工廠模式使用實(shí)例、應(yīng)用技巧、基本知識(shí)點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值,需要的朋友可以參考一下

    2021-11-11
  • Python生成截圖選餐GIF動(dòng)畫(huà)

    Python生成截圖選餐GIF動(dòng)畫(huà)

    本篇文章主要介紹了Python生成截圖選餐GIF動(dòng)畫(huà),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • 基于Python制作ASCII碼轉(zhuǎn)換器

    基于Python制作ASCII碼轉(zhuǎn)換器

    ASCII碼是基于拉丁字母的一套電腦編碼系統(tǒng),主要用于顯示現(xiàn)代英語(yǔ)和其他西歐語(yǔ)言。本文將利用Python制作一個(gè)ASCII碼轉(zhuǎn)換器,感興趣的可以動(dòng)手試一試
    2022-02-02
  • python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5控件數(shù)據(jù)拖曳Drag與Drop詳細(xì)使用方法與實(shí)例

    python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5控件數(shù)據(jù)拖曳Drag與Drop詳細(xì)使用方法與實(shí)例

    這篇文章主要介紹了python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5控件數(shù)據(jù)拖曳Drag與Drop詳細(xì)使用方法與實(shí)例,需要的朋友可以參考下
    2020-02-02
  • 推薦技術(shù)人員一款Python開(kāi)源庫(kù)(造數(shù)據(jù)神器)

    推薦技術(shù)人員一款Python開(kāi)源庫(kù)(造數(shù)據(jù)神器)

    今天小編給大家推薦一款Python開(kāi)源庫(kù),技術(shù)人必備的造數(shù)據(jù)神器!非常不錯(cuò),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2020-07-07
  • Pygame?精準(zhǔn)檢測(cè)圖像碰撞的問(wèn)題

    Pygame?精準(zhǔn)檢測(cè)圖像碰撞的問(wèn)題

    這篇文章主要介紹了Pygame?精準(zhǔn)檢測(cè)圖像碰撞,在用Pygame寫(xiě)游戲的時(shí)候,有人可能會(huì)遇到兩個(gè)Rect對(duì)象碰撞但是對(duì)象之間還有空間間隔的問(wèn)題,這里,將教大家用一種方法精準(zhǔn)地檢測(cè)圖像碰撞,需要的朋友可以參考下
    2022-06-06
  • python中shape[0]與shape[1]的說(shuō)明

    python中shape[0]與shape[1]的說(shuō)明

    這篇文章主要介紹了python中shape[0]與shape[1]的說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • python實(shí)現(xiàn)按行分割文件

    python實(shí)現(xiàn)按行分割文件

    這篇文章主要為大家詳細(xì)介紹了python如何實(shí)現(xiàn)按行分割文件,python按指定行數(shù)分割文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07

最新評(píng)論