Use python to query Oracle database

Need to use the library: cx_Oracle

Use pip to install

pip install cx_Oracle

After completing the installation of cx_Oracle, you can almost operate Oracle according to the idea of ​​connecting to MySQL.

import cx_Oracle
conn = cx_Oracle.connect('user', 'passwd', 'ipadress')cursor = conn.cursor()sql = 'select * from test_table'cursor.execute(sql)res = cursor.fetchall()

The above is the simplest example, but there are a few points to note.

  • Do not add; at the end of the sql statement, because it will be added automatically to avoid errors.

  • The result returned by res is a list, a list, and each element of the list is a tuple, which records each row of data that is queried.


The idea of ​​using python to operate major databases is very simple and clear. The main content of each third-party library connected to the database is to realize the communication with each database server. What we need is to send the SQL statement to the server completely, and then the server will return the queried data to the local, and then we can directly use the obtained data.


Whether python connects to MySQL or python connects to Oracle, the essence is the same. What we need is a tool that can talk to the database server, and other parts can be treated as normal data operations.


For example, the above four lines of code can complete all operations on the database without using other library functions.



Guess you like

Origin blog.51cto.com/15069488/2578546