python3.6 using MySQL

Source: Internet
Author: User
Tags rowcount

Because MYSQLDB does not support Python3, you need to import Pymysql

You can choose Pip to install Pymysql, or Pycharm install

[File] >> [Settings] >> [Project:python] >> [Project interpreter] >> "+"

Importpymysql.cursors#connecting to a databaseConnect =Pymysql. Connect (Host='localhost', Port=3310, the user='Root', passwd='Root', DB='python', CharSet='UTF8')#Get cursorcursor =connect.cursor ()#Inserting Datasql ="INSERT into trade (name, account, saving) VALUES ('%s ', '%s ',%.2f)"Data= ('Lei June','13512345678', 10000) cursor.execute (SQL%data) Connect.commit ()Print('successfully inserted', Cursor.rowcount,'Bar Data')#Modifying Datasql ="UPDATE trade SET saving =%.2f WHERE account = '%s '"Data= (8888,'13512345678') cursor.execute (SQL%data) Connect.commit ()Print('successfully modified', Cursor.rowcount,'Bar Data')#Querying Datasql ="SELECT name,saving from trade WHERE account = '%s '"Data= ('13512345678',) cursor.execute (SQL%data) forRowinchCursor.fetchall ():Print("name:%s\tsaving:%.2f"%row)Print('co-check to find out', Cursor.rowcount,'Bar Data')#Delete Datasql ="DELETE from trade WHERE account = '%s ' LIMIT%d"Data= ('13512345678', 1) cursor.execute (SQL%data) Connect.commit ()Print('successfully deleted', Cursor.rowcount,'Bar Data')#Transaction ProcessingSql_1 ="UPDATE trade SET saving = saving + $ WHERE account = ' 18012345678 '"sql_2="UPDATE trade SET expend = expend + $ WHERE account = ' 18012345678 '"Sql_3="UPDATE Trade SET income = income + a WHERE account = ' 18012345678 '"Try: Cursor.execute (sql_1)#Increase in savingsCursor.execute (sql_2)#increased spendingCursor.execute (Sql_3)#Increase in revenueexceptException as E:connect.rollback ()#Transaction Rollback    Print('Transaction Failure', E)Else: Connect.commit ()#Transaction Commit    Print('Transaction Success', Cursor.rowcount)#Close Connectioncursor.close () connect.close ( )

python3.6 using MySQL

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.