尝试将变量从 pandas 传递到 sql

Trying to pass variable from pandas to sql

我正在尝试将一个简单的变量(字符串,列出我可以工作的任何内容)传递给 sql(使用 jaydebapi 到 AWS Redshift 集群。

python/pandas 代码目前看起来像这样。我认为 %s 是问题的一部分:

id = (261894)
df = pd.read_sql_query(
'''
select * from purchases where customer_id = %s
''',
conn, params=[id])

例外是

ava.sql.SQLExceptionPyRaisable           Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\sql.py in execute(self, *args, **kwargs)
   1377             else:
-> 1378                 cur.execute(*args)
   1379             return cur

C:\ProgramData\Anaconda3\lib\site-packages\jaydebeapi\__init__.py in execute(self, operation, parameters)
    497         self._close_last()
--> 498         self._prep = self._connection.jconn.prepareStatement(operation)
    499         self._set_stmt_parms(self._prep, parameters)

java.sql.SQLExceptionPyRaisable: java.sql.SQLException: [Amazon](500310) Invalid operation: syntax error at or near "%" 
Position: 70;

During handling of the above exception, another exception occurred:

java.sql.SQLExceptionPyRaisable           Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\jaydebeapi\__init__.py in rollback(self)
    416         try:
--> 417             self.jconn.rollback()
    418         except:

java.sql.SQLExceptionPyRaisable: java.sql.SQLException: [Amazon][JDBC](11680) Cannot use rollback while Connection is in auto-commit mode.

During handling of the above exception, another exception occurred:

DatabaseError                             Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\sql.py in execute(self, *args, **kwargs)
   1381             try:
-> 1382                 self.con.rollback()
   1383             except Exception:  # pragma: no cover

C:\ProgramData\Anaconda3\lib\site-packages\jaydebeapi\__init__.py in rollback(self)
    418         except:
--> 419             _handle_sql_exception()
    420 

C:\ProgramData\Anaconda3\lib\site-packages\jaydebeapi\__init__.py in _handle_sql_exception_jpype()
    155         exc_type = InterfaceError
--> 156     reraise(exc_type, exc_info[1], exc_info[2])
    157 

C:\ProgramData\Anaconda3\lib\site-packages\jaydebeapi\__init__.py in reraise(tp, value, tb)
     56         if tb:
---> 57             raise value.with_traceback(tb)
     58         raise value

C:\ProgramData\Anaconda3\lib\site-packages\jaydebeapi\__init__.py in rollback(self)
    416         try:
--> 417             self.jconn.rollback()
    418         except:

DatabaseError: java.sql.SQLException: [Amazon][JDBC](11680) Cannot use rollback while Connection is in auto-commit mode.

During handling of the above exception, another exception occurred:

DatabaseError                             Traceback (most recent call last)
 in 
      5 select * from purchases where customer_id = %s
      6 ''',
----> 7 conn, params=[id])
      8 

C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\sql.py in read_sql_query(sql, con, index_col, coerce_float, params, parse_dates, chunksize)
    312     return pandas_sql.read_query(
    313         sql, index_col=index_col, params=params, coerce_float=coerce_float,
--> 314         parse_dates=parse_dates, chunksize=chunksize)
    315 
    316 

C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\sql.py in read_query(self, sql, index_col, coerce_float, params, parse_dates, chunksize)
   1411 
   1412         args = _convert_params(sql, params)
-> 1413         cursor = self.execute(*args)
   1414         columns = [col_desc[0] for col_desc in cursor.description]
   1415 

C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\sql.py in execute(self, *args, **kwargs)
   1384                 ex = DatabaseError("Execution failed on sql: %s\n%s\nunable"
   1385                                    " to rollback" % (args[0], exc))
-> 1386                 raise_with_traceback(ex)
   1387 
   1388             ex = DatabaseError(

C:\ProgramData\Anaconda3\lib\site-packages\pandas\compat\__init__.py in raise_with_traceback(exc, traceback)
    402         if traceback == Ellipsis:
    403             _, _, traceback = sys.exc_info()
--> 404         raise exc.with_traceback(traceback)
    405 else:
    406     # this version of raise is a syntax error in Python 3

C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\sql.py in execute(self, *args, **kwargs)
   1380         except Exception as exc:
   1381             try:
-> 1382                 self.con.rollback()
   1383             except Exception:  # pragma: no cover
   1384                 ex = DatabaseError("Execution failed on sql: %s\n%s\nunable"

C:\ProgramData\Anaconda3\lib\site-packages\jaydebeapi\__init__.py in rollback(self)
    417             self.jconn.rollback()
    418         except:
--> 419             _handle_sql_exception()
    420 
    421     def cursor(self):

C:\ProgramData\Anaconda3\lib\site-packages\jaydebeapi\__init__.py in _handle_sql_exception_jpype()
    154     else:
    155         exc_type = InterfaceError
--> 156     reraise(exc_type, exc_info[1], exc_info[2])
    157 
    158 def _jdbc_connect_jpype(jclassname, url, driver_args, jars, libs):

C:\ProgramData\Anaconda3\lib\site-packages\jaydebeapi\__init__.py in reraise(tp, value, tb)
     55             value = tp(value)
     56         if tb:
---> 57             raise value.with_traceback(tb)
     58         raise value
     59 

C:\ProgramData\Anaconda3\lib\site-packages\jaydebeapi\__init__.py in rollback(self)
    415     def rollback(self):
    416         try:
--> 417             self.jconn.rollback()
    418         except:
    419             _handle_sql_exception()

DatabaseError: Execution failed on sql: 
select * from purchases where customer_id = %s

java.sql.SQLException: [Amazon](500310) Invalid operation: syntax error at or near "%" 
Position: 70;
unable to rollback
[39]



id = (261894)
[44]

关于如何让它工作有什么想法吗?我需要替代 %s 吗?

试试这个:

id = (261894) 
df = pd.read_sql_query( ''' select * from purchases where customer_id = ? ''', conn, params=[id])

它应该有效