【发布时间】:2018-08-17 01:23:10
【问题描述】:
我正在用 SQLite、python 和 tkinter 编写一个小目录。我在数据库的一列中插入多个单词时遇到问题,当我插入一个包含两个以上单词的名称时,它出现在 {} 中(如图所示),而不是在另一列中列,我该如何解决这个问题?
后端:
import sqlite3
def connect():
"""Set up a connection with the database."""
conn_obj = sqlite3.connect("Database.db")
cur_obj = conn_obj.cursor()
cur_obj.execute("CREATE TABLE IF NOT EXISTS "
"book (id integer PRIMARY KEY, "
"title text, "
"name text, "
"email text, "
"address text, "
"telephone integer, "
"cellphone integer, "
"description text, "
"company text, "
"addresscompany text, "
"telephonecompany integer, "
"cellphonecompany integer, "
"company text)")
conn_obj.commit()
conn_obj.close()
def insert(title, name, email, address, telephone, cellphone, description, company, addresscompany, telephonecompany, cellphonecompany, comment):
"""Insert entry into database."""
conn_obj = sqlite3.connect("Database.db")
cur_obj = conn_obj.cursor()
cur_obj.execute("INSERT INTO book "
"VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (title, name, email, address, telephone, cellphone, description, company, addresscompany, telephonecompany, cellphonecompany, comment))
conn_obj.commit()
conn_obj.close()
connect()
def view():
"""View all database entries."""
conn_obj = sqlite3.connect("Database.db")
cur_obj = conn_obj.cursor()
cur_obj.execute("SELECT * FROM book")
rows = cur_obj.fetchall()
conn_obj.close()
return rows
前端:
from tkinter import *
import backend
def add_command():
"""Insert entry via button."""
backend.insert(title_text.get(),
name_text.get(),
email_text.get(),
address_text.get(),
telephone_text.get(),
cellphone_text.get(),
description_text.get(),
company_text.get(),
addresscompany_text.get(),
telephonecompany_text.get(),
cellphonecompany_text.get(),
comment_text.get())
listing.delete(0, END)
listing.insert(END,
(title_text.get(),
name_text.get(),
email_text.get(),
address_text.get(),
telephone_text.get(),
cellphone_text.get(),
description_text.get(),
company_text.get(),
addresscompany_text.get(),
telephonecompany_text.get(),
cellphonecompany_text.get(),
comment_text.get()))
def view_command():
"""View entries via button."""
listing.delete(0, END)
for row in backend.view():
listing.insert(END, row)
listing = Listbox(window, height = 6, width = 100)
listing.grid(row = 4, column = 0, rowspan = 7, columnspan = 6)
window = Tk()
window.wm_title("Directory")
name_text = StringVar()
entry2 = Entry(window, textvariable = name_text)
entry2.grid(row = 1, column = 1)
【问题讨论】:
-
从控制台 (
sqlite3 Database.db) 运行select时,您是否在数据库中看到{}? -
不,{} 只出现在我的 GUI 上
-
如何在 Python 中选择这些值?选择它们并将它们传递给 UI 的代码在哪里?
-
我只是将缺少的代码部分添加到帖子中