Usage of MySQL in Python3.

Source: Internet
Author: User
Tags mysql in rowcount

First, Python2 in general use MYSQLDB to call MySQL, but in Python3 does not support the package, using Pymysql to replace, the use of the exact same.

Second, installation:

Pip Install Pymysql

Third, examples:

#Coding Utf-8ImportPymysqlTry: Conn= Pymysql.connect (host='localhost', user="Root", password='123456', database='Datarepair', port=3306,charset='UTF8') Cursor=conn.cursor ()#cursor.execute ("Select Rowid,name from Ruletab where ruletype=%s", (Ten))Cursor.executemany ("Select Rowid,name from Ruletab where ruletype=%s", [10,20])#generally used in bulk additions and deletions.     Print(Cursor.rowcount,cursor.rownumber) forXinchCursor.fetchall ():Print("Rowid:{0[0]} name:{0[1]} rownumber:{1}". Format (x,cursor.rownumber))Print('-'* 50)     forXinchCursor.fetchmany (2):        Print("Rowid:{0[0]} name:{0[1]} rownumber:{1}". Format (x,cursor.rowcount))Print('-'* 50) cursor.scroll (0,'Absolute')#the cursor is recalled to the 0 position.      forXinchCursor.fetchmany (2):#take 2 records.         Print("Rowid:{0[0]} name:{0[1]} rownumber:{1}". Format (x,cursor.rownumber))Print(Cursor.rowcount,cursor.rownumber) cursor.close ()finally:    if 'Conn' inchDir () andCallable (conn) andconn.open:conn.close ()Print("closed successfully.")

Usage of MySQL in Python3.

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.