[Programmer technical skills] Learning a scripting language python (3) dealing with databases, Scripting Language python

Source: Internet
Author: User

[Programmer technical skills] Learning a scripting language python (3) dealing with databases, Scripting Language python

Next, the previous article describes how to use python to perform basic CRUD operations on the database. Here, we will describe sqlite3 as an example. Sqlite3 is a very lightweight database. It is very simple to install and use. I will not discuss it here.

In python, The sqlite3 module is provided for our use. The usage of this module is similar to that of the jdbc driver in JAVA, we can easily perform various operations with sqlite3.

 

Okay. Let's get started.

The python database module has a unified interface standard, and all operations have a unified mode, which is roughly divided into the following steps (assuming the sqlite3 module is used ):

 

The specific CRUD implementation code is as follows:

Import osimport sqlite3 # obtain a database connection based on the input path. if the path is invalid, create a temporary memory database def get_conn (path): conn = sqlite3.connect (path) if OS. path. exists (path) and OS. path. isfile (path): print ('database path is % s' % format (path) return conn else: conn = None return sqlite3.connect (': memory ')

 

# Retrieve the cursor def get_cursor (conn): if conn is not None: return conn. cursor () else: return get_conn (''). cursor ()

 

# Create table data def create_table (conn, SQL): if SQL is not None and SQL! = '': Cursor = get_cursor (conn) cursor.exe cute (SQL) conn. commit () close_all (conn, cursor) # Close the database connection def close_all (conn, cursor): try: if cursor is not None: cursor. close () finally: if conn is not None: conn. close ()

 

# The return value is required for the CURD operation. The query must return the value def crud (conn, SQL, data): cursor = get_cursor (conn) n = cursor.exe cute (SQL, data) print ("n", n. rowcount) fetchall = cursor. fetchall () conn. commit () close_all (conn, cursor) return fetchall

 

 

# The CRUD operation does not require the return value. The query must return the def crud (conn, SQL, data): cursor = get_cursor (conn) n = cursor.exe cute (SQL, data) conn. commit () close_all (conn, cursor)

 

 

A simple example:

Conn = get_conn ('d:/temp/sqlite3/test. db ') fetchall = crud (conn, 'select * from student', '') # assume that the returned data has three fields for row in fetchall: print ('name: % s, age: % d, address: % s' % (row [0], row [1], row [2])

 

 

 

The above code is the simplest operation on the python database.

If you have any questions or file errors, you can leave a message for discussion!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.