一、查询操作

1.导入pymysql

1
import pymysql

2.创建连接对象

调用pymysql模块中的connect()函数来创建连接对象,代码如下:

1
conn=connect(host,port,user,password,database,charset)

host:连接的mysql主机

port:连接的mysql端口

user:连接的用户名

password:连接的密码

database:连接的数据库

charset:采用的编码方式,推荐utf-8

连接对象说明

  • 关闭连接 conn.close()
  • 提交数据 conn.commit()
  • 撤销数据 conn.rollback()

3.获取游标对象

获取游标对象的目的就是执行SQL语句,完成对数据库中的增、删、改、查操作。代码如下:

1
cur = conn.cursor()

游标操作说明

  • 使用游标执行SQL语句:execute(opertion [parameters])执行SQL语句,返回受影响的行数,主要用于执行insert、update、delect、select等语句。
  • 获取查询结果集中的一条数据:cur.fetchone()返回一个元组,如(1,’张三’)
  • 获取查询结果集中的所有数据:cur.fetchall()返回一个元组,如((1,’张三’),(2,’李四’))
  • 关闭游标:cur.close(),表示和数据库的操作完成

4.定义SQL语句

1
sql = "select * from students"

5.执行SQL语句

1
row_count = cursor.execute(sql)

返回row_count的值为执行sql的行数

6.读取数据表中的数据

1
print(cursor.fetchall())

7.关闭游标对象和连接对象

1
2
cursor.close()
conn.close()

整体语句:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import pymysql

conn=connect(host='',port='',user='',password='',database='',charset='')

cur = conn.cursor()

sql = "select * from students"

row_count = cursor.execute(sql)

#这是获取表中第一个数据
rest=cursor.fetchone()
print(rest)

#这是查询表中所有的数据
rest=cursor.fetchall()
for i in rest:
print(i)

cursor.close()
conn.close()

二、增加操作

1.导入pymysql模块

1
import pymysql

2.创建连接对象

1
conn=connect(host='',port='',user='',password='',database='',charset='')

3.获取游标对象

1
cur = conn.cursor()

4.定义SQL语句

1
sql = "insert into studebts valuse (null, '赵云', 22, 'male', 98.0, 3)"

5.执行SQL语句

1
2
row_count = cursor.execute(sql)
conn.commit() #对SQL增删改操作必须要提交数据

6.关闭游标对象和连接对象

1
2
cursor.close()
conn.close()

整体语句:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import pymysql

conn=connect(host='',port='',user='',password='',database='',charset='')

cur = conn.cursor()

try:
sql = "insert into studebts valuse (null, '赵云', 22, 'male', 98.0, 3)"

row_count = cursor.execute(sql)
conn.commit()
except Exception as e:
conn.rollback() #异常处理

cursor.close()
conn.close()

三、删除操作

1.导入pymysql模块

1
import pymysql

2.创建连接对象

1
conn=connect(host='',port='',user='',password='',database='',charset='')

3.获取游标对象

1
cur = conn.cursor()

4.定义SQL语句

1
sql = "delect from students where id = 6"

5.执行SQL语句

1
2
row_count = cursor.execute(sql)
conn.commit() #对SQL增删改操作必须要提交数据

6.关闭游标对象和连接对象

1
2
cursor.close()
conn.close()

整体语句:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import pymysql

conn=connect(host='',port='',user='',password='',database='',charset='')

cur = conn.cursor()

try:
sql = "delect from students where id = 6"

row_count = cursor.execute(sql)
conn.commit()
except Exception as e:
conn.rollback() #异常处理

cursor.close()
conn.close()

四、更改操作

1.导入pymysql模块

1
import pymysql

2.创建连接对象

1
conn=connect(host='',port='',user='',password='',database='',charset='')

3.获取游标对象

1
cur = conn.cursor()

4.定义SQL语句

1
sql = "update students set name = '小乔' where id = 5"

5.执行SQL语句

1
2
row_count = cursor.execute(sql)
conn.commit() #对SQL增删改操作必须要提交数据

6.关闭游标对象和连接对象

1
2
cursor.close()
conn.close()

整体语句:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import pymysql

conn=connect(host='',port='',user='',password='',database='',charset='')

cur = conn.cursor()

try:
sql = "update students set name = '小乔' where id = 5"

row_count = cursor.execute(sql)
conn.commit()
except Exception as e:
conn.rollback() #异常处理

cursor.close()
conn.close()