Common Database Operation modules and connection instances in python

Source: Internet
Author: User
This article mainly introduces various common database operation modules and connection instances in python, including sqlite3, oracle, mysql, and excel. If you need them, refer to the following work, it is often necessary to use python to access various databases, such as reading configuration files from oracle or writing result information to mysql.
The following lists the modules that may be used.

Sqlite3: built-in modules
Sometimes it is very convenient to use sqlite. I think it does implement the declared "Zero Configuration ". Since python version 2.5, it has built-in support for sqlite3, which is also very simple to use. Follow the instructions below:

The Code is as follows:


# Open the db file and obtain the connection
Conn = sqlite3.connect ('data filename ')
# Obtain a cursor
C = conn. cursor ()
# Execute SQL
C.exe cute (''' SQL fragment ''')
# If there is a data modification operation, you need to commit it.
Conn. commit ()
# Closing a cursor
C. close ()
# Closing a connection
Conn. close ()


In addition, for how to use sqlite in C and bash, refer to previous articles.

Oracle: cx_Oracle

In fact, I first introduced sqlite3. Apart from being a small database, there is another reason: the operations of other databases in python are basically the same as those in sqlite3. That is to say, python has actually basically integrated database interfaces.
Open the cx_Oracle document page and you will find that the style is similar to that of the python document, because they all use Sphinx. The usage of the module is more like, replace the line in the above Code that gets the connection with this:

The Code is as follows:


Conn = cx_Oracle.connect ('username/password @ tnsname ')


You can. You only need to input the username, password, and TNS as a string to get an oracle connection.

Mysql: MySQLdb

Similar to the first two, the connection uses one of the following two syntaxes:

The Code is as follows:


Conn = MySQLdb. connect ('host', 'username', 'Password', 'database ')
Conn = MySQLdb. connect (host = "host", user = "username", passwd = "password", db = "database ")


Next, we can use it as sqlite.

Excel: pyExcelerator

Okay, I admit that excel is not a database. It's just written here. Haha. Because sometimes we still need to remove the data from the excel files from others.
In fact, using pyExcelerator to read files is also very simple:

The Code is as follows:


Sheets=pyExcelerator.parse_xls('xxx.xls ')


After this is done, sheets is the whole working thin, which is a list composed of worksheets, and a worksheet corresponds to a tuple in the format of ('worksheet name', content ), the content is a dict, and the key is a tuple (number of rows, number of columns). value is the content of the corresponding grid. It looks really awkward. Fortunately, there are not many excel applications.
In addition, pyExcelerator also supports writing data to excel. If you want to save the query results as excel, try again. I still try not to use this format, haha.

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.