Python database operations

Source: Internet
Author: User

You can use python to operate the database easily. The first script is to export the database to a specified location. The following is a brief summary of Python database operations.


Download and install the python environment, download mysqldb, and mysqldb is the python database operation module. The following describes the API documentation.

Http://mysql-python.sourceforge.net/MySQLdb.html

Http://mysql-python.sourceforge.net/MySQLdb-1.2.2/public/MySQLdb-module.html

Step 1: Import python to the database module import mysqldb

Step 2: connect to the database. There are many methods in the document, such as DB = _ mysql. Connect ("localhost", "joebob", "MoonPie", "thangs ")

Or DB = mysqldb. Connect (host = "localhost", Port = 3306, user = "root", passwd = "sa", DB = "mytable ")

_ For more information about MySQL, see the following document.

Mysqldb also supports transaction operations,

Commit () Submit
Rollback () rollback


Callproc (self, procname, argS): used to execute a stored procedure. The received parameters are the stored procedure name and parameter list. The returned values are the number of affected rows.

There are two methods for adding, deleting, modifying, and querying. One is to directly use dB for operations, and the other is to use dB. Query () for operations by obtaining the cursor and executing the operation through the cursor.

Curmongodb.cursor((,cur.exe cute (), the parameter is an SQL statement

You can also execute cur.exe cute.pdf (SQL, Param)

# When using SQL statements, the parameters to be received here use the % s placeholder. Note that no matter what type of data you want to insert, The placeholder will always use % s

SQL = "insert into cdinfo values (% s, % s )"

# Param should be tuple or list

Param = (title, singer, imgurl, URL, alpha)

# Execution. If the execution succeeds, the value of N is 1.

N=cursor.exe cute (SQL, Param)

# If you want to insert data in batches, do this.

SQL = "insert into cdinfo values (0, % s )"

# The set of each value is a tuple, and the entire parameter set is a tuple or list

Param = (title, singer, imgurl, URL, alpha), (title2, singer2, imgurl2, url2, alpha2 ))

# Use the executeplugin method to insert data in batches. This is a really cool method!

N=cursor.exe cute.pdf (SQL, Param)


Fetchall (Self): receives all returned result rows.

Fetchmany (self, size = none): receives the size of returned results rows. If the size value is greater than the number of returned results rows, the returned cursor. arraysize data is returned.

Fetchone (Self): returns a result line.

Scroll (self, value, mode = 'relative '): Move the pointer to a row. if mode = 'relative ', the value bar is moved from the current row. If mode = 'absolute', the value bar is moved from the first row of the result set.

Number of records in the query result

DB. Query ("select * From table_name ")
Result = mesondb. store_result ()
Row_count = int (result. num_rows ())


After insertion or deletion, execute dB. Commit () to submit data.

Close connection

You need to close the pointer object and connection object respectively. They have the same name.

Cursor. Close ()
Conn. Close ()



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.