Python 如何通过tkinter删除SQL数据库中的多个数据项?
我创建了一个基于tkinter的应用程序,用户可以创建条目并根据需要进行修改。此应用程序与SQLite交互以存储数据。但是,我有一个问题,就是通过tkinter删除SQL数据库中的多个数据项。您可以检查以下代码:Python 如何通过tkinter删除SQL数据库中的多个数据项?,python,sqlite,tkinter,Python,Sqlite,Tkinter,我创建了一个基于tkinter的应用程序,用户可以创建条目并根据需要进行修改。此应用程序与SQLite交互以存储数据。但是,我有一个问题,就是通过tkinter删除SQL数据库中的多个数据项。您可以检查以下代码: from tkinter import * import backendv2 def get_selected_row(event): try: global selected_tuple index=list1.curselection()[
from tkinter import *
import backendv2
def get_selected_row(event):
try:
global selected_tuple
index=list1.curselection()[0]
selected_tuple=list1.get(index)
e1.delete(0,END)
e1.insert(END,selected_tuple[2])
e2.delete(0,END)
e2.insert(END,selected_tuple[5])
e9.delete(0,END)
e9.insert(END,selected_tuple[1])
print(index)
except IndexError:
pass
def view_command(): #View button function
list1.delete(0,END) #Delete items in listbox
for row in backendv2.view(): #loop for getting data in SQLite database
list1.insert(END,row) #Insert data to listbox
def search_command(): #Search button function
list1.delete(0,END) #Delete items in listbox
for row in backendv2.Search(Buildingcallsign_text.get()): #loop for fetching data in SQLite database based on search value
list1.insert(END,row) #Insert data in listbox
def add_command(): #Add entry button function
backendv2.calculate(Buildingcallsign_text.get(),AllPrisms.get(),NW_Prism.get(),NE_Prism.get(),SW_Prism.get(),\
SE_Prism.get(),NS_Distance.get(),WE_Distance.get(),Station_ID.get(),Variable_ID.get()) #Calculate and insert data to database based on user's entry
def delete_all():
backendv2.drop()
def reconnect():
backendv2.connect()
def Export_file():
backendv2.export()
def delete_command():
backendv2.delete(selected_tuple[0])
def update_command():
backendv2.update(selected_tuple[0],Station_ID.get(),Buildingcallsign_text.get(),AllPrisms.get())
window=Tk() #Initiate the app
window.wm_title("Virtual Variable") #Name of the App
l1=Label(window,text="Building callsign") #Building callsign label
l1.grid(row=0,column=0) #Setup location for Building callsign label
l2=Label(window,text="All Prisms") #All Prisms label
l2.grid(row=0,column=2) #Setup location for All Prisms label
l3=Label(window,text="NW Prism") #NW Prism label
l3.grid(row=1,column=0) #Setup location for NW Prism
l4=Label(window,text="NE Prism") #NE Prism label
l4.grid(row=1,column=2) #Setup location for NE Prism
l5=Label(window,text="SW Prism") #SW Prism label
l5.grid(row=2,column=0) #Setup location for SW Prism
l6=Label(window,text="SE Prism") #SE Prism label
l6.grid(row=2,column=2) #Setup location for SE Prism
l7=Label(window,text="NS Distance") #NS Distance label
l7.grid(row=3,column=0) #Setup location for NS Distance
l8=Label(window,text="WE Distance") #WE Distance label
l8.grid(row=3,column=2) #Setup location for WE Distance
l9=Label(window,text="Station ID") #Station ID label
l9.grid(row=4,column=0) #Setup location for Station ID
l10=Label(window,text="Variable ID") #Variable ID label
l10.grid(row=4,column=2) #Setup location for Variable ID
Buildingcallsign_text=StringVar() #Create a StringVar object
e1=Entry(window,textvariable=Buildingcallsign_text,width=40) #Create Building callsign entry box widget
e1.grid(row=0,column=1) #Setup Building callsign entry box widget location
AllPrisms=StringVar() #Create a StringVar object
e2=Entry(window,textvariable=AllPrisms,width=40) #Create AllPrisms entry box widget
e2.grid(row=0,column=3) #Setup AllPrisms entry box widget location
NW_Prism=StringVar() #Create a StringVar object
e3=Entry(window,textvariable=NW_Prism,width=40) #Create NW Prism entry box widget
e3.grid(row=1,column=1) #Setup location for NW Prism entry box widget
NE_Prism=StringVar() #Create a StringVar object
e4=Entry(window,textvariable=NE_Prism,width=40) #Create NE Prism entry box widget
e4.grid(row=1,column=3) #Setup location for NE Prism entry box widget
SW_Prism=StringVar() #Create a StringVar object
e5=Entry(window,textvariable=SW_Prism,width=40) #Create SW Prism entry box widget
e5.grid(row=2,column=1) #Setup location for SW Prism entry box widget
SE_Prism=StringVar() #Create a StringVar object
e6=Entry(window,textvariable=SE_Prism,width=40) #Create SE Prism entry box widget
e6.grid(row=2,column=3) #Setup location for SE Prism entry box widget
NS_Distance=StringVar() #Create a StringVar object
e7=Entry(window,textvariable=NS_Distance,width=40) #Create Ns Prism entry box widget
e7.grid(row=3,column=1) #Setup location for NS Prism entry box widget
WE_Distance=StringVar() #Create a StringVar object
e8=Entry(window,textvariable=WE_Distance,width=40) #Create WE Prism entry box widget
e8.grid(row=3,column=3) #Setup location for WE Prism entry box widget
Station_ID=StringVar() #Create a StringVar object
e9=Entry(window,textvariable=Station_ID,width=40) #Create Station ID Prism entry box widget
e9.grid(row=4,column=1) #Setup location for Station ID Prism entry box widget
Variable_ID=StringVar() #Create a StringVar object
e10=Entry(window,textvariable=Variable_ID,width=40) #Create Variable ID Prism entry box widget
e10.grid(row=4,column=3) #Setup location for Variable ID Prism entry box widget
list1=Listbox(window, height=9,width=55, selectmode=EXTENDED) #Create a listbox and setup its size
list1.grid(row=5,column=0,rowspan=7,columnspan=2) #Setup listbox location
sb1=Scrollbar(window) #Create a vertical scrollbar for the listbox
sb1.grid(row=6,column=2,rowspan=4,sticky="NS") #Setup the scrollbar's location and size - extends from top to bottom
list1.configure(yscrollcommand=sb1.set) #Attach the vertical scrollbar to the listbox
sb1.configure(command=list1.yview) #Setup scrollbar for vertical scrolling
sb2=Scrollbar(window, orient='horizontal') #Create a horizontal scrollbar for the listbox
sb2.grid(row=11,column=0,columnspan=2,sticky="WE") #Setup the scrollbar's location and size - extends from left to right
list1.configure(xscrollcommand=sb2.set) #Attach the horizontal scrollbar to the listbox
sb2.configure(command=list1.xview) #Setup scrollbar for horizontal scrolling
list1.bind('<<ListboxSelect>>',get_selected_row)
b1=Button(window,text="Add Entry",width=12,command=add_command) #Create the Add Entry button and assign to the add_command function above
b1.grid(row=5,column=3) #Setup the Add Entru button's location
b2=Button(window,text="Search Entry",width=12, command=search_command) #Create the Search Entry button and assign to the search_command above
b2.grid(row=6,column=3) #Setup the Search Entry button's location
b3=Button(window,text="View All",width=12,command=view_command) #Create the View All button and assign to the view_command above
b3.grid(row=7,column=3) #Setup the View All button's location
b4=Button(window,text="Update",width=12,command=update_command) #Create the Update button
b4.grid(row=8,column=3) #Setup the Update button's location
b5=Button(window,text="Delete",width=12,command=delete_command) #Create the Delete button
b5.grid(row=9,column=3) #Setup the Delete button's location
b6=Button(window,text="Export",width=12,command=Export_file) #Create the Export button
b6.grid(row=10,column=3) #Setup the Export button's location
b7=Button(window,text="Close",width=12,command=window.destroy) #Create the Close button
b7.grid(row=11,column=3) #Setup the Close button's location
window.mainloop()
您可以按以下方式尝试数据输入:
Building callsign: testing
All Prisms: Z101+Z102+Z103+Z104
NW Prism: Z101
NE Prism: Z102
SW Prism: Z103
SE Prism: Z104
NS Distance: 20
WE Distance: 12
Station ID: 1234
然后单击添加条目-查看全部,数据填充如下:
用户可以通过在列表框中选择一行,然后按delete按钮,一次删除一行数据。如果用户想要删除多行,这可能会给他们带来不便
是否可以执行多次删除?如果可能的话,我如何通过tkinter进行操作?因为您已经在列表1中使用了selectmode=EXTENDED,所以您可以选择多个项目 以下是删除多个选定项目的示例: 后端V2模块:
import sqlite3
import pandas
import csv
import re
def connect(): #Function to create SQL database
conn=sqlite3.connect("lite.db") #Establish connection to lite.db
cur=conn.cursor() #Create cursor object in dataase
cur.execute("CREATE TABLE IF NOT EXISTS calculation (ID INTEGER PRIMARY KEY,Station_ID INTEGER,Variable TEXT,\
Variable_ID INTEGER,Unit_ID INTEGER,Formula TEXT,RoC_Active INTEGER,RoC_Precision INTEGER,\
RoC_Period_Value INTEGER,RoC_Period_Type INTEGER,RoC_Unit_Value INTEGER,RoC_Unit_Type INTEGER,\
Datum_Variable_ID INTEGER,Datum_Timestamp TEXT,Datum_Information TEXT,Constants TEXT)") #Create SQL table if it does not exist
conn.commit() #Commit changes in database
conn.close() #Close the connection to database
def calculate(Building_callsign,AllPrisms,NW_Prism,NE_Prism,SW_Prism,SE_Prism,NS_Distance,WE_Distance,Station_ID,Variable_ID): #Function to calculate deformations and displacements
VVListofLists=[] #Assign an empty list to VVListofLists
VVList=[] #Assign an empty list to VVList
Tassgloname=Building_callsign + "Tass_Glo" #Assign Tassgloname to the name of Tassement Global variable with corresponding to building's name
ListAllPrisms=AllPrisms.split("+") #Create a list with all Prisms in AllPrisms input box
TasGLO="("+AllPrisms+")"+"/"+str(len(ListAllPrisms)) #Assign TasGLO as the calculation for Tassement Global
VVList=[Station_ID,Tassgloname,Variable_ID,42,TasGLO] #Put Station_ID,Tassgloname,Variable_ID,42,TasGLO into VVList of current input
VVListofLists.append(VVList) #Append VVList to VVListofLists
if (NW_Prism!="" and SW_Prism!="") or (NE_Prism!="" and SE_Prism!=""): #Conditional for Tassement differentiel NS calculation is possible
TasDifNSname=Building_callsign + "Tass_Diff_NS" #Assign TasDifNSname to the name of Tassement differentiel NS variable with corresponding to building's name
if NW_Prism!="" and SW_Prism!="" and NE_Prism!="" and SE_Prism!="": #Conditional for all prisms inputs are not empty
TasDifNS="("+"("+"Z"+str(NW_Prism)+"+"+"Z"+str(NE_Prism)+")"+"/"+"2"+"-"+"("+"Z"+str(SW_Prism)+"+"+"Z"+str(SE_Prism)+")"+"/"+"2"+")"+"/"+str(NS_Distance) #Assign TasDifNS as the calculation for Tassement Differentiel NS
elif NW_Prism!="" and SW_Prism!="" and NE_Prism!="" and SE_Prism=="": #Conditional for SE_Prism input box is empty
TasDifNS="("+"("+"Z"+str(NW_Prism)+"+"+"Z"+str(NE_Prism)+")"+"/"+"2"+"-"+"Z"+str(SW_Prism)+")"+"/"+str(NS_Distance) #Assign TasDifNS as the calculation for Tassement Differentiel NS
elif NW_Prism!="" and SW_Prism!="" and NE_Prism=="" and SE_Prism!="": #Conditional for NE_Prism input box is empty
TasDifNS="("+"Z"+str(NW_Prism)+"-"+"("+"Z"+str(SW_Prism)+"+"+"Z"+str(SE_Prism)+")"+"/"+"2"+")"+"/"+str(NS_Distance) #Assign TasDifNS as the calculation for Tassement Differentiel NS
elif NW_Prism!="" and SW_Prism=="" and NE_Prism!="" and SE_Prism!="": #Conditional for SW_Prism input box is empty
TasDifNS="("+"("+"Z"+str(NW_Prism)+"+"+"Z"+str(NE_Prism)+")"+"/"+"2"+"-"+"Z"+str(SE_Prism)+")"+"/"+str(NS_Distance) #Assign TasDifNS as the calculation for Tassement Differentiel NS
elif NW_Prism=="" and SW_Prism!="" and NE_Prism!="" and SE_Prism!="": #Conditional for NW_Prism input box is empty
TasDifNS="("+"Z"+str(NE_Prism)+"-"+"("+"Z"+str(SW_Prism)+"+"+"Z"+str(SE_Prism)+")"+"/"+"2"+")"+"/"+str(NS_Distance) #Assign TasDifNS as the calculation for Tassement Differentiel NS
elif NW_Prism!="" and SW_Prism!="" and NE_Prism=="" and SE_Prism=="": #Conditional for NE_Prism and SE_Prism input boxes are empty
TasDifNS="("+"Z"+str(NW_Prism)+"-"+"Z"+str(SW_Prism)+")"+"/"+str(NS_Distance) #Assign TasDifNS as the calculation for Tassement Differentiel NS
elif NW_Prism=="" and SW_Prism=="" and NE_Prism!="" and SE_Prism!="": #Conditional for NW_Prism and SW_Prism input boxes are empty
TasDifNS="("+"Z"+str(NE_Prism)+"-"+"Z"+str(SE_Prism)+")"+"/"+str(NS_Distance) #Assign TasDifNS as the calculation for Tassement Differentiel NS
VVList=[Station_ID,TasDifNSname,Variable_ID,101,TasDifNS] #Put Station_ID,TasDifNSname,Variable_ID,101,TasDifNS into VVList of current input
VVListofLists.append(VVList) #Append VVList to VVListofLists
else: #Conditional for Tassement differentiel NS calculation is not possible
TasDifNS="" #Set TasDifNS as blank
TasDifNSname="" #Set TasDifNSname as blank
if (NW_Prism!="" and NE_Prism!="") or (SW_Prism!="" and SE_Prism!=""): #Conditional for Tassement differentiel OE calculation is possible
TasDifOEname=Building_callsign + "Tass_Diff_OE" #Assign TasDifOEname to the name of Tassement differentiel OE variable with corresponding to building's name
if NW_Prism!="" and SW_Prism!="" and NE_Prism!="" and SE_Prism!="": #Conditional for all prisms inputs are not empty
TasDifOE="("+"("+"Z"+str(NW_Prism)+"+"+"Z"+str(SW_Prism)+")"+"/"+"2"+"-"+"("+"Z"+str(NE_Prism)+"+"+"Z"+str(SE_Prism)+")"+"/"+"2"+")"+"/"+str(WE_Distance) #Assign TasDifOE as the calculation for Tassement Differentiel OE
elif NW_Prism!="" and SW_Prism!="" and NE_Prism!="" and SE_Prism=="": #Conditional for SE_Prism input box is empty
TasDifOE="("+"("+"Z"+str(NW_Prism)+"+"+"Z"+str(SW_Prism)+")"+"/"+"2"+"-"+"Z"+str(NE_Prism)+")"+"/"+str(WE_Distance) #Assign TasDifOE as the calculation for Tassement Differentiel OE
elif NW_Prism!="" and SW_Prism!="" and NE_Prism=="" and SE_Prism!="": #Conditional for NE_Prism input box is empty
TasDifOE="("+"("+"Z"+str(SW_Prism)+"+"+"Z"+str(NW_Prism)+")"+"/"+"2"+"-"+"Z"+str(SE_Prism)+")"+"/"+str(WE_Distance) #Assign TasDifOE as the calculation for Tassement Differentiel OE
elif NW_Prism!="" and SW_Prism=="" and NE_Prism!="" and SE_Prism!="": #Conditional for SW_Prism input box is empty
TasDifOE="("+"Z"+str(NW_Prism)+"-"+"("+"Z"+str(SE_Prism)+"+"+"Z"+str(NE_Prism)+")"+"/"+"2"+")"+"/"+str(WE_Distance) #Assign TasDifOE as the calculation for Tassement Differentiel OE
elif NW_Prism=="" and SW_Prism!="" and NE_Prism!="" and SE_Prism!="": #Conditional for NW_Prism input box is empty
TasDifOE="("+"Z"+str(SW_Prism)+"-"+"("+"Z"+str(NE_Prism)+"+"+"Z"+str(SE_Prism)+")"+"/"+"2"+")"+"/"+str(WE_Distance) #Assign TasDifOE as the calculation for Tassement Differentiel OE
elif NW_Prism!="" and NE_Prism!="" and SW_Prism=="" and SE_Prism=="": #Conditional for SW_Prism and SE_Prism input boxes are empty
TasDifOE="("+"Z"+str(NW_Prism)+"-"+"Z"+str(NE_Prism)+")"+"/"+str(WE_Distance) #Assign TasDifOE as the calculation for Tassement Differentiel OE
elif NW_Prism=="" and NE_Prism=="" and SW_Prism!="" and SE_Prism!="": #Conditional for NW_Prism and NE_Prism input boxes are empty
TasDifOE="("+"Z"+str(SW_Prism)+"-"+"Z"+str(SE_Prism)+")"+"/"+str(WE_Distance) #Assign TasDifOE as the calculation for Tassement Differentiel OE
VVList=[Station_ID,TasDifOEname,Variable_ID,101,TasDifOE] #Put Station_ID,TasDifOEname,Variable_ID,101,TasDifOE into VVList of current input
VVListofLists.append(VVList) #Append VVList to VVListofLists
else: #Conditional for Tassement differentiel OE calculation is not possible
TasDifOE="" #Set TasDifOE as blank
TasDifOEname="" #Set TasDifOEname as blank
if (NW_Prism!="" and NE_Prism!="") or (SW_Prism!="" and SE_Prism!=""): #Conditional for Deformation Horizontal NS calculation is possible
DEHNSname=Building_callsign + "DEH_NS" #Assign DEHNSname to the name of Deformation Horizontal NS variable with corresponding to building's name
if NW_Prism!="" and SW_Prism!="" and NE_Prism!="" and SE_Prism!="": #Conditional for all prisms inputs are not empty
DEHNS="("+"("+"Y"+str(NW_Prism)+"+"+"Y"+str(SW_Prism)+")"+"/"+"2"+"-"+"("+"Y"+str(NE_Prism)+"+"+"Y"+str(SE_Prism)+")"+"/"+"2"+")"+"/"+str(NS_Distance) #Assign DEHNS as the calculation for Deformation Horizontal NS
elif NW_Prism!="" and SW_Prism!="" and NE_Prism!="" and SE_Prism=="": #Conditional for SE_Prism input box is empty
DEHNS="("+"("+"Y"+str(NW_Prism)+"+"+"Y"+str(SW_Prism)+")"+"/"+"2"+"-"+"Y"+str(NE_Prism)+")"+"/"+str(NS_Distance) #Assign DEHNS as the calculation for Deformation Horizontal NS
elif NW_Prism!="" and SW_Prism!="" and NE_Prism=="" and SE_Prism!="": #Conditional for NE_Prism input box is empty
DEHNS="("+"("+"Y"+str(SW_Prism)+"+"+"Y"+str(NW_Prism)+")"+"/"+"2"+"-"+"Y"+str(SE_Prism)+")"+"/"+str(NS_Distance) #Assign DEHNS as the calculation for Deformation Horizontal NS
elif NW_Prism!="" and SW_Prism=="" and NE_Prism!="" and SE_Prism!="": #Conditional for SW_Prism input box is empty
DEHNS="("+"Y"+str(NW_Prism)+"-"+"("+"Y"+str(SE_Prism)+"+"+"Y"+str(NE_Prism)+")"+"/"+"2"+")"+"/"+str(NS_Distance) #Assign DEHNS as the calculation for Deformation Horizontal NS
elif NW_Prism=="" and SW_Prism!="" and NE_Prism!="" and SE_Prism!="": #Conditional for NW_Prism input box is empty
DEHNS="("+"Y"+str(SW_Prism)+"-"+"("+"Y"+str(NE_Prism)+"+"+"Y"+str(SE_Prism)+")"+"/"+"2"+")"+"/"+str(NS_Distance) #Assign DEHNS as the calculation for Deformation Horizontal NS
elif NW_Prism!="" and NE_Prism!="" and SW_Prism=="" and SE_Prism=="": #Conditional for SW_Prism and SE_Prism input boxes are empty
DEHNS="("+"Y"+str(NW_Prism)+"-"+"Y"+str(NE_Prism)+")"+"/"+str(NS_Distance) #Assign DEHNS as the calculation for Deformation Horizontal NS
elif NW_Prism=="" and NE_Prism=="" and SW_Prism!="" and SE_Prism!="": #Conditional for NW_Prism and NE_Prism input boxes are empty
DEHNS="("+"Y"+str(SW_Prism)+"-"+"Y"+str(SE_Prism)+")"+"/"+str(NS_Distance) #Assign DEHNS as the calculation for Deformation Horizontal NS
VVList=[Station_ID,DEHNSname,Variable_ID,101,DEHNS] #Put Station_ID,DEHNSname,Variable_ID,101,DEHNS into VVList of current input
VVListofLists.append(VVList) #Append VVList to VVListofLists
else: #Conditional for Deformation Horizontal NS calculation is not possible
DEHNS="" #Set DEHNS as blank
DEHNSname="" #Set DEHNSname as blank
if (NW_Prism!="" and SW_Prism!="") or (NE_Prism!="" and SE_Prism!=""): #Conditional for Deformation Horizontal OE calculation is possible
DEHOEname=Building_callsign + "DEH_OE" #Assign DEHOEname to the name of Deformation Horizontal OE variable with corresponding to building's name
if NW_Prism!="" and SW_Prism!="" and NE_Prism!="" and SE_Prism!="": #Conditional for all prisms inputs are not empty
DEHOE="("+"("+"X"+str(NW_Prism)+"+"+"X"+str(NE_Prism)+")"+"/"+"2"+"-"+"("+"X"+str(SW_Prism)+"+"+"X"+str(SE_Prism)+")"+"/"+"2"+")"+"/"+str(WE_Distance) #Assign DEHOE as the calculation for Deformation Horizontal OE
elif NW_Prism!="" and SW_Prism!="" and NE_Prism!="" and SE_Prism=="": #Conditional for SE_Prism input box is empty
DEHOE="("+"("+"X"+str(NW_Prism)+"+"+"X"+str(NE_Prism)+")"+"/"+"2"+"-"+"X"+str(SW_Prism)+")"+"/"+str(WE_Distance) #Assign DEHOE as the calculation for Deformation Horizontal OE
elif NW_Prism!="" and SW_Prism!="" and NE_Prism=="" and SE_Prism!="": #Conditional for NE_Prism input box is empty
DEHOE="("+"X"+str(NW_Prism)+"-"+"("+"X"+str(SW_Prism)+"+"+"X"+str(SE_Prism)+")"+"/"+"2"+")"+"/"+str(WE_Distance) #Assign DEHOE as the calculation for Deformation Horizontal OE
elif NW_Prism!="" and SW_Prism=="" and NE_Prism!="" and SE_Prism!="": #Conditional for SW_Prism input box is empty
DEHOE="("+"("+"X"+str(NW_Prism)+"+"+"X"+str(NE_Prism)+")"+"/"+"2"+"-"+"X"+str(SE_Prism)+")"+"/"+str(WE_Distance) #Assign DEHOE as the calculation for Deformation Horizontal OE
elif NW_Prism=="" and SW_Prism!="" and NE_Prism!="" and SE_Prism!="": #Conditional for NW_Prism input box is empty
DEHOE="("+"X"+str(NE_Prism)+"-"+"("+"X"+str(SW_Prism)+"+"+"X"+str(SE_Prism)+")"+"/"+"2"+")"+"/"+str(WE_Distance) #Assign DEHOE as the calculation for Deformation Horizontal OE
elif NW_Prism!="" and SW_Prism!="" and NE_Prism=="" and SE_Prism=="": #Conditional for NE_Prism and SE_Prism input boxes are empty
DEHOE="("+"X"+str(NW_Prism)+"-"+"X"+str(SW_Prism)+")"+"/"+str(WE_Distance) #Assign DEHOE as the calculation for Deformation Horizontal OE
elif NW_Prism=="" and SW_Prism=="" and NE_Prism!="" and SE_Prism!="": #Conditional for NW_Prism and SW_Prism input boxes are empty
DEHOE="("+"X"+str(NE_Prism)+"-"+"X"+str(SE_Prism)+")"+"/"+str(WE_Distance) #Assign DEHOE as the calculation for Deformation Horizontal OE
VVList=[Station_ID,DEHOEname,Variable_ID,101,DEHOE] #Put Station_ID,DEHOEname,Variable_ID,101,DEHOE into VVList of current input
VVListofLists.append(VVList) #Append VVList to VVListofLists
else: #Conditional for Deformation Horizontal OE calculation is not possible
DEHOE="" #Set DEHOE as blank
DEHOEname="" #Set DEHOEname as blank
for a_list in VVListofLists: #Loop for each list in VVListofLists
insert(*a_list) #Call the insert function with arguments based of items in a_list
return TasGLO, Tassgloname, TasDifNS, TasDifNSname, TasDifOE, TasDifOEname, DEHNS, DEHNSname, DEHOE, DEHOEname
def insert(Station_ID,Virtual_Variable,Variable_ID,Unit_ID,Formula):
conn=sqlite3.connect("lite.db")
cur=conn.cursor()
cur.execute("INSERT INTO calculation VALUES(NULL,?,?,?,?,?,0,0,1,3600,1,3600,0,\"0000-00-00 00:00:00\",0,\"\")",(Station_ID,Virtual_Variable,Variable_ID,Unit_ID,Formula))
conn.commit()
conn.close()
def view():
conn=sqlite3.connect("lite.db")
cur=conn.cursor()
cur.execute("SELECT * FROM calculation")
rows=cur.fetchall()
conn.close()
return rows
def drop():
conn=sqlite3.connect("lite.db")
cur=conn.cursor()
cur.execute("DROP TABLE calculation")
conn.close()
def Search(Virtual_Variable):
conn=sqlite3.connect("lite.db")
cur=conn.cursor()
cur.execute("SELECT * FROM calculation WHERE Variable LIKE ?",(Virtual_Variable+"%",))
rows=cur.fetchall()
conn.close()
return rows
def delete(id):
conn=sqlite3.connect("lite.db")
cur=conn.cursor()
cur.execute("DELETE FROM calculation WHERE id=?",(id,))
conn.commit()
conn.close()
def export():
conn = sqlite3.connect('lite.db')
cur=conn.cursor()
sql = """
SELECT
Station_ID,
'"' || COALESCE(Variable, '') || '"' Variable,
Variable_ID,
Unit_ID,
'"' || COALESCE(Formula, '') || '"' Formula,
RoC_Active,
RoC_Precision,
RoC_Period_Value,
RoC_Period_Type,
RoC_Unit_Value,
RoC_Unit_Type,
Datum_Variable_ID,
'"' || COALESCE(Datum_Timestamp, '') || '"' Datum_Timestamp,
'"' || COALESCE(Datum_Information, '') || '"' Datum_Information,
'"' || COALESCE(Constants, '') || '"' Constants
FROM calculation
"""
cur.execute(sql)
rows=cur.fetchall()
csv_path = "output.csv"
with open(csv_path, "w", newline="") as csv_file:
csv_writer = csv.writer(csv_file,delimiter=";",quotechar='',quoting=csv.QUOTE_NONE)
# Write headers.
csv_writer.writerow([i[0] for i in cur.description])
# Write data.
csv_writer.writerows(rows)
def update(id,Station_ID,Virtual_Variable,Formula):
conn = sqlite3.connect("lite.db")
cur = conn.cursor()
cur.execute("UPDATE calculation SET Station_ID=?, Variable=?, Formula=? WHERE id=?",(Station_ID,Virtual_Variable,Formula,id))
conn.commit()
conn.close()
connect()
def delete_manyid_列表:
占位符=','。连接'?'*lenid_列表
sql=fDELETE来自计算,其中id位于{占位符}
printsql,id\u列表
cur.executesql,id\u列表
主要模块:
将tkinter作为tk导入
导入后端v2
选定的_id_列表=[]
def get_selectd_ROW事件:
全局选定\u id\u列表
选定的\u id\u list=[list1.getindex[0]用于列表1.1中的索引]
打印选定的\u id\u列表
def delete_命令:
如果选择了\u id\u列表:
backendv2.delete_many selected_id_列表
从列表框中删除项目
对于列表1.1中的项,选择[:-1]:
列表1.deleteitem
root=tk.tk
list1=tk.Listboxroot,宽度=20,选择mode=tk.EXTENDED
列表1.pack
list1.bind,获取所选行
插入虚拟项目
对于范围1、11中的i:
列表1.insertend,i,1234,fItem_{i},
tk.Buttonroot,text=Delete,command=Delete\u command.pack
root.mainloop
您还可以使用backendv2.delete,如下所示:
def delete_命令:
如果选择了\u id\u列表:
对于所选id列表中的id:
backendv2.deleteid
删除列表框中的项目
对于列表1.1中的项,选择[:-1]:
列表1.deleteitem
您可以使用从v1、v2、v3中的某个_字段所在的某个_表中删除来删除数据库中的多个记录。用户希望通过选择要删除的行来删除多个条目。但是,上面的代码一次只能删除一行。我可以将SQL查询传递给delete,但为了特定目的而使用tkinter实现它是我感到困惑的地方。上面已经提到了一般的解决方案。问题是我不知道如何将多个ID从listbox选择传递到backendv2模块的delete查询。您能够从listbox选择多个项目吗?很抱歉,太晚了回答但你的解决方案很有魅力。您帮助十几个人避免了在数据输入上浪费数千小时。非常感谢。