程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Database programming interface of Python operating database

編輯:Python

Catalog

One 、 Preface

Two 、 Connection object

1. Get the connection object

2. How to connect objects

3、 ... and 、 Cursor object

One 、 Preface

In 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 object

Database 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 object

How 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)

explain : 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 .

2. How to connect objects

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 object

Cursor 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 !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved