To access the database, the Python interpreter must install the database module. You have many options that comply with standardized API specifications and are familiar to anyone who has used ODBC or JDBC programmatically.
You will use cx_Oracle because it is easier to install. Download a Windows Installer that matches your Python and Oracle database versions. After cx_Oracle is installed, return to the Python command line interpreter for trial. Since cx_Oracle is a module independent from the core Python interpreter language, it must be imported before it is used in any session or script.
- >>> orcl = cx_Oracle.connect('scott/tiger@orcl')>>>
- curs = orcl.cursor()>>> sql = """CREATE TABLE INIT_PARAMS ...
- ( fileName VARCHAR2(30),... param VARCHAR2(64),...
- value VARCHAR2(512) )"""
Remember to use uppercase letters! Next, we will create a table for storing results.
- import readInitOra, cx_OracleinitParams = {}
- for fileName in ['init_orcl.ora', 'init_default.ora']:
- initParams[fileName] = readInitOra.read(fileName)orcl = cx_Oracle.
- connect('scott/tiger@orcl')curs = orcl.cursor()for fileName in initParams.keys():
- for param in initParams[fileName].keys():value = initParams[fileName][param]sql = """INSERT INTO INIT_PARAMS VALUES
- (:fileName, :param, :value)"""bindVars = {'fileName':fileName,
- 'param':param, 'value':value}curs.execute(sql, bindVars)curs.close()
- orcl.commit()
The above is all the code. Note that you used the Bind Variable in the SQL string this time and provided the value for them in a separate dictionary. Binding variables helps you get rid of SPCSP's troubles in preventing incorrect use of the Sharing pool Association. Retrieving results from a query is slightly more complex. After the cursor object is called execute.
You can use fetchone () to obtain a row at a time, or use fetchall () to obtain a list of all rows. In either case, each row adopts a byte group, that is, an ordered value sequence that can be accessed by a numerical index. For example, we will compile compareInitOra. py to print the init_orcl.ora PARAMETER that conflicts with the current value in V $ PARAMETER:
This script introduces some Python interpreter skills you have not seen before:
◆ Calling items () on the dictionary fileParams will return a list of key-value pairs. You can specify two loop variables in the for statement to traverse these key values.
◆ Call liveParams. get (param) works in a way similar to liveParams [param], except that if no parameter is found in liveParams, an error is returned-very similar to "ORA-01403: no data found in PL/SQL message. LiveParams. get (param) returns None if the liveParams parameter does not exist.
◆ The Python interpreter can use the % operator to execute string replacement. Similar to C's printf, % s indicates that a string value will be inserted at this point. These values are extracted from the byte group after % in sequence.
◆ The last line of code runs longer than you do without line breaks. Therefore, you use a backslash, this breaks the regular rule that the Python interpreter interprets line breaks as the end of a command.