Python rookie promotion 10 ---- database operations, python10 ----

Source: Internet
Author: User

Python rookie promotion 10 ---- database operations, python10 ----
Introduction to the release of python3 has received negative comments from many programmers, saying that it is unstable and incompatible. However, negative comments are negative comments. Since python3 has been released, it must be a trend, however, in python3.4, mysqldb using python2.7 can no longer connect to the mysql database, which is quite confusing. However, we can use pymysql to complete the task of connecting to mysql. The steps are as follows:

Serial number Description
1 Go to github to download the pymysql installation package pymysql
2 Decompress the package to a drive letter.
3 Open the cmd window (in Windows) and run the command in the root directory of pymysql. python setup. py install
4 In the program, import pymysql
5 Start database connection
Database operation API documentation connection: http://legacy.python.org/dev/peps/pep-0249/

Example
# Import pymysql package import pymysqltry: # Get A database connection, note that if it is a UTF-8 type, you need to develop the database conn = pymysql. connect (host = 'localhost', user = 'root', passwd = '000000', port = 123456, charset = 'utf8') cur = conn. cursor () # obtain a cursor object cur.exe cute ("create database test15") # execute the corresponding SQL statement cur.exe cute ("USE test15 ") cur.exe cute ("create table users (id INT, name VARCHAR (18)") cur.exe cute ("insert into users VALUES (1, 'blog '), (2, 'csdn '), (3, 'net'), (4, 'a359680405') ") cur.exe cute (" SELECT * FROM users ") data = cur. fetchall () for row in data: print ('% s \ t % s' % row) cur. close () # close the cursor conn. commit () # submit any outstanding transactions to the database, and do not perform any operations on the database that does not support transactions. close () # close the connection to the database and release the database resource snapshot t Exception: print ("Exception occurred ")


To use python3.4 with django1.7.2, add the following code in the _ init _. py file of the site:
1 import pymysql2 pymysql.install_as_MySQLdb()

After completing the preceding steps, you can access mysql in django.







Last Lecture: python rookie promotion 09 ---- Regular Expression next lecture:



Welcome to my public account

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.