I have a dataframe testdata like this:
Here are the variables' types in Python:
detectorid:int64
starttime:str
volume:float64
speed:float64
occupancy:float64
Now I want to creat a datatable in oracle and insert this dataframe into it, here is what I tried:
import pandas as pd
import cx_Oracle
host = "192.168.1.100"
port = "1521"
sid = "orcl"
dsn = cx_Oracle.makedsn(host, port, sid)
conn = cx_Oracle.connect("scott", "tiger", dsn)
cursor = conn.cursor()
#creat datatable:
sql_creat = "create table portland(detectorid number(32), starttime varchar(32), volume number(32), speed number(32), occupancy number(32))"
cursor.execute(sql_creat)
query = "insert into portland (detectorid,starttime,volume,speed,occupancy) VALUES (%d,'%s',%f,%f,%f)"
#insert by rows:
for i in range(len(testdata)):
detectorid= testdata.ix[i,0]
starttime= testdata.ix[i,1]
volume= testdata.ix[i,2]
speed= testdata.ix[i,3]
occupancy= testdata.ix[i,4]
cursor.execute(query % (detectorid,starttime,volume,speed,occupancy))
conn.commit()
cursor.close()
conn.close()
However it gives me DatabaseError: ORA-00984:column not allowed here. I think there are something wrong about the columns' types in my sql statement but I don't know how to solve it. Could somebody give me some instructions? Thank you for your attention!
NUMBERdoesn't acceptNaN, you might have to look into the datatypeBINARY_DOUBLE, see this answer for instance.DATEor, if you have timezones,TIMESTAMP.number(32)intoBINARY_DOUBLEandVARCHAR(32)intoDATEbut still get the same error. Is myquery =code correct?