Take the 2-minute tour ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

I'm pretty new to python and SQLite but have done substantial work with MySQL and php.

I'm trying trying to do a conditional select on some data and would like to use the AND operator.

The MySQL query would look like this.

SELECT * FROM sensors WHERE sensor_id = "sometext" AND group_id = "somegroup"

Here's what my python looks like

Import SQLite3 as sqlite

con = sqlite.connect('../DB/system.db')

with con:

cur = con.cursor()

cur.execute("SELECT * FROM sensors WHERE sensor_id=:id",{"id": subdirname})
con.commit()
share|improve this question
1  
Your question is a generic Python question, and not Raspberry Pi specific. You'll have a better chance of receiving an answer if you ask it on stackoverflow. –  Pedro Romano Dec 2 '12 at 10:52
    
I thought about putting it on stack overflow but I wanted to get the answer on the raspberry pi section, in case other new python SQLite programmers had the same issue. The accepted answer adds clarity to an issue that is difficult to search for on google, with the AND/OR keywords in the search context. –  peledies Dec 2 '12 at 17:37
add comment

1 Answer 1

up vote 1 down vote accepted

You should be able to continue the SQL query in the same manner and add another named placeholder with a corresponding element in the parameter dictionary.

cur.execute("SELECT * FROM sensors WHERE sensor_id=:id AND group_id=:gid", {"id": subdirname, "gid": somevar})
share|improve this answer
    
Worked like a charm. Thanks –  peledies Dec 2 '12 at 17:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.