【问题标题】:Sentences in columns SQLiteSQLite 列中的句子
【发布时间】: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 的代码在哪里?
  • 我只是将缺少的代码部分添加到帖子中

标签: python sqlite tkinter


【解决方案1】:

{} 表示您正在插入一个元组,tkinter 需要一个字符串。在将数据插入小部件之前将列表或元组显式转换为字符串(大括号是底层 tcl 解释器表示列表的方式)。

用这段简单的代码很容易说明:

import Tkinter as tk
data = ("one", "two three", "four")
root = tk.Tk()
listbox = tk.Listbox(root)
listbox.pack(fill="both", expand=True)
listbox.insert("end", data)
root.mainloop()

上面的代码将产生一个如下所示的窗口:

底线是insert 方法需要一个字符串,而不是字符串列表。您需要在调用insert 之前将数据显式转换为字符串。

例如:

listbox.insert("end", " ".join(data))

【讨论】:

  • 我明白,但我已经用 StringVar() 将我的输入转换为字符串
  • @matilarab:您正在使用StringVar,但您正在为insert 方法提供StringVars 的列表。此方法需要一个字符串,而不是列表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多