Home > Software design >  Python SQLite multiple question marks query leads to error "row value misused"
Python SQLite multiple question marks query leads to error "row value misused"

Time:10-27

The below sample query leads to "row value misused" error. I am just not able to figure out the problem. My intent is to extract all records where "ISIN" matches "Test1" and "Test2".

Environment: Python 3.10.0 Windows 21H1

import sqlite3 as objSQLite

# Connect
objDatabase = objSQLite.connect("Sample.db")
objCursor = objDatabase.cursor()

# Create table
objCursor.execute("create table if not exists ScripData(ISIN text);")

# Add rows
objCursor.execute("insert or ignore into ScripData values(?)", ("Test1", ))
objCursor.execute("insert or ignore into ScripData values(?)", ("Test2", ))
objCursor.execute("insert or ignore into ScripData values(?)", ("Test3", ))
objCursor.execute("insert or ignore into ScripData values(?)", ("Test4", ))

objDatabase.commit()

# Query
objCursor.execute("select * from ScripData where ISIN=(?,?)", ("Test1", "Test2", ))
arrResult = objCursor.fetchall()
print(arrResult)

Error is:

Traceback (most recent call last):
  File "Query.py", line 19, in <module>
    objCursor.execute("select * from ScripData where ISIN=(?,?)", ("Test1", "Test2", ))
sqlite3.OperationalError: row value misused

CodePudding user response:

You can't use = to find matches for multiple values, you need to use IN.

objCursor.execute("select * from ScripData where ISIN IN (?,?)", ("Test1", "Test2" ))
arrResult = objCursor.fetchall()
  • Related