Create table in Redshift through db_query() in Python

29 views Asked by At

I'm trying to create a table in Redshift using db_query() with the following code:

snapshot_date = '20240325'
sql = """
create table tablename""" + snapshot_date + """ as (select * from tablename)
"""

data = db_query('redshift', sql)

And I'm getting the following error:

TypeError                                 Traceback (most recent call last)
<ipython-input-3-ff895c5014e5> in <module>
      3 """
      4 
----> 5 data = db_query('redshift', sql)

~/premium-analysis/notebooks/../src/data/db_connect.py in db_query(db_name, query, conn_kwargs, verbose, load_file, save_file, params, chunk_size, **kwargs)
    161 
    162         if params is None:
--> 163             df = sql_query(conn, query, **kwargs)
    164 
    165         else:

~/premium-analysis/notebooks/../src/data/db_connect.py in sql_query(conn, query, params, **kwargs)
    234     """
    235     try:
--> 236         df = pd.read_sql(query, conn, params=params, **kwargs)
    237         return df
    238 

~/anaconda3/lib/python3.8/site-packages/pandas/io/sql.py in read_sql(sql, con, index_col, coerce_float, params, parse_dates, columns, chunksize)
    600 
    601     if isinstance(pandas_sql, SQLiteDatabase):
--> 602         return pandas_sql.read_query(
    603             sql,
    604             index_col=index_col,

~/anaconda3/lib/python3.8/site-packages/pandas/io/sql.py in read_query(self, sql, index_col, coerce_float, params, parse_dates, chunksize, dtype)
   2115         args = _convert_params(sql, params)
   2116         cursor = self.execute(*args)
-> 2117         columns = [col_desc[0] for col_desc in cursor.description]
   2118 
   2119         if chunksize is not None:

TypeError: 'NoneType' object is not iterable

I understand db_query is used to iterate over the resultset. Is there any way I can use it to create a table and not return a resultset?

0

There are 0 answers