Pycharm2017 realizes the connection between python3.6 and mysql

Pycharm2017 realizes the connection between python3.6 and mysql

This article shares with you how to connect python3.6 and mysql on pycharm2017 for your reference. The specific content is as follows

Unlike other IDEs, pycharm does not require the download of mydqldb packages, etc. pymysql can be automatically installed in pycharm, and its functions are the same as those of the downloaded mydqldb.

1. Install pymysql that comes with pycharm

1. First open the settings in pycharm, settings->protect->protect Interpreter->double-click pip on the right-->enter pymysql in the search box-->then select the version and click install package. Wait for a while and it will prompt successful.

2. Code part

import pymysql
 
#Establish database connection conn=pymysql.Connect(
 host='localhost',
 port=3306,
 user='root',
 passwd='database password',
 db='bigsdut',
 charset='utf8'
)
 
#Get the cursor cursor=conn.cursor()
#print(conn)
#print(cursor)
 
#1. Query from the database #sql="INSERT INTO login(user_name,pass_word)"
sql="SELECT *FROM login"
#cursor executes the sql statement cursor.execute(sql)
#Print the number of execution results print(cursor.rowcount)
 
#Use the fetch method to traverse the results. There are three data in total. #rs=cursor.fetchone()#Put the first result in rs#re=cursor.fetchmany(3)#Put multiple results in rerr=cursor.fetchall()#Put all results in rr#Process the results for row in rr:
 print("ID is: =%s, name is: =%s, password is: =%s"%row)
#print(re)#Output two pieces of data, because the fetch() method is based on the previous fetch() method #2 Insert data into the database sql_insert="INSERT INTO login(user_name,pass_word) values('Zhongxing','123')"
#Execute statement cursor.execute(sql_insert)
#Transaction commit, otherwise the database will not be updated conn.commit()
print(cursor.rowcount)
 
 
#Modify the content in the database sql_update="UPDATE login SET user_name='hhh' WHERE id=3"
cursor.execute(sql_update)
conn.commit()
 
#Delete the content in the database and use the try catch statement to roll back the transaction try:
 sql_delete="DELETE FROM login WHERE id=6"
 cursor.execute(sql_delete)
 conn.commit()
except Exception as e:
 print (e)
 #Transaction rollback, that is, after an error occurs, the program will not continue to execute, but will return to the state where the program has not been executed, and the original execution will not be counted conn.rollback()
 
 
 
#Closing the database connection and cursor conn.close()
cursor.close()

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of how to use Pycharm to connect to MySQL database in Django
  • Pycharm connects to the remote server and implements remote debugging
  • Detailed steps to connect to MySQL database in pycharm
  • How to use pycharm to connect to Databricks

<<:  Vue implements a complete process record of a single file component

>>:  How to Rename a Group of Files at Once on Linux

Recommend

React native realizes the monitoring gesture up and down pull effect

React native implements the monitoring gesture to...

MySQL5.7 parallel replication principle and implementation

Anyone who has a little knowledge of data operati...

Detailed tutorial on installing and using Kong API Gateway with Docker

1 Introduction Kong is not a simple product. The ...

Detailed installation and configuration tutorial of MySQL 5.7 under Win10

1. Unzip MySQL 5.7 2. Create a new configuration ...

How to add custom system services to CentOS7 systemd

systemd: The service systemctl script of CentOS 7...

Detailed explanation of Vue slot

1. Function : Allows the parent component to inse...

Usage of Node.js http module

Table of contents Preface HTTP HTTP Server File S...

JavaScript Timer Details

Table of contents 1. Brief Introduction 2. setInt...

Solve the problem of yum installation error Protected multilib versions

Today, when installing nginx on the cloud server,...

Pure CSS to achieve a single div regular polygon transformation

In the previous article, we introduced how to use...

VMware virtual machine installation CentOS 8 (1905) system tutorial diagram

The world-famous virtual machine software VMware-...

CSS tips for implementing Chrome tab bar

This time let’s look at a navigation bar layout w...