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