简体   繁体   中英

Can't display sqlite3 data on python

I want to display a specific column from sqlite3 on a python Tkinter combobox, but what's displayed instead is <sqlite3.Cursor object at 0x0000000003E73110> , I don't know what to do? I just started coding not a while ago.

I write the following:

from tkinter import *
from tkinter import ttk
import  sqlite3

conn = sqlite3.connect('Library.db')
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS tablename (Name TEXT UNIQUE NOT NULL)")
combolist = c.execute("SELECT Name FROM tablename")


root = Tk()
ttk.Combobox(root, value = (combolist))

Calling execute on a cursor object won't return you the values. Instead you can iterate over the cursor, or call c.fetchall() to get all of the results back together.

sqlite3 also supports a shorthand where you can call conn.execute(...) and get back the results directly without using an explicit cursor at all, but that requires you to call execute on the connection rather than the cursor.

Also you may want to unpack the name field out of each row.

I think this code should work (though I haven't tested it):

conn = sqlite3.connect('Library.db')
with conn:
    conn.execute("CREATE TABLE IF NOT EXISTS tablename (Name TEXT UNIQUE NOT NULL)")

... more code that actually inserts some data is assumed here ...

with conn:
    combolist = [row["name"] for row in conn.execute("SELECT Name FROM tablename"))]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM