Connect usage of MySQLdb in Python

Source: Internet
Author: User

The MySQLdb module is a module used by python to connect to the mysql database. It is frequently used to operate mysql databases. connect is the most commonly used method when connecting to databases. This method has many parameters, to sum up, there are several main types:

The connect () method is used to connect to the database and return a database connection object. To connect to a mysql database named MySQL on the server www.gyyx.com, you can write the connection string as follows:

Db = MySQLdb. connect (host = "www.gyyx.com", user = "user", passwd = "xxx", db = "mysql ")

The parameters of connect () are listed as follows:

Host, the host name of the connected database server. The default value is local host ).

User, the user name used to connect to the database. The default value is the current user.

Passwd, connection password, no default value.

Database, the name of the connected database, with no default value.

Conv: maps text to a Python dictionary. The default value is MySQLdb. converters. conversions.

The type used by cursorclass and cursor (). The default value is MySQLdb. cursors. Cursor.

Compress: enables protocol compression.

Named_pipe is connected to a named pipe in windows.

Init_command: Once the connection is established, a statement is specified for the database server to run.

Read_default_file: Use the specified MySQL configuration file.

Read_default_group: the default read group.

Unix_socket: the socket used for connection in unix. TCP is used by default.

Port: Specifies the connection port of the database server. The default value is 3306.

The db. close () method of the connection object can close the database connection and release related resources.

The db. cursor ([cursorClass]) method of the connection object returns a pointer object for accessing and operating data in the database.

The db. begin () method of the connection object is used to start a transaction. If the database AUTOCOMMIT has been enabled, close it until the transaction calls commit () and rollback.

The db. commit () and db. rollback () Methods of the connection object indicate transaction commit and rollback respectively.

The cursor. close () method of the pointer object closes the pointer and releases related resources.

The cursor.exe cute (query [, parameters]) method of the specified object executes the database query.

The cursor. fetchall () of the pointer object can retrieve all rows in the pointer result set, and the returned result set is a tuples ).

The cursor. fetchiterator ([size = cursor. arraysize]) of the pointer object extracts multiple rows from the query result set. We can use optional parameters to specify the number of rows to be retrieved.

Cursor. fetchone () of the pointer object returns the next row from the query result set.

The cursor. arraysize attribute of the pointer object specifies the number of rows returned by the cursor. fetchmany () method, which affects the performance of fetchall (). The default value is 1.

The cursor. rowcount attribute of the pointer object indicates the number of rows that occurred during the last query or update. -1 indicates that the query has not started or data has not been queried.

The following is an example of connecting to mysql:

def mysql_conn():      try:          conn = MySQLdb.connect(host = '192.168.8.100',user = 'mysql',passwd = '123456',connect_timeout=10)          cursor = conn.cursor()           sql = "SELECT COUNT(1) FROM mysql.user"         cursor.execute(sql)          alldata = cursor.fetchall()          count = alldata[0][0]          cursor.close()          conn.close()          print count      except Exception,e:          print "Can not Connect to mysql server" 

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.