Search code examples
sqlsql-serverpython-2.7sqlobject

SqlObject and SqlBuilder IN() function with string values


as my first post here, I would like to warn that I looked as well as I could on the web but nothing that solved it.

I am using python 2.7 and latest version of sqlobject lib.

I faced that when calling the IN() function with a list or tuple of strings, you have a result that looks like an error:

from sqlobject.sqlbuilder import table, IN

t = table.MyTable
print(IN(t.s_ID, ("1", "2")))
>>> <SQLOp 34ec648L>

While with integers in seems to work better:

print(IN(t.s_ID, (1, 2)))
>>>> ((MyTable.s_ID) IN (1, 2))

Does anyone have an idea of why this doesn't work? I think more about an issue in the lib but maybe i'm missing something

Thanks in advance!

note: I don't consider it as important in this case (as I only want to build the query) but I wish to send this query to SQL Server 2012

D. FE.


Solution

  • Looks like both of your statements are correct. But representation of the 1st one (which includes strings) is database-dependent — it depends on how strings are presented in queries for each specific database (how they are quoted, escaped, etc). So it can't be converted to string without proper db-aware object, like connection or model — the object that has sqlrepr method. In the next example __connection__ object is a postgresql-connection:

    >>> print(__connection__.sqlrepr(IN(t.s_ID, ("1", "2")))
    ((MyTable.s_ID) IN ('1', '2'))