Normally, if I need to do something, I just google it on the internet. Let me give you a concrete example. There was a time when I want to access an Oracle database using python. I typed "python Oracle" on the google search engine and this is the result:
-----------------------------------------
Python - Oracle Wiki
2 posts - 1 author - Last post: 3 Feb 2010 Getting Started: #!/usr/bin/python import cx_Oracle connstr='scott/tiger' conn = cx_Oracle.connect(connstr) curs = conn.cursor() ...wiki.oracle.com/page/Python |
cx_Oracle
cx_Oracle is a Python extension module that allows access to Oracle databases and conforms to the ... Windows amd64 Installer (Oracle 10g, Python 2.5) ... cx-oracle.sourceforge.net/ |
Using Python With Oracle Database 11g
With the rise of Frameworks, Python is also becoming common for Web application development. If you want to use Python and an Oracle database, this tutorial ... www.oracle.com/technetwork/articles/dsl/python-091105.html |
Python - Oracle FAQ
5 Nov 2010 ... cx_Oracle is a Python extension module that allows access to Oracle databases and conforms to the Python database API specification. ... www.orafaq.com/wiki/Python ------------------------------------------------------------------- #!/usr/bin/python import cx_Oracle connstr='scott/tiger' conn = cx_Oracle.connect(connstr) curs = conn.cursor() curs.arraysize=50 curs.execute('select 2+2 "aaa" ,3*3 from dual') print curs.description print curs.fetchone() conn.close() Resources: - Python Programming Language - Official Website - cx_Oracle module for accessing Oracle Database from Python (DB API 2.0 conformant, updated for 11g) - Pydev, a superb IDE for Python built on top of Eclipse - Mod_python, Apache/Python Integration, introducing Python Server Pages technology - Python Package Index - official repository for 3rd party modules - comp.lang.python - active, helpful community of Python experts - Essbase.py - port of Essbase perl module to ----------------------------------------------------------------------------- This tells me that I need the cx_Oracle module (because of the 'import' statement at the start). So I checked via the python interpreter if I have this module. >>> import cx_Oracle Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named cx_Oracle Ok, I don't have it; I need to get this module and install it. Again, from the first site that I clicked, it has a link on cx_Oracle under "Resources" (please see above). So, I clicked on it and saw several windows installer for different versions of Oracle. I selected the one compatible to our Oracle server and installed it. Now, importing that module from the python interpreter no longer issue any error. Now, I need more documentation. Going back to google result, I selected "Python-Oracle FAQ" and saw these code snippets: --------------------------------------------------------------------------- python >>> import cx_Oracle >>> connection = cx_Oracle.connect('scott/tiger@orcl') >>> # do some stuff here >>> connection.close() connection = cx_Oracle.connect("uid/pwd@database") cursor = connection.cursor() cursor.execute("SELECT COUNT(*) FROM User_Tables") count = cursor.fetchall()[0][0] cursor.close() connection.close() connection = cx_Oracle.connect("uid/pwd@database") cursor = connection.cursor() cursor.arraysize = 50 cursor.execute(""" select Col1, Col2, Col3 from SomeTable where Col4 = :arg_1 and Col5 between :arg_2 and :arg_3""", arg_1 = "VALUE", arg_2 = 5, arg_3 = 15) for column_1, column_2, column_3 in cursor.fetchall(): print "Values from DB:", column_1, column_2, column_3 cursor.close() connection.close()-------------------------------------------------- Well, there are a lot more but you get the idea. If you don't know SQL, these code snippets may not be enough for you. Fortunately, in my case, I know SQL and these snippets are enough to keep me going. Now, if you don't have administrative rights, you would not be able to install the module at the default location where python expects to find it. To solve this problem, you have to include the installation folder in your PYTHONPATH so that python would find it. To change your PYTHONPATH, you could either convince your administrator to append the installation folder at the end of this environment variable (good luck with that!) or dynamically change PYTHONPATH in your script as follows: import sys sys.path.append(r'/path/to/cx_Oracle') import cx_Oracle sys.path is actually a list containing folders where python would search for modules |
No comments:
Post a Comment