简体   繁体   中英

How to select a row by primary key in sqlite3?

I'm trying to find a way to select a row in sqlite from Python. Say I have a table soft drinks with columns drink and price , with the primary key being drink name . I want to select coke out of drink name. How would I do this without looping through every single entry?

Here's my code:

conn = sqlite3.Connection("database.db")
curs = conn.cursor()
curs.execute("create table soft drinks (drink name TEXT, drink TEXT, price INTEGER)"
curs.execute("insert into soft drinks (drink name, drink, price) values ("coke", "whatever", 89)")

I'm looking for a command something like curs.execute(select "coke" from soft drinks) and it will return the whole row.

SQLite使用引号作为列标识符 ,因此您需要在列和表名周围加上引号: SELECT * FROM "soft drinks" WHERE "drink name" = 'Coke';

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