Python database operations-Getting Started with PyMySQL

Source: Internet
Author: User
Python database operations-PyMySQL entry PyMySQL is a module for operating MySQL in Python. it has the same basic functions as the MySQLdb module. PyMySQL has almost the same performance as MySQLdb.

It is not particularly strong. it will be more convenient to use PyMySQL. PyMySQL is completely written in python, avoiding the trouble of installing MySQLdb on different systems.

Applicable environment

Python version> = 2.6 or 3.3

Mysql version> = 4.1


Install

Run the following command on the command line:

pip install pymysql

Install it manually. download it first. : Https://github.com/pymysql/pymysql/tarball/pymysql-x.x.


X. X is the version.


Decompress the package. Enter the decompressed directory in the command line and execute the following command:

python setup.py install

We recommend that you use pip to install the package, which automatically solves the package dependency problem and avoids various errors during installation.


The basic operations of pymysql are as follows:

#! /Usr/bin/env python # -- coding = UTF-8 # Author Allen Leeimport pymysql # Create a link object conn = pymysql. connect (host = '2017. 0.0.1 ', port = 3306, user = 'root', passwd = '000000', db = 'Allen') # Create a cursor = conn. cursor () # execute SQL, update a single piece of data, and return the affected number of rows pait_row = cursor.exe cute ("update hosts set host = '1. 1.1.2 '") # insert multiple entries and return the affected function effect_row = cursor.exe cute.pdf (" insert into hosts (host, color_id) values (% s, % s )", [("1.0.0.1", 1,), ("10.0.0.3", 2)]) # obtain the latest auto-incrementing IDnew_id = cursor.lastrowid1_query data cursor.exe cute ("select * from hosts ") # obtain a row row_1 = cursor. fetchone () # get multiple (3) rows row_2 = cursor. fetchmany (3) # obtain all row_3 = cursor. fetchall () # reset the cursor type to dictionary type cursor = conn. cursor (cursor = pymysql. cursors. dictCursor) # submit and save the new or modified data conn. commit () # Close the cursor. close () # close the connection conn. close ()

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.