我有一个带有多个插入语句(1000 +)的.sql文件,我想将这个文件中的语句运行到我的Oracle数据库中。从文件插入数据到数据库
现在,使用蟒蛇与ODBC IM与下面的连接到我的数据库:
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)
但是当我运行它,我收到以下错误:
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引擎在点) –
编辑主要问题与更多信息 –
如果您使用'with open('theFile.sql','rb')作为f:'? – unutbu