开发者

How to select a row by primary key in sqlite3?

开发者 https://www.devze.com 2023-02-03 07:54 出处:网络
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

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) value开发者_开发问答s ("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 uses quotation marks as column identifiers, so you'd need to put quotation marks around the column and table name: SELECT * FROM "soft drinks" WHERE "drink name" = 'Coke';

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号