本文介绍了从cx_oracle执行sql脚本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在python中使用cx_oracle执行sql脚本文件。
Is there a way to execute a sql script file using cx_oracle in python.
我需要在sql文件中执行创建表脚本。
I need to execute my create table scripts in sql files.
推荐答案
p> ,cx_oracle尝试遵守真的有这样的方法。
PEP-249, which cx_oracle tries to be compliant with, doesn't really have a method like that.
但是,这个过程应该很简单。将文件的内容拉到一个字符串中,将其拆分成;字符,然后在生成的数组的每个成员上调用.execute。我假设;字符仅用于定界文件中的oracle SQL语句。
However, the process should be pretty straight forward. Pull the contents of the file into a string, split it on the ";" character, and then call .execute on each member of the resulting array. I'm assuming that the ";" character is only used to delimit the oracle SQL statements within the file.
f = open('tabledefinition.sql')
full_sql = f.read()
sql_commands = full_sql.split(';')
for sql_command in sql_commands:
curs.execute(sql_command)
这篇关于从cx_oracle执行sql脚本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!