Python connection MySQL Database method detailed

Source: Internet
Author: User
Tags mysql client mysql tutorial in python

Python connection MySQL Tutorial database Tutorial method detailed


Host, the connected database server hostname, which defaults to the local host (localhost).
User, the username that connects to the database, and defaults to the current user.
passwd, connection password, no default value.
DB, the database name of the connection, and there is no default value.
Conv, maps text to a Python-type dictionary. Default to

Mysqldb.converters.conversions
Cursorclass,cursor () The type used, the default value is Mysqldb.cursors.cursor


Compress, enable the Protocol compression feature.
Named_pipe, in Windows, is connected to a named pipe.
Init_command, once the connection is established, specify a statement for the database server to run.


Read_default_file, using the specified MySQL configuration file.
Read_default_group, the default group to read.
Unix_socket, in Unix, the socket used by the connection, TCP is used by default.
Port, specify the connection ports for the database server, which defaults to 3306.

Import MySQLdb

conn = MySQLdb.connect (host= "localhost",

User= "Root",

passwd= "Root",

db= "TestDB")

cursor = Conn.cursor ()

Cursor.execute ("SELECT * from users")

res = Cursor.fetchall ()

Print Res

Cursor.close ()

Conn.close ()

Query data Instance Two
cn = Connection (' localhost ', ' root ', ' ")
cn.select_db (' yphp tutorial ')
cur = cn.cursor ()
Cur.execute (' Set names UTF8 ')
Cur.execute (' Select Id,url,content from Yphp_spider_url where checked=0

Limit 0,1 ')
rs = Cur.fetchone ()
Print RS

MySQL Connector/python is a MySQL client server that is implemented in Python language

Connection Agreement. This package does not need to install any MySQL software. is using Python 3.1.

This version supports the Python 2.4 version, which handles large packets, supports connection compression protocols, and

Ssl.

MySQL Connector/python is the MySQL official provided by the Python connection MySQL database

The driver.

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.