cx_Oracle的基本操作

具体的cx_oracle的api请查看:http://cx-oracle.sourceforge.net/html/index.html

首先开启开启oracle数据库,同时也要开启侦听端口,使用命令lsnrctl status产看,lsnrctl  start开启


import cx_Oracle db
=cx_Oracle.connect('system','dingjia','192.168.88.213/orcl11') #获取connection对象 cursor=db.cursor() #获取游标对象 create_table = """ create table python_modules( module_name VARCHAR2(50) NOT NULL, file_path VARCHAR2(300) NOT NULL ) """
#假如需要一个很大型的数据库进行测试,可以不断的产生随机数去模拟
如:
cursor.execute("create table department(username varchar2(30),password varchar2(30))")
count=0
while count<10000:
user=[ randint(10000000000,99999999999) for i in range(2)]
   cursor.execute("insert into department values(:1,:2)",user)
   count+=1
#通过这种方法就可以模拟出巨大的数据库出来,记得要db.commit(),提交事务,不然数据可能丢失。
from sys import modules
cursor.execute(create_table)
M= []
 for m_name, m_info in modules.items():
    try:
             M.append((m_name,m_info.__file__))
   except AttributeError:
            pass

len(M)
cursor.prepare("insert into python_modules(module_name, file_path) values(:1, :2)")
cursor.executemany(None,M)
db.commit()
r= cursor.execute(" select *from python_modules")
result = r.fetchall()
for i in result:
    print i
输出:
('heapq', '/usr/lib/python2.7/heapq.pyc')
('distutils', '/usr/lib/python2.7/distutils/__init__.pyc')
('functools', '/usr/lib/python2.7/functools.pyc')
('datetime', '/usr/lib/python2.7/lib-dynload/datetime.so')
('sysconfig', '/usr/lib/python2.7/sysconfig.pyc')
('distutils.dep_util', '/usr/lib/python2.7/distutils/dep_util.pyc')
('collections', '/usr/lib/python2.7/collections.pyc')
('string', '/usr/lib/python2.7/string.pyc')
('encodings.utf_8', '/usr/lib/python2.7/encodings/utf_8.pyc')
('bisect', '/usr/lib/python2.7/bisect.pyc')
('decimal', '/usr/lib/python2.7/decimal.pyc')
('threading', '/usr/lib/python2.7/threading.pyc')
('distutils.log', '/usr/lib/python2.7/distutils/log.pyc')
('google', '/usr/lib/python2.7/dist-packages/google/__init__.pyc')
('locale', '/usr/lib/python2.7/locale.pyc')
('encodings', '/usr/lib/python2.7/encodings/__init__.pyc')
('abc', '/usr/lib/python2.7/abc.pyc')
('re', '/usr/lib/python2.7/re.pyc')
('UserDict', '/usr/lib/python2.7/UserDict.pyc')
('lazr', '/usr/lib/python2.7/dist-packages/lazr/__init__.pyc')
('codecs', '/usr/lib/python2.7/codecs.pyc')
('posixpath', '/usr/lib/python2.7/posixpath.pyc')
('traceback', '/usr/lib/python2.7/traceback.pyc')
('pkg_resources', '/usr/local/lib/python2.7/dist-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.pyc')
('weakref', '/usr/lib/python2.7/weakref.pyc')
('distutils.spawn', '/usr/lib/python2.7/distutils/spawn.pyc')
('os', '/usr/lib/python2.7/os.pyc')
('__future__', '/usr/lib/python2.7/__future__.pyc')
('distutils.util', '/usr/lib/python2.7/distutils/util.pyc')
('_heapq', '/usr/lib/python2.7/lib-dynload/_heapq.so')
('pkgutil', '/usr/lib/python2.7/pkgutil.pyc')
('sre_constants', '/usr/lib/python2.7/sre_constants.pyc')
('os.path', '/usr/lib/python2.7/posixpath.pyc')
('copy', '/usr/lib/python2.7/copy.pyc')
('keyword', '/usr/lib/python2.7/keyword.pyc')
('apport_python_hook', '/usr/lib/python2.7/dist-packages/apport_python_hook.pyc')
('encodings.aliases', '/usr/lib/python2.7/encodings/aliases.pyc')
('sre_parse', '/usr/lib/python2.7/sre_parse.pyc')
('copy_reg', '/usr/lib/python2.7/copy_reg.pyc')
('sre_compile', '/usr/lib/python2.7/sre_compile.pyc')
('site', '/usr/lib/python2.7/site.pyc')
('numbers', '/usr/lib/python2.7/numbers.pyc')
('cx_Oracle', '/home/hzhida/.python-eggs/cx_Oracle-5.1.2-py2.7-linux-x86_64.egg-tmp/cx_Oracle.so')
('linecache', '/usr/lib/python2.7/linecache.pyc')
('_abcoll', '/usr/lib/python2.7/_abcoll.pyc')
('zope', '/usr/lib/python2.7/dist-packages/zope/__init__.pyc')
('genericpath', '/usr/lib/python2.7/genericpath.pyc')
('stat', '/usr/lib/python2.7/stat.pyc')
('warnings', '/usr/lib/python2.7/warnings.pyc')
('readline', '/usr/lib/python2.7/lib-dynload/readline.so')
('types', '/usr/lib/python2.7/types.pyc')
('sitecustomize', '/usr/lib/python2.7/sitecustomize.pyc')
('distutils.errors', '/usr/lib/python2.7/distutils/errors.pyc')
('_weakrefset', '/usr/lib/python2.7/_weakrefset.pyc')
posted @ 2012-08-13 17:20  苍术厚朴  阅读(16084)  评论(0编辑  收藏  举报