One 、 Preface
Two 、 Connection object
1. Get the connection object
2. How to connect objects
3、 ... and 、 Cursor object
One 、 PrefaceIn project development , Database application is essential . Although there are many kinds of databases , Such as SQLite、MySQL、Oracle etc. , But their functions are basically the same , For the unified operation of the database , Most languages offer simple 、 Standardized interfaces (API). stay Python Database API 2.0 Specification , Defined Python database API All parts of the interface , Such as module interface 、 Connection object 、 Cursor object 、 Type objects and constructors 、DB API Optional extension and optional error handling mechanism etc . This article will focus on the database connection object and cursor object .
Two 、 Connection objectDatabase connection object (Connection Object) It mainly provides getting database cursor object and submitting / How to roll back a transaction , And how to close the database connection .
1. Get the connection objectHow to get the connection object ? That's what you need to use connect() function . This function has more than one parameter , Which parameter to use specifically , Depends on the type of database used . for example , Need to access Oracle Database and MySQL database , It must be downloaded at the same time Oracle and MySQL Database module . When these modules get the connection object , You need to use connect() function .
connect() The common parameters and descriptions of the function are shown in the table below :
for example , Use PyMySQL Module connection MySQL database , The sample code is as follows :
import pymysqlconn = pymysql.connect( host="localhost", password="123456", db="test", charset="utf8", cursorclass=pymysql.cursors.DictCursor)
2. How to connect objectsexplain : In the above code ,pymysql.connect() The parameters used by the method are not exactly the same as those in the above table . When use , The specific database module shall prevail .
connect() Function returns the connection object , This object represents the current session with the database , The methods supported by connection objects are shown in the following table :
Method name Description close() Close database connection commit() Commit transaction rollback() Roll back the transaction cursor() Get cursor object , Operating the database , Such as implementation DML operation , Calling stored procedures, etc
Transactions are mainly used to process large amounts of data 、 Data with high complexity . If the operation is a series of actions , For example, chicken with vegetables transfers money to Xiaobai ,
There are the following 2 Operations :
The account amount of chicken with vegetables decreased
Li Si's account amount increases
At this time, using transactions can maintain the integrity of the database , Guarantee 2 Actions or all , Or none at all .
3、 ... and 、 Cursor objectCursor object (Cursor Object) Represents the cursor in the data , Used to indicate the context of the grab data operation . Mainly provides execution SQL sentence 、 Calling stored procedure 、 Get query results and other methods .
How to get cursor object ? By connecting objects cursor() Method , You can get the cursor object .
The properties of the cursor object are as follows :
description: Description of database column types and values .
rowcount: Statistics on the number of rows returned , Such as SELECT,UPDATE,CALLPROC etc. .
The method of cursor object is shown in the following table :
Method name Description callproc(procname,[,parameters]) Calling stored procedure , Need database support close() Close the current cursor execute(operation,[,parameters]) Perform database operations ,SQL Statement or database command executemany(operation,seq_of_params) For batch operation , Such as batch update fetchone() Get the next record of the query result fetchmany(size) Gets the specified amount of records fetchall() Get all the records of the result set nextset() Skip to the next available result set arraysize Specify the use of fetchmany() Number of rows retrieved , The default is 1setinputsizes(sizes) Set to call execute*() Method, the size of the allocated memory area setoutputsize(sizes) Set column buffer size , For big data columns, such as LONGS and BLOBS Especially useful
This is about Python This is the end of the article on database programming interface for operating database , More about Python For programming interface content, please search the previous articles of SDN or continue to browse the relevant articles below. I hope you will support SDN more in the future !