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

25 fresh useful icon sets for download abroad

1. E-Commerce Icons 2. Icon Sweets 2 3. Mobile Ph...

How to check disk usage in Linux

1. Use the df command to view the overall disk us...

Dockerfile implementation code when starting two processes in a docker container

I want to make a docker for cron scheduled tasks ...

Summary of Spring Boot Docker packaging tools

Table of contents Spring Boot Docker spring-boot-...

Importance of background color declaration when writing styles

As the title says, otherwise when the page is revi...

How to use skeleton screen in vue project

Nowadays, application development is basically se...

HTML+VUE paging to achieve cool IoT large screen function

Effect demo.html <html> <head> <me...

A brief discussion on the three major issues of JS: asynchrony and single thread

Table of contents Single thread asynchronous Sing...

jQuery implements form validation function

jQuery form validation example / including userna...

The "3I" Standards for Successful Print Advertising

For many domestic advertisers, the creation and ev...

Steps to enable MySQL database monitoring binlog

Preface We often need to do something based on so...

Steps for Docker to build its own local image repository

1. Environment and preparation 1. Ubuntu 14.04 2....

Summary of tips for making web pages

Preface This article mainly summarizes some of th...