python connects to mysql database navicat connects to mysql database

1. Start the mysql service

(First make sure the mysql service is installed)

Open the cmd window as an administrator and enter net start mysql

2. Connect to mysql service

(You can operate directly in the cmd window or use tools)

1. Open navicat and connect to mysql

 Host localhost

 The default port is 3306, which can be modified by yourself.

 The username and password are set by yourself

 After confirmation, you can see the created mysql service. Double-click the service name. The icon turns green to indicate a successful connection.

2.Create database

There will be some databases in it by default. Don’t worry, just click on one and right-click to create a new database.

 Double-click the database name on the right. If the icon turns green, the connection is successful.

3.Create table

Right-click the table under stand-alone test to create a new table

First set some table fields

After setting a field, click Add Field to continue setting.

 

 After setting, click Save

 Custom table name

4. Add data to the table

Double-click the user_info table

3. python connects to mysql service

Create a new mysql.py file

import pymysql

conn = pymysql.connect(
    host='localhost',
    port=3306,
    user='root',
    password="990422",
    database='test',
    charset="utf8",
)
cur = conn.cursor()
cur.execute("select * from user_info")
for users in cur.fetchall():
    print(users)
cur.close()
conn.close()

Change the port, user name, and password to your own settings

executable file

Guess you like

Origin blog.csdn.net/qq_45600228/article/details/132422078