(1), download cx_oracle compilation package
If you want to use a compiled package, choose what version you can pay attention to. If your machine already has Oracle clients installed, look at the operating system's x64 or i386 architecture, see the version of the Oracle client installed on the machine (10g or 11g, 64bit or 32bit), and finally look at your Python version ( 2.7 or 2.6, 64bit or 32bit). The cx_oracle version must be selected. Otherwise, it cannot be used even after installation.
(2), download cx_oracle and follow the download cx_oracle
Http://sourceforge.net/projects/cx-oracle/files/Select a version, such as 5.1.2, (here choose Source installation), download cx_oracle-5.1.2.tar.gz
installation, please refer to how to install a Third-party library: http://blog.csdn.net/cdl2008sky/article/details/10382663
(4), establish connection, query and query space data conversion, variable binding, bulk deletion.
#!/usr/bin/python #-*-Coding:utf-8-*-' Created on 2013-9-5 @author: CHENLL ' ' Import os,sys import cx_oracle rel Oad (SYS) sys.setdefaultencoding (' utf-8 ') user = "map13spring" password = "123456" url = "192.168.9.104:1521/orcl" con = N
One #获取数据库连接 def getconn (): Global con;
Con = cx_oracle.connection (user, password, URL);
Return con #关闭数据库连接 def closeconn (): If con!= None:con.close ();
#查询数据, processing Oracle Spatial's Geoloc field needs to be converted Def query (): Global con; cursor = Con.cursor () #注意: Python cannot parse Geoloc objects directly, only convert Cx_oracle.object objects to XML or GML cursor.execute (' SELECT id,kind, Width,pathname,to_char (Sdo_util.
To_gmlgeometry (Geoloc)) Geoloc from rbeijing where PATHNAME to NOT null and rownum<10 ') fields = [];
For i in Cursor.description: #数据字段及类型 fields.append (i[0]);
#所有结果值 list = [];
For row in cursor:i = 0;
#<key,value> < field, value > Fieldmap = {}; For field in FIelds:fieldmap[field] = Row[i] List.append (FIELDMAP);
i = i+1;
#根据字段名取值 for item in List:print item[' Geoloc '] print item[' ID '] pathName = item[' PathName ']
If PathName!= none:print pathname.decode (' GBK ') cursor.close ();
#绑定变量的查询 def Querypara (): Global con; cursor = Con.cursor () Nameparames = {' id ': 195699} cursor.execute (' SELECT Id,kind,width,pathname,to_char (sdo_util.t
O_gmlgeometry (Geoloc)) Geoloc from rbeijing where Id=:id ', nameparames);
#检查目前已指定的绑定变量 print cursor.bindnames (); For row in Cursor:print row #准备语句, the prepared statement can execute any cursor.prepare (' SELECT Id,kind,width,pathname,to_char (sdo_ UTIL. To_gmlgeometry (Geoloc)) Geoloc from rbeijing where id=:id ') row = Cursor.execute (None, {' ID ': 195699}) row = cursor
Execute (None, {' id ': 195663}) row = Cursor.execute (none, {' id ': 195657}) Cursor.close (); #插入多行 Executemany
Def insermanyrow (): Global con;
cursor = Con.cursor () #存放需要插入到表中的值 values = [] values.append (195657, ' Zhong Ding Road ');
Values.append (195663, ' s322/Yellow Road/South six Ring Road '));
Values.append ((195699, ' II distributors '));
Cursor.prepare ("INSERT into rbeijing (id,pathname) VALUES (: 1,:2)");
Cursor.executemany (None, values);
Cursor.close ();
Con.commit ();
def main (): con = Getconn ();
Query ();
Querypara ();
Closeconn (); if __name__ = = ' __main__ ': Main ();
PS: field type corresponding relationship
Oracle |
Cx_oracle |
Python |
VARCHAR2 NVARCHAR2 LONG |
Cx_oracle.string |
Str |
CHAR |
Cx_oracle.fixed_char |
Number |
Cx_oracle.number |
Int |
FLOAT |
Float |
DATE |
Cx_oracle.datetime |
Datetime.datetime |
TIMESTAMP |
Cx_oracle.timestamp |
Clob |
Cx_oracle.clob |
Cx_oracle.lob |
Blob |
Cx_oracle.blob |