当我批量插入oracle使用这样的python时,它会抛出错误:
DatabaseError: ORA-00928: missing SELECT keyword
我尝试在每个循环中只插入一行,它仍然会抛出此错误。 在我的oracle中,我使用序列使列id自身增加,然后在这里我不向id插入数据,我认为没有必要为列id插入值,我不知道这是否为真。我非常需要你的帮助!
db = cx_Oracle.connect("*","*","*" ) # 打开数据库连接
print db
cursor = db.cursor() # 使用cursor()方法获取操作游标
begin=datetime.date(2017,1,1)
end=datetime.date(2017,1,5)
result=[]
for i in range((end-begin).days+1):
print i
day=begin+datetime.timedelta(days=i)
a=(str(day),1,1,day.strftime("%Y-%m-%d 00:00:00"),1,day.strftime("%Y-%m-%d 00:00:00"),0)
print a
result.append(a)
print result
sql = "INSERT INTO calendar(date,type,created_user,created_at,updated_user,updated_at,deleted) VALUES (:1, :2, :3, :4, :5, :6, :7)"
cursor.prepare(sql)
cursor.executemany(None,result) #执行SQL语句
答案 0 :(得分:0)
根据您的表格结构,我可以说明问题在于id
语句中INSERT
列的值不足。您没有为主键提供价值。您的INSERT
应该使用例如序列:
CREATE SEQUENCE sequence_name
START WITH 1
INCREMENT BY 1
NOCACHE
NOCYCLE;
INSERT INTO calendar(id, date,type,created_user,created_at,updated_user,updated_at,deleted)
VALUES (sequence_name.nextval, :1, :2, :3, :4, :5, :6, :7)
答案 1 :(得分:0)
cursor = db.cursor() # 使用cursor()方法获取操作游标
begin=datetime.date(2017,1,1)
end=datetime.date(2017,1,5)
result=[]
for i in range((end-begin).days+1):
print i
day=begin+datetime.timedelta(days=i)
a=(str(day),1,1,day.strftime("%Y-%m-%d 00:00:00"),1,day.strftime("%Y-%m-%d 00:00:00"),0)
print a
result.append(a)
print result
sql = "INSERT INTO factory_calendar (date,type,created_user,created_at,updated_user,updated_at,deleted) VALUES (seq_factory_calendar.nextval,:2, :3, :4, :5, :6, :7, :8)"
cursor.prepare(sql)
cursor.executemany(None,result) #执行SQL语句