(1) Preface
This article explains how to connect to Oracle, MySQL, sqlserver, execute sql, and get query results.
(2) DB-API
DB-API articulates a set of standards for the required objects and database access mechanisms.
If the module that operates the database in Python follows the DB-API standard (which should all follow this standard), the names and functions of the functions and methods should be similar (the following tables list some of the contents), that is, the parameters passed may be a little different. .
connect function
The connect function accesses the database through the connection object. This function creates and returns a connection object.
parameter |
illustrate |
host |
The access address of the database instance (for example: IP\instance name) |
user |
username |
password |
password |
database |
data storage name |
connection object
method |
illustrate |
close() |
close database connection |
commit() |
commit transaction |
rollback() |
cancel transaction |
cursor() |
Create and return a cursor object (or cursor-like object) using the connection |
cursor object
(The following three are the most important ones)
method |
illustrate |
execute() |
execute SQL |
fetchall() |
Get the query result (after executing the select statement) |
callproc() |
call stored procedure |
abnormal
abnormal |
illustrate |
DataError |
There was a problem processing the data |
OperationalError |
A problem occurred during the execution of the database operation |
IntegrityError |
Database relational integrity error |
ProgrammingError |
SQL execution failed |
(3) Module installation
Oracle:pip install cx_Oracle
Mysql: pip install pymysql
sql server: https://www.lfd.uci.edu/~gohlke/pythonlibs/ Download pymssql, then pip install xxx.whl. (pip install pymssql can't be installed in this way, and the above two can be installed in this way if they can't be installed)
(4) Oracle example
As can be seen from the following three examples, the code is basically the same, but the imported modules are different (other databases should be similar).
import cx_Oracle #Connect to the database, parameters: username/password@server ip: port number/instance name conn = cx_Oracle.connect( ' py/[email protected]:1521/orcl ' ) #Create a cursor object cur = conn.cursor () #Execute SQL cur.execute( " insert into test_py t values('1005','ZS','Zhang San','123456') " ) cur.execute( " update test_py t set t.user_name='Li Si' where t.id='123' " ) #Commit transaction conn.commit() #Execute SQL cur.execute( " select * from test_py " ) # Get the query result row = cur.fetchall() print (row) #Close the database connection conn.close()
(5) MySql example
1 import pymysql 2 3 #Connect to the database, host: server ip user: username password: password database: database name 4 conn = pymysql.connect(host= ' 192.168.4.196 ' , user= ' root ' , password= ' password ' , database= ' test ' ) 5 #Create a cursor object 6 cur = conn.cursor() 7 #Execute SQL 8 cur.execute( " insert into test_py(id,user_account) values('100','admin') " ) 9 cur.execute(" update test_py set user_account = 'test6' where id='123' " ) 10 #Commit transaction 11 conn.commit() 12 #Execute SQL 13 cur.execute( " select * from test_py " ) 14 #Get query result 15 row = cur.fetchall() 16 print (row) 17 #Close the database connection 18 conn.close()
(6) Sql server example
import pymssql #Connect to the database, host: server ip\\ instance name user: username password: password database: database name conn = pymssql.connect(host= ' 192.168.4.196\\amsys ' , user= ' sa ' , password= ' 123 ' , database= ' test ' ) #Create a cursor object cur = conn.cursor() #Execute SQL cur.execute( " insert into test_py(id,user_account) values('100861','admin') " ) cur.execute( " update test_py set user_account = 'test6' where id='123' " ) #commit transaction conn.commit() #execute SQL cur.execute( " select * from test_py " ) #get query result row = cur .fetchall() print (row) #Close the database connection conn.close()