Example of MySQLdb module usage in python, pythonmysqldb

Source: Internet
Author: User

Example of MySQLdb module usage in python, pythonmysqldb

This example describes the usage of the MySQLdb module in python. Share it with you for your reference. The usage analysis is as follows:

MySQLdb is actually a bit like a mode for connecting to a database in php or asp, but MySQLdb is an interface for connecting to mysql. We can connect MySQLdb in python to implement various data operations.

The methods for connecting python to mysql include oursql, PyMySQL, myconyy, and MySQL Connector. However, this article is about another class library MySQLdb, and MySQLdb is the interface for connecting Python to Mysql database, it implements the Python database API Specification V2.0, which is based on MySQL c api. Can be obtained and installed from: https://pypi.python.org/pypi/MySQL-python, and many releases of the linux source has this module, can be directly installed through the source.

I. Database Connection

MySQLdb provides the connect method to establish a connection with the database. It receives several parameters and returns the connection object:
Copy codeThe Code is as follows: conn = MySQLdb. connect (host = "localhost", user = "root", passwd = "jb51", db = "test", charset = "utf8 ")
Common parameters include:
Host: specifies the Database host name. The local host is used by default.
User: Database login name. The default value is the current user.
Passwd: Password for database login. Empty by default
Db: name of the database to be used. No default value exists.
Port: the TCP port used by the MySQL service. The default value is 3306.
Charset: Database Encoding

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.