Pycharm link mysql database under Mac

         Before we start, we first need to download mysql and perform some simple configuration operations. These processes are recorded in the author's previous blog for reference.

        Then after downloading mysql, pycharm, etc., we can start to connect to the mysql database in pycharm, so as to use it in python code and query the database. Of course, if you want to perform a lot of operations on the database, it is recommended to use visual tools for operations. After all, it is still a little troublesome to implement operations such as update and delete in python code. Here, the author recommends that you can download datagrip. Realized, well, without further ado, let's get to the point.

       1: Import pymysql

        To operate the database in python code, we first need to import the pymysql package:

Implementation:

(1): Enter in the terminal terminal: pip3 install pymysql Wait for a while and then write import pymysql in the code

(2): Find under file->setting or preference: Python Interpreter and click the small + sign to import the pymysql package as shown in the figure below

        2: Create a python file to write database statements in the code

Open your new python project:

(1): Open the database (mysql) through the toolbar on the right:

Click the database button on the right, and click the plus sign to add a database: there are many database options, here we choose the mysql database to use. The page obtained after adding is as follows:

Here we have successfully displayed the local mysql database in pycharm. In the future, when we operate the database, in order to facilitate us to check whether it is successful, we can directly click on the database on the right side, and we can quickly see the database after we have completed the operation.

(2): Write database statements through python code

        First import the pymyql package in the python file, the first step has been completed, then we need to connect to the mysql database, using the pymysql.connect method:

conn=pymysql.connect(host='localhost',database='你自己创建的数据库名',user='root',password='你自己创建的密码',charset='utf8')

ps: Here we can use the try-except statement. If the conn is successfully created, we output the database connection success, and if it fails, we output the database connection failure.

        Next create a cursor object: cursor

cursor=conn.cursor()

         Complete database addition, deletion, modification and query operations: After the above operations are completed, we can implement database addition, deletion, modification and query operations in the python code.

3: add, delete, modify and check

(1): query statement

        We first implement the simplest database query statement: Suppose we have created a database table named student, which contains several records.

cursor.execute("select * from student")

But after querying, we will not see the result of the query, we need to output the result to see:

print(cursor.fetchall())

Then run, we can see all the records in the student on the console.

 result can return the number of rows we have operated on. fetchone() can return records, output one by one.

(2): insert operation

        Insert a new record into the student table:

sqladd="  insert into student values( 2021304622 , 'csdn' , '人工智能学院' , 25,'男'  )  "
cursor.execute(sqladd)
conn.commit()

        The last sentence: conn.commit is very important, without this sentence, the insertion operation cannot be completed.

ps: In addition to the query operation does not need to write conn.commit(), other operations need to be written, otherwise it cannot be completed.

After completion, we can go to the student table to see if the insertion is successful:

 (3): delete operation

sqldelete=" delete from student where name='卫生纸' "
cursor.exexute(sqldelete)
conn.commit()

You can see that the toilet paper record has been deleted.

(4): Update operation

        Let's add Xiao Song's age by 1:

corsur.execute(" update student set age=age+1 where name='小宋' ")
conn.commit()

         

Summarize:

        The above is the operation of connecting and operating the database realized by using python code. I hope it can help everyone, and I hope the teacher can give me a little more usual time ~! ! ! !

Guess you like

Origin blog.csdn.net/m0_61908582/article/details/130317678