Facing issue to Insert data into table in which column name start with number using python and 'INSERT' query

448 views Asked by At

I am facing problem to insert pandas dataframe into Postgres table, here column name starting with Number like this - 03APH13W04D001. table contains 72 such field , out of that I want to insert dataframe in only 6 columns. Dataframe column length is 6. This is my code :

 table='uaq_sec13_stream03'
 # dataframe = df13_03_00_00
 tpls = [tuple(x) for x in df13_03_00_00.to_numpy()] 
 cols = ','.join(list(df13_03_00_00.columns))
 sql = "INSERT INTO %s(date_time,'03APH13W04D001','03AOR13W04D001','03ATU13W04D001','03ACT13W04D001','03TIT13W04D001') VALUES(%%s,%%s,%%s,%%s,%%s,%%s)" % (table, cols)
 cur.executemany(sql, tpls)

Now I have chnaged thge querystring. sql = """INSERT INTO %s(date_time,"03APH13W04D001","03AOR13W04D001","03ATU13W04D001","03ACT13W04D00","03TIT13W04D001") VALUES(%%s,%%s,%%s,%%s,%%s,%%s)""" % (table, cols)

Now I am getting this error TypeError: not all arguments converted during string formatting Error : psycopg2.errors.SyntaxError: syntax error at or near "03" LINE 1: INSERT INTO uaq_sec13_stream03(date_time,03APH13W04D001,03AO...

0

There are 0 answers