Python's MySQL database operation

Source: Internet
Author: User
Tags microsoft sql server mysql in

Python operation MySQL Database

The Python standard database interface for Python Db-api,python DB-API provides developers with a database application programming interface.

The Python database interface supports a very great number of databases, and you can choose the database that works for your project:

    • Gadfly
    • MSQL
    • Mysql
    • PostgreSQL
    • Microsoft SQL Server 2000
    • Informix
    • Interbase
    • Oracle
    • Sybase

I am currently using MySQL database, so this is just a log of the use of MySQL in Python

Before writing the MySQL Python code, you need to make sure that MySQL is already installed on your computer.

How do I install mysqldb?

In order to write MySQL scripts with Db-api, you must make sure that MySQL is already installed. Copy the following code and execute:

#!/usr/bin/python#-*-coding:utf-8-*-importmysqldb  

If the output after execution is as follows, it means that you do not have the MySQLdb module installed:

traceback  (most recent call last):  file< Span class= "PLN" >  "test.py" , line 3, in <module>  import mysqldbimporterror: nomodule named mysqldb        

To install MYSQLDB, please visit http://sourceforge.net/projects/mysql-python

Read the code directly to understand the use of MySQL statements in Python

1 #! /usr/bin/python2 3 #-*-coding:utf-8-*-4 5 ImportMySQLdb6 7 #Import of Packages8 9db = MySQLdb.connect ("IP address, native to localhost","User name","Password","Table name")Ten  One #Open a connection to a database A  -cursor =db.cursor () -  the #using the cursor () method to get an action cursor -  -Cursor.execute ("MySQL Statement") -  + #executing SQL statements using the Execute () method -  +data =Cursor.fetchone () A  at #Use the Fetchone () method to get a single piece of data -  - PrintData -  - #outputs the obtained data -  in db.close () -  to #to close a connection to a database

1 #!/usr/bin/python2 #-*-coding:utf-8-*-3 4 ImportMySQLdb5 6 #Open a database connection7db = MySQLdb.connect ("localhost","testuser","test123","TESTDB" )8 9 #get an operation cursor using the cursor () methodTencursor =db.cursor () One  A #SQL INSERT statement -sql ="INSERT into EMPLOYEE (first_name, - last_name, age, SEX, INCOME) the VALUES ('%s ', '%s ', '%d ', '%c ', '%d ')"%  -('Mac','Mohan', 20,'M', 2000) - Try: -    #Execute SQL statement + cursor.execute (SQL) -    #commit to database execution + Db.commit () A except: at    #Roll Back when an error occurs - Db.rollback () -  - #To close a database connection -Db.close ()
Instance:

The following code uses variables to pass parameters to the SQL statement:

1 " test123 " 2 " Password " 3 4 con.execute ('insert into Login ' values ('%s ', '%s ')' %5               (user_id, password))

Gets the return result after the MySQL statement executes:
In the middle of the first code, I wrote a method to get a row to return the result, and here is the
1 Fetchone (): This method gets the next query result set. The result set is an object 2fetchall (): Receives all the returned result rows. 3 RowCount: This is a read-only property and returns the number of rows affected after the Execute () method is executed.




Python's MySQL database operation

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.