具體的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')
