Python3 mysql tutorial and python3mysql tutorial

Source: Internet
Author: User

Python3 mysql tutorial and python3mysql tutorial

1. Download \ install \ Configuration

1. python3

Python3 download URL: http://www.python.org/getit/

The latest version is python3.2, which is

Http://www.python.org/ftp/python/3.2.3/python-3.2.3.msi

The installation process is not required. It is installed in the C: \ Python32 directory by default.

 

After installation, add the installation directory C: \ Python32 to the environment variable. Open the Command Prompt window and enter python. If the python version is returned, the installation is successful and the environment variable is set successfully.

C:\>pythonPython 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> 

 

2. MySQL

MySQL: http://www.mysql.com/downloads/mysql/

MySQL has many types of versions. Here we choose MySQL Community Server, the latest version 5.25a.

: Http://cdn.mysql.com/Downloads/MySQL-5.5/mysql-5.5.25a-win32.msi

The installation process is a bit complex. You can refer to the MySQL installation diagram:

Http://wenku.baidu.com/view/fe9b292e4b73f242336c5fe9.html

Note that you must set the MySQL encoding to utf8.

After the installation is complete, you need to configure MySQL. Here I configure its username as root and password as Welcome123.

 

Log on to mysql using the command. The installation is successful.

C:\>mysql -u root -pEnter password: **********Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 10Server version: 5.5.25a MySQL Community Server (GPL)Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respective owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>

 

Here we create a database named txw1958.

mysql> create database txw1958;Query OK, 1 row affected (0.03 sec)mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || test               || txw1958            |+--------------------+6 rows in set (0.00 sec)

 

3. MySQL-python Module

MySQL-python is the database interface used by MySQL for python. Currently, python2.7 and python3.2 are supported.

: Http://download.csdn.net/detail/txw1958/5818181

 

The installation directory of python3.2 can be found automatically during installation and installed under the directory.

 

After the installation is complete, import MySQLdb in python. If no error is reported, the installation is successful.

>>> import MySQLdb>>> 

 

 

Ii. Use python3 to operate MySQL

The following is an example of MySQL5.5 using python3, which includes the functions of connecting to MySQL, creating databases, creating tables, and inserting/querying data:

#-*-Coding: UTF-8-*-# author: txw1958 # website: http://www.cnblogs.com/txw1958/import MySQLdb # connect cxn = MySQLdb. connect (host = '2017. 0.0.1 ', user = 'root', passwd = 'welcome123') # cursor cur = cxn. cursor () try: cur.exe cute ("drop database txw1958") failed t Exception as e: print (e) finally: passthrough creates the Data Warehouse cur.exe cute ("create database txw1958" Export cur.exe cute ("USE txw1958" then creates the TABLE cur.exe cute ("create table users (id INT, name VARCHAR (8 )) "inserting values into cur.exe cute (" insert into users VALUES (1, 'www '), (2, 'cnblogs'), (3, 'com'), (4, 'txw1958 ') "zookeeper queries cur.exe cute (" SELECT * FROM users ") for row in cur. fetchall (): print ('% s \ t % s' % row) # disable cur. close () cxn. commit () cxn. close ()

For introduction and API of MySQLdb, see http://mysql-python.sourceforge.net/MySQLdb.html

 

The running result is as follows:

 

C:\>python py3-mysql.py1       www2       cnblogs3       com4       txw1958C:\>

 

 

Appendix: related information of MySQLdb

 

1. Introduce the MySQLdb Library

Import MySQLdb

 

2. Establish a connection with the database

Conn = MySQLdb. connect (host = "localhost", user = "root", passwd = "sa", db = "mytable", charset = "utf8 ")

The connect method is used to establish a connection with the database, receive several parameters, and return the connection object.

 

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.