码迷,mamicode.com
首页 > 数据库 > 详细

python连接Mysql数据库写的小电话本

时间:2018-04-16 21:32:41      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:als   note   selection   view   python   cep   def   pytho   cti   

先不说了,先把代码放这,等有时间再说明程序里的问题,

这个小程序知识把功能实现,并没有对布局方面有太多追求

有些注释掉的也没有删除,也没有个函数包起来,整体看着

应该是有点乱

以下是完整代码,

import tkinter
import pymysql
from tkinter import *
from tkinter import  ttk
window=tkinter.Tk()#创建窗口

window.title("电话簿")#设置标题
window.geometry(500x500+500+200)#设置大小偏移量
window.resizable(width=False,height=False)#禁用宽高
database = pymysql.connect(host="localhost", user="root", password="******",
                               db="phonebook", port=3306, charset="utf8")

ListN=ttk.Treeview(window)
ListN=ttk.Treeview(window,show="headings",height=10,columns=("a","b","c","d"))
#ListN["columns"]=("a","b","c","d")
ListN.column("a",width=60)
ListN.column("b",width=100)
ListN.column("c",width=120)
ListN.column("d",width=200)
ListN.heading("a",text="姓名")
ListN.heading("b",text="电话")
ListN.heading("c",text="邮箱")
ListN.heading("d",text="住址")
ListN["selectmode"]="browse"
def DeleteInfo(name,phone):
    sql_delete=r"delete from info where name=‘%s‘ "                r"and phone=‘%s‘;" % (name,phone)
    cur=database.cursor()
    try:
        cur.execute(sql_delete)
        database.commit()
    except Exception as e:
        database.rollback()
    cur.close()
def ListAll():
    sql_sel_all="select * from info;"
    cur=database.cursor()
    try:
        cur.execute(sql_sel_all)
        results=cur.fetchall()
        #print("姓名","电话","邮箱","住址")

        #首先删除表格中的原节点数据
        for _ in map(ListN.delete,ListN.get_children("")):
            pass

        ii=0
        for row in results:
            name=row[0]
            phone=row[1]
            mail=row[2]
            address=row[3]
            #print(name,phone,mail,address)
            ListN.insert("",ii,text=ii+1,values=(name,phone,mail,address))
            ii=ii+1
    except Exception as e:
        raise e
    cur.close()
def InsertInfo(name,phone,mail=None,address=None):
    sql_insert=r"insert into info (name,phone,"                r"mail,address) values (‘%s‘,‘%s‘,‘%s‘,"                r"‘%s‘);" % (name,phone,mail,address)
    cur=database.cursor()
    try:
        cur.execute(sql_insert)
        database.commit()
    except Exception as e:
        database.rollback()
    cur.close()
def Nodes():
    notenote1="说明:"
    notenote2="     添加联系人时至少输入姓名和电话"
    notenote3 ="     邮箱和住址可以省略,可以添加同名"
    notenote4 ="     但不能添加同名,同电话的信息"
    notenote5 ="删除时只需选中要删除的项,然后点击"
    notenote6 ="删除联系人按钮即可删除"
    note1=Label(window,text=notenote1)
    note1.pack()
    Label(window,text=notenote2).pack()
    Label(window,text=notenote3).pack()
    Label(window,text=notenote4).pack()
    Label(window,text=notenote5).pack()
    Label(window,text=notenote6).pack()
    pass
#创建子窗口
def zwindow():

    zwin = tkinter.Tk()
    zwin.title("添加")
    zwin.geometry(350x200+520+350)
    zwin.resizable(width=False,height=False)
    Ln=Label(zwin,text="姓名")
    Lp=Label(zwin,text="电话")
    Lm=Label(zwin,text="邮箱")
    La=Label(zwin,text="地址")
    Ln.grid(row=0)
    Lp.grid(row=1)
    Lm.grid(row=2)
    La.grid(row=3)
    e1=Entry(zwin)
    e2=Entry(zwin)
    e3=Entry(zwin)
    e4=Entry(zwin)
    e1.grid(row=0,column=1)
    e2.grid(row=1,column=1)
    e3.grid(row=2,column=1)
    e4.grid(row=3,column=1)

    def InsertI():
        InsertInfo(e1.get(), e2.get(), e3.get(), e4.get())
        zwin.destroy()
        ListAll()
    YButton=Button(zwin,text=确定,command=InsertI)
    NButton=Button(zwin,text=取消,command=zwin.destroy)
    YButton.grid(row=4,column=1,padx=3)
    NButton.grid(row=4,column=2)
def Delete():
    #ListN.
    items=ListN.selection()#返回被选中行的ID
    Delete_name=ListN.item(items)["values"][0]
    Delete_phone=ListN.item(items)["values"][1]
    #print(items)
    #print(ListN.item(items))
    #print(Delete_name)
    #print(Delete_phone)
    DeleteInfo(Delete_name,Delete_phone)
    ListAll()
    pass
InsertB=Button(window,text=添加联系人,command=zwindow)
DeleteB=Button(window,text=删除联系人,command=Delete)
ListAll()
ListN.pack()
InsertB.pack()
DeleteB.pack()
Nodes()
window.mainloop()

 另外附一个 练习 连接数据库时的代码:

# coding = utf-8
import pymysql
#连接数据库
database=pymysql.connect(host="localhost",user="root",password="******",
                         db="pydata",port=3306)
cur=database.cursor()

print("#1.首先查询数据库")
sql="select * from info"
try:
    cur.execute(sql)
    results=cur.fetchall()
    print("姓名","ID")
    for row in results:
        name=row[0]
        id=row[1]
        print(name,id)
except Exception as e:
    raise e
print("#2.插入数据")
sql_in_name=input("请输入要插入的姓名:")
sql_in_id=input("请输入要插入的ID:")
sql_insert=r"insert into info (name,id) values (‘%s‘,‘%s‘);" % (sql_in_name,sql_in_id)
try:
    cur.execute(sql_insert)
    database.commit()
except Exception as e:
    database.rollback()
print("验证插入")
try:
    cur.execute(sql)
    results=cur.fetchall()
    #print("姓名","ID")
    for row in results:
        name=row[0]
        id=row[1]
        print(name,id)
except Exception as e:
    raise e

 

python连接Mysql数据库写的小电话本

标签:als   note   selection   view   python   cep   def   pytho   cti   

原文地址:https://www.cnblogs.com/khzf/p/8858630.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!