Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中为Oracle数据库执行SQL*Plus.SQL文件_Python_Python 3.x_Oracle_Cx Oracle - Fatal编程技术网

在Python中为Oracle数据库执行SQL*Plus.SQL文件

在Python中为Oracle数据库执行SQL*Plus.SQL文件,python,python-3.x,oracle,cx-oracle,Python,Python 3.x,Oracle,Cx Oracle,大家好,我有这个sql文件: Student.sql 使用cx_oracle pip 任何帮助都可以在代码中定义查询,而不是执行文件 import cx_Oracle dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') #if needed, place an 'r' before any parameter in order to address any special ch

大家好,我有这个sql文件:

Student.sql

使用cx_oracle pip
任何帮助都可以在代码中定义查询,而不是执行文件

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') #if needed, place an 'r' before any parameter in order to address any special character such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) #if needed, place an 'r' before any parameter in order to address any special character such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
query = """
        Select * from student where age > 18;
        DeletE student_ name , studen_id if age > 18 
        """
c = conn.cursor()
c.execute(query) # use triple quotes if you want to spread your query across multiple lines
print('Result', c)
#conn.close()

正如其他回复所指出的,使用cx_Oracle一次只能执行一条语句。但是,您可以编写一个包装器来读取SQL文件并执行每条语句。如果您限制SQL语法,特别是关于行终止符的语法,这将容易得多。例如,请参见

您可以在Oracle中运行以下查询以获取服务名称:选择sys_context'userenv',dual中的'Service_Name'。您还可以在Oracle中运行以下查询以获取用户列表:从dba_users中选择username感谢您的快速回答,但我有400行代码。。我只是想,是否有可能执行file.sql direct.:f=openrun\u all\u su.sql full\u sql=f.read sql\u commands=full\u sql.split';'sql_commands=full_sql.replace'\n',.split';'[:-1]这只带来了[]这是空的printsql\u命令,用于sql\u命令中的sql\u命令:printsql\u命令cur.executesql\u命令您不能在Python解释器中直接执行sql代码。Python解释器不是SQL引擎。您通常需要将其包装成一个字符串,然后将该字符串发送给SQL引擎执行。有像cx_Oracle这样的库可以做到这一点。我知道这一点,但通过cx_Oracle,我可以找到.sql和execut,但我不确定您当时在问什么,因为您的问题似乎是python中的execute.sql文件。如果你知道你做不到,为什么要问?也许更新你的问题,问你想知道什么。
import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') #if needed, place an 'r' before any parameter in order to address any special character such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) #if needed, place an 'r' before any parameter in order to address any special character such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
query = """
        Select * from student where age > 18;
        DeletE student_ name , studen_id if age > 18 
        """
c = conn.cursor()
c.execute(query) # use triple quotes if you want to spread your query across multiple lines
print('Result', c)
#conn.close()