Python connection to the Oracle database and return the results to a dictionary

As we all know, Python connect to Oracle databases typically use cx_Oracle this package.

But the key is cx_Oracle this package, the returned results are tuples or lists.

As the following code:

 1 import cx_Oracle
 2 import os
 3 
 4 os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
 5 
 6 connection = cx_Oracle.connect("Booker", "123456", "172.18.240.31:1521/Book")
 7 
 8 cursor = connection.cursor()
 9 cursor.execute(" select * from book",)
10 
11 a = cursor.fetchall()
12 for c in a:
13     print(c)
14 
15 cursor.close()
16 connection.close()

Returns the result as follows:

( ' PHP ' ,   ' yellow vigorously ' , ' alien publishing house ' , ' 34 ' )

This is a tuple. Unable to return to the dictionary format contains the field names.

Check out the official documentation cx_Oracle: https://oracle.github.io/python-cx_Oracle/

But could not find the appropriate method.

For such people I have obsessive-compulsive disorder, this is unacceptable.

You can not have other solutions? Looking for a bit and found cursor has a property description, is a description of the fields, print to see what the results:

print(cursor.description)

It is shown below:

[ ('book_name', <class 'cx_Oracle.STRING'>, 300, 900, None, None, 1),, ('author', <class 'cx_Oracle.STRING'>, 300, 900, None, None, 1), ('public', <class 'cx_Oracle.STRING'>, 300, 900, None, None, 1), ('price', <class 'cx_Oracle.STRING'>, 300, 900, None, None, 1)]

Is a list, and each field is a tuple, the first element of the tuple is the field name. This is easier to handle.

for d in cursor.description:
    print(d)

Output:

('book_name', <class 'cx_Oracle.STRING'>, 20, 60, None, None, 0)
('author', <class 'cx_Oracle.DATETIME'>, 23, None, None, None, 1)
('public', <class 'cx_Oracle.STRING'>, 20, 60, None, None, 1)
('price', <class 'cx_Oracle.STRING'>, 300, 900, None, None, 1)

Ah, right, each element is a tuple, we can [0] field name taken out by d.

for d in cursor.description:
    print(d[0])

Output:

book_name
author
public
price

That way, we can use a for loop, taken out field names. The following this statement, is in cursor.description cycle, remove the first element, and then form a list.

cols = [d[0] for d in cursor.description]
print(cols)

Output:

['book_name', 'author', 'public', 'price']

So, how and query results combined into a dictionary it? Ah, the use of zip functions.

zip () function is used as a parameter iterables, the corresponding element of the object packed into a tuple, and return the object of these tuples.

zip () function syntax:

zip(iterable1,iterable2, ...)

Parameter Description:

  • iterable - one or more iterations objects (strings, lists, tuples, dictionaries)

Yes, strings, lists, tuples, dictionaries are iterable.

zip function like a zipper look, the two elements correspond iterables up. If the number of elements of the various iterables inconsistent, then the object is the same as the shortest length of the returned object may be iterative.

Use the following code, Dictionary field name, and the results can be formed.

 1 import cx_Oracle
 2 import os
 3 
 4 os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
 5 
 6 connection = cx_Oracle.connect("Booker", "123456", "172.18.240.31:1521/Book") 
 7 cursor = connection.cursor() cursor.execute("select * from book")  
 8 a = cursor.fetchall() 
 9 cols = [d[0] for d in cursor.description] 
10 print(cols) 
11 for row in a:
12   b = dict(zip(cols, row)) 
13   print(b['book_name']) 
14 cursor.close() 
15 connection.close()

Output:

PHP

So that we can use the b [ 'Field Name'] way into a variety of operations, and clear and convenient.

 

Guess you like

Origin www.cnblogs.com/dhanchor/p/11111247.html