我正在创建一个代码,该代码将从数据库中获取数据并以pandastable显示。它完美地显示了数据。我编辑了一个单元格,然后按Enter键并下载了运行良好的数据。但是我如何也将同一单元更新到数据库中?
以下是我的pandastable代码:
from tkinter import *
import tkinter as tk
from tkinter import ttk
from pandastable import Table, TableModel
import sqlite3
import pandas as pd
import Backend
root = tk.Tk()
root.geometry("1250x650+0+0")
root.title("MAYA")
root.configure(background="black")
f = Frame(root)
f.pack(fill=BOTH,expand=1)
conn = sqlite3.connect("99_data_increment.db")
df = pd.read_sql_query("SELECT * FROM crawled",conn)
pt = Table(f, dataframe=df, showtoolbar=True, showstatusbar=True)
pt.show()
blank = ""
#Backend.insert(blank, blank, blank, blank, blank, "1", blank, blank, blank, blank, blank, blank,blank, blank, blank, blank,blank)
root.mainloop()
这些是我的数据库代码:
import sqlite3
def connect():
conn=sqlite3.connect("99_data_increment.db")
cur=conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS crawled (id INTEGER PRIMARY KEY, State , XID , Project_Name , City , Main_City , Registration_Number , Promoter_Name , Rera_URL , PDF_text, Crawled_Date , Status, Names, Transaction_Date, Comments, Call_Contact_Number, Creation_Type, Builder_Website)")
conn.commit()
conn.close()
def insert(State, XID, Project_Name, City, Main_City, Registration_Number, Promoter_Name, Rera_URL, PDF_text, Crawled_Date, Status, Names, Transaction_Date, Comments, Call_Contact_Number, Creation_Type, Builder_Website):
conn=sqlite3.connect("99_data_increment.db")
cur=conn.cursor()
cur.execute("INSERT INTO crawled VALUES (NULL,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",(State, XID, Project_Name, City, Main_City, Registration_Number, Promoter_Name, Rera_URL, PDF_text, Crawled_Date, Status, Names, Transaction_Date, Comments, Call_Contact_Number, Creation_Type, Builder_Website))
conn.commit()
conn.close()
connect()
答案 0 :(得分:1)
您在DataFrame中拥有所有数据,可以使用
替换数据df.to_sql("crawled", conn, if_exists="replace")
因此您可以在Tkinter中添加按钮,该按钮将使用to_sql()
import tkinter as tk
from tkinter import ttk
from pandastable import Table, TableModel
import sqlite3
import pandas as pd
import Backend
# --- functions ---
def save():
df.to_sql("crawled", conn, if_exists="replace")
# --- main ---
root = tk.Tk()
#root.geometry("1250x650+0+0")
#root.title("MAYA")
#root.configure(background="black")
f = tk.Frame(root)
f.pack(fill="both", expand=True)
conn = sqlite3.connect("99_data_increment.db")
df = pd.read_sql_query("SELECT * FROM crawled", conn)
pt = Table(f, dataframe=df, showtoolbar=True, showstatusbar=True)
pt.show()
f = tk.Button(root, text="Save", command=save)
f.pack(fill="both", expand=True)
root.mainloop()
编辑
如果我创建自己的表(使用pandatable.Table
,则可以更改在结束单元格编辑时执行的handleCellEntry()
-这样我就可以看到更改了哪一行并可以更新数据库中的行。
类似这样的东西
import tkinter as tk
from tkinter import ttk
from pandastable import Table, TableModel
import sqlite3
import pandas as pd
import Backend
# --- classes ---
class MyTable(Table):
def handleCellEntry(self, row, col):
super().handleCellEntry(row, col)
print('changed:', row, col, "(TODO: update database)")
#pd.read_sql_query("UPDATE ...", conn)
# or remeber `row` on list and `UPDATE` it later
return
# --- main ---
root = tk.Tk()
#root.geometry("1250x650+0+0")
#root.title("MAYA")
#root.configure(background="black")
f = tk.Frame(root)
f.pack(fill="both", expand=True)
conn = sqlite3.connect("99_data_increment.db")
df = pd.read_sql_query("SELECT * FROM crawled", conn)
pt = MyTable(f, dataframe=df, showtoolbar=True, showstatusbar=True) # <-- MyTable
pt.show()
root.mainloop()