好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

操作oracle 数据库 python

需要修改oracle的配置数据库 import cx_Oracle from Common.dir_config import caps_dir import yaml class DoSql: def do_orcal(self,query_sql,state= ‘ all ‘ ): # 1打开yaml文件 放置的是数据库的地址和密码用户名 fs = open(caps_dir+ " /oracle.yaml " ) # 2转换成python对象 db_config = yaml.load(fs, Loader= yaml.FullLoader) conn =cx_Oracle.connect(db_config[ ‘ db_config ‘ ]) # 游标cursor cursor= conn.cursor() # 写sql语句--是字符串 # 执行语句 cursor.execute(query_sql) # 获取结果,打印结果 if state==1 : res =cursor.fetchone() # 返回的是元祖 针对一条数据 else : res =cursor.fetchall() # 列表,针对多行数据,列表嵌套元祖 # 关闭游标 cursor.close() # 关闭连接 conn.close() return res if __name__ == ‘ __main__ ‘ : query_sql = ‘ select * from DEPT t ‘ print (DoSql().do_orcal(query_sql,1)[1]) # 取值

caps_dir.py  放oracle数据库信息的配置文件

 import   os
caps_dir  = os.path.join(base_dir, "  Desired_Caps  " )

oracle数据库的地址和密码放到配置文件(起名叫oracle.yaml):db_config: scott/tiger@localhost:1521/orcl    这个是安装上oracle 自身带的数据库  可以练习

 

操作oracle 数据库 python

标签:python   关闭   base   oracl   cursor   red   返回   hal   sql语句   

查看更多关于操作oracle 数据库 python的详细内容...

  阅读:28次