1
from pandas.io import sql

print sql.execute('SELECT * FROM testTable WHERE testColumn IN ("테스트1", "테스트2")', engine).fetchall()

When I try the test code above, it's works like a charm.

But because of the reason that the query should work dynamically, somehow I did think about the code below(But not works).

I think the problem related with escape character of unicode, but I have no idea how could I solve this problem despite of searching & trying many cases in stackoverflow.

testss = [u"테스트1", u"테스트2"]
print sql.execute('SELECT * FROM testTable WHERE testColumn IN {}'.format(tuple(tests)), engine).fetchall()

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) near "'\uc774\ud130\ub110 \uc120\uc0e4\uc778'": syntax error [SQL: "SELECT * FROM boxOffice WHERE movieNm IN (u'\\ud14c\\uc2a4\\ud2b81', u'\\ud14c\\uc2a4\\ud2b82')"]

If possible, please let me know how could it be applicable to read_sql_query, or read_sql related with sqlalchemy.text().

Any advice would be most welcome! Thank you for your help.

cf. http://pandas.pydata.org/pandas-docs/stable/io.html#sql-queries

1 Answer 1

1
print sql.execute('SELECT * FROM testTable WHERE testColumn IN ("{}")'.format(u'", "'.join(tests).encode('utf-8')), engine).fetchall()
print pd.read_sql_query('SELECT * FROM testTable WHERE testColumn IN ("{}")'.format(u'", "'.join(tests).encode('utf-8')), engine)

Someone posted very close to the correct answer but deleted soon. Anyway I tweaked that code and both above worked fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.