여러 삽입 문 (1000 +)이있는 .sql 파일이 있고이 파일의 문을 Oracle 데이터베이스로 실행하고 싶습니다.파일에서 데이터베이스로 데이터를 입력하십시오.
import pyodbc
from ConfigParser import SafeConfigParser
def db_call(self, cfgFile, sql):
parser = SafeConfigParser()
parser.read(cfgFile)
dsn = parser.get('odbc', 'dsn')
uid = parser.get('odbc', 'user')
pwd = parser.get('odbc', 'pass')
try:
con = pyodbc.connect('DSN=' + dsn + ';PWD=' + pwd + ';UID=' + pwd)
cur = con.cursor()
cur.execute(sql)
con.commit()
except pyodbc.DatabaseError, e:
print 'Error %s' % e
sys.exit(1)
finally:
if con and cur:
cur.close()
con.close()
with open('theFile.sql','r') as f:
cfgFile = 'c:\\dbinfo\\connectionInfo.cfg'
#here goes the code to insert the contents into the database using db_call_many
statements = f.read()
db_call(cfgFile,statements)
을하지만 난 그것을 실행할 때 나는 다음과 같은 오류가 나타납니다 :
지금은, ODBC를 파이썬을 사용하는 메신저는 다음과 같이 내 데이터베이스에 연결하는
pyodbc.Error: ('HY000', '[HY000] [Oracle][ODBC][Ora]ORA-00911: invalid character\n (911) (SQLExecDirectW)')
그러나 모두를 파일의 내용은 다음과 같습니다
INSERT INTO table (movie,genre) VALUES ('moviename','horror');
편집
db_db_call (cfgFile, 문) 전에 print '<{}>'.format(statements)
추가 내가 얻을 결과 (100 +) :이 글을 읽고에 대한 당신의 시간에 대한
<INSERT INTO table (movie,genre) VALUES ('moviename','horror');INSERT INTO table (movie,genre) VALUES ('moviename_b','horror');INSERT INTO table (movie,genre) VALUES ('moviename_c','horror');>
감사합니다.
문제는 '명령문'에있는 것입니다 ... 저것을 보았습니까? (즉, SQL 엔진에서 해석 할 수 없습니다. point) –
더 많은 정보가있는 편집 주요 질문 –
open ('theFile.sql', 'rb') with with f :'? – unutbu