pandas - 如何使用 pandastable 更新数据库

标签 pandas tkinter sqlite python-3.6

我正在创建一个代码,将从数据库中获取数据并将其显示在 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()

最佳答案

您在 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()

关于pandas - 如何使用 pandastable 更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51533805/

相关文章:

python-3.x - Pandas 总计数高于阈值

python - 有效使用 ttk (tk 8.5) Notebook 小部件(选项卡滚动)

c# - 基于日期范围的 SQLite 总和列

Python(Tkinter)创建菜单不会显示

sqlite3 - MAX() 函数不返回列的最大值

java - SQLite 数据库查询只返回一行

python - 我什么时候必须使用 plt.show()?

python - 如何将字典输出到数据帧,包括 None 类型值

python - 将带有二维数组列的 pandas 数据框保存为 python 中的 Parquet 文件

python - _tkinter.TclError : Item 1 already exists with tkinter treeview