Python operates MySQL database through sql statement


1. Commonly used databases

Insert picture description here

1.1 Database key

Insert picture description here

1.2 Database interface

1.2.1 SQL statement mode (execute SQL statement directly)

The most primitive way: simple, direct, and efficient.
Disadvantage: There are many SQL statements and it is not convenient to maintain

1.2.2 ORM method (object-relational mapping)

A table is a class ( addition, deletion, and modification of class == table addition, deletion, and modification )
advantage: No need to write SQL statements
. A piece of data in the table is an object
. The fields in the table are equivalent to the member variables of the class. The values ​​of
a record are equivalent to the attribute variables in the object.


Second, the use of Mysql

2.1 Use the visualization tool SQLyog

Here, I am using Mysql visual lightweight tool: SQLyog
Click: Add users to the database
Insert picture description here

2.2 Using the command line

After creating the userMust be authorized, So that you can operate on the database
Insert picture description here


Three, Python operation database

Steps to use pymysql:
Core class Connect link and Cursor read and write use
1. Establish a link with the database server
2. Get the cursor object (used to send and receive data)
3. Use the cursor to execute the sql statement
4. Use the fetch method to get the result of the execution
5. Close the link first close the cursor and then close the link for
more details

3.1 Create student table

The code is as follows (example):

db = pymysql.connect(user='test',host='localhost', password='123456',database='testdb',charset='utf8')
cursor = db.cursor() #创建游标  游标的每个动作会执行到数据库里面
sql = """create table student (
        id int auto_increment primary key,
        name varchar(20) not null,
        age int,
        score int)"""
cursor.execute(sql)
db.close()

Result display:
Insert picture description here

Query new table information in SQLyog:

DESC student

Insert picture description here

3.2 Insert data

The code is as follows (example):

sql = """insert into student(name,age,score) 
        values(%s,%s,%s)"""

cursor.execute(sql,('Tom',34,78))
db.commit()
cursor.execute(sql,('Geroge',14,88))
db.commit()

db.close()

Result display:
Insert picture description here

3.3 Query results in Pyhthon

Code example:

#读取
sql = "select name,age,score from student"

cursor.execute(sql) #只是拿到了数据 
for name,age,score in cursor:
    print(name,age,score)

Result display:
Insert picture description here


to sum up

Question 1: The following problems occurred during operation:
Insert picture description here

Solution : if yesNew user, new databaseYou must log in to mysql first, and then execute the python statement to access it. If you execute the python statement directly without logging in, the problem just now will occur.

Next article: Operate MySQL through ORM

Guess you like

Origin blog.csdn.net/HG0724/article/details/112323696