Python实战之Oracle数据库操作
1. 要想使Python可以操作Oracle数据库,首先需要安装cx_Oracle包,可以通过下面的地址来获取安装包
- http://cx-oracle.sourceforge.net/
- http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html
- $ sudo rpm -ivh oracle-instantclient11.2-basic-11.2.0.3.0-1.i386.rpm
- $ export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/lib/oracle/11.2/client/lib
- import cx_Oracle
- conn = cx_Oracle.connect('fkong/[email protected]/orcl')
- cursor = conn.cursor ()
- cursor.execute ("select * from dual")
- row = cursor.fetchone ()
- print row[0]
- cursor.close ()
- conn.close ()
- import cx_Oracle
- conn = cx_Oracle.connect('fkong/[email protected]/orcl')
- cursor = conn.cursor ()
- cursor.execute ("CREATE TABLE TEST(ID INT, COL1 VARCHAR(32), COL2 VARCHAR(32), COL3 VARCHAR(32))")
- cursor.execute ("INSERT INTO TEST (ID, COL1, COL2, COL3)VALUES(1, 'a', 'b', 'c')")
- cursor.execute ("INSERT INTO TEST (ID, COL1, COL2, COL3)VALUES(2, 'aa', 'bb', 'cc')")
- cursor.execute ("INSERT INTO TEST (ID, COL1, COL2, COL3)VALUES(3, 'aaa', 'bbb', 'ccc')")
- conn.commit()
- cursor.close ()
- conn.close ()
- import cx_Oracle
- conn = cx_Oracle.connect('fkong/[email protected]/orcl')
- cursor = conn.cursor ()
- cursor.execute ("SELECT * FROM TEST")
- rows = cursor.fetchall()
- for row in rows:
- print "%d, %s, %s, %s" % (row[0], row[1], row[2], row[3])
- print "Number of rows returned: %d" % cursor.rowcount
- cursor.execute ("SELECT * FROM TEST")
- while (1):
- row = cursor.fetchone()
- if row == None:
- break
- print "%d, %s, %s, %s" % (row[0], row[1], row[2], row[3])
- print "Number of rows returned: %d" % cursor.rowcount
- cursor.close ()
- conn.close ()
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16