python3.4 How to connect MySQL database

Source: Internet
Author: User

python3.4 How to connect MySQL database
release Time: 2014-08-04 Editor: www.jbxue.com
This article introduced the python3.4 connection MySQL database method, in python3.4 can not use the MYSQLDB connection MySQL, may use pymysql completes the connection MySQL database, needs the friend reference.

In the python3.4 with the original python2.7 MySQLdb has been unable to connect to the MySQL database.

Like MySQLdb's friends, you can refer to:

    • Python Instance MYSQLDB operations database
    • Python mysqldb operating instance code for MySQL
    • Examples of Python mysqldb manipulating MySQL Database
    • Examples of Python using mysqldb to connect to MySQL
    • A simple wrapper code for a Python instance of the MySQLdb module
    • Python Django uses MYSQLDB to export XML from the database
    • Installation and use of Python mysqldb
    • Python connects to MySQL database using MySQLdb

You can use Pymysql to complete the task of connecting MySQL with the following steps:
Ordinal description
1, go to GitHub download Pymysql installation package Pymysql
2. Unzip to a certain drive letter
3. Open CMD window (win environment), enter Pymysql root directory to execute command, Python setup.py install
4, in the program, import Pymysql
5. Start Connection Database (www.jbxue.com script Academy)

API documentation connection for database operations: http://legacy.python.org/dev/peps/pep-0249/

Code:

Copy Codecode example: __author__ = ' Qindongliang '
#导入pymysql的包
Import Pymysql
Try
#获取一个数据库连接, note that if you have a UTF-8 type, you need to make a database
Conn=pymysql.connect (host= ' localhost ', user= ' root ', passwd= ' Qin ', db= ' person ', port=3306,charset= ' UTF8 ')
Cur=conn.cursor () #获取一个游标
Cur.execute (' select * from person ')
Data=cur.fetchall ()
For D in data:
#注意int类型需要使用str函数转义
Print ("ID:" +str (d[0]) + ' name: ' +d[1]+ "Gender:" +d[2 ")

Cur.close () #关闭游标
Conn.close () #释放数据库资源
Except Exception:p rint ("Exception occurred")

Output:

D:\python\python.exe d:/pythonide/pythonprojectworkspace/python/mysql.py
Id:1 Name: Qin Day Sex: Male
Id:2 name: Wang Jing Sex: female

Process finished with exit code 0

python3.4 How to connect MySQL database

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.