python - Sqlite3 List index out of range when trying to get LIKE Query -
i'm trying execute following code:
conn = sqlite3.connect("./databases/functions/he.db") cursor = conn.cursor() sql = "select * requests request ?" cursor.execute(sql, [(msg)]) results = cursor.fetchall()[0] print results if((results)[1] == "true"): gpio = (results)[2] direction = (results)[3] print "gpio setup ready.." and i'm getting following error:
file "./functions/dofunctions.py", line 29, in enrequests results = cursor.fetchall()[0] indexerror: list index out of range i'm messing database, , fixing problems, don't know why pop beginning..
the code getting msg string, searching if exist on database, if taking gpio pin , sending signal (request @ index 0 in database row while gpio pin @ index 2).
your query returned 0 results, there no first row fetch. .fetchall() returns empty list in case, , there no item @ index 0.
if interested in first result of query, should use .fetchone() instead:
results = cursor.fetchone() .fetchone() either returns row, or none if there no matching results.
you using redundant parenthesis in query; not cause of problem, can safely remove them:
cursor.execute(sql, [msg]) or use tuple:
cursor.execute(sql, (msg,)) you in several places, can cut out lot of (...) in code without effect other improved readability. adding in test if there are results .fetchone() call code be:
if results , results[1] == "true": gpio = results[2] direction = results[3] print "gpio setup ready.."
Comments
Post a Comment