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