零基础 学 python开发 (Genius套餐A) 三十八

版权声明:Genius https://blog.csdn.net/weixin_41987706/article/details/90200872

夜光序言:

 

 

只要心情是晴朗的,人生就没有雨天。给自己一个微笑,无论你过去做了什么,将来即将做什么,生活中依旧有许多值得感恩的,给自己一个微笑,是对自己的一个肯定,也是对未来的一份期许……

 

 

 

 

 

 

 

正文:夜光,java中我们通过mysql-connect包也进行了相关操作~

7.2 MySQL 数据库读写
7.2.1 目标 


数据库存储的数据要进行读取与更新,例如前节的 students 表中的学生记录要读出显示,还有进行更新、删除、增加等操作。

目标就是维护students表的查询(select)、增加(insert)、删除(delete)、更新(update)这些基本的数据库操作。



7.2.2 读取数据库 


如果要读取数据库表的数据则使用select的SQL命令,例如读students表的数据则执行:
cursor.execute("select * from students")

执行完毕后接下来要使用:
cursor.fetchone()

其中 fetchone()获取一行数据,第一次执行时得到第一行数据,再次执行时得到第二行数据,以此类推,如果到了记录集的最后再次执行 fetchone()返回 None。

还有一种方法是使用 fetchall()代替 fecthone(),fetchall()一次可以读取所有的行,读取后一般再次使用 for 循环取出每一行。

rows=cursor.fetachall()
for row in rows:
print(row)


例 7-2-1:读取 students 表的记录 


 

import pymysql
try:
 con = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="1111",db="agvtest1", charset="utf8")
 cursor = con.cursor(pymysql.cursors.DictCursor)
 cursor.execute("select * from students")
 while True:
  row=cursor.fetchone()
  print(row)
  if not row:
   break
  con.close()
except Exception as err:
  print(err)


结果:
{'pNo': '1', 'pName': 'A', 'pGender': '男', 'pAge': 20}
{'pNo': '2', 'pName': 'B', 'pGender': '女', 'pAge': 21}
None

由此可见第一次 fetchone()读取返回第一条记录,第二次 fetchone()读取返回第二条记录,第三次 fetchone()读取返回 None。


 
例 7-2-2:读取 students 表的记录各个字段值 


 

import pymysql
try:
 con = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="1111", db="agvtest1", charset="utf8")
 cursor = con.cursor(pymysql.cursors.DictCursor)
 cursor.execute("select * from students")
 while True:
 row=cursor.fetchone()
 if not row:
 break
 print(row["pName"],row["pGender"],row["pAge"])
 con.close()
except Exception as err:
 print(err)

结果:
1 A 男 20
2 B 女 21
由 此 可 见 使 用 row["pName"],row["pGender"],row["pAge"] 分 别 得 到 字 段pNo,pName,pGender,pAge 的值。在执行 select 命令后读数据库数据除了使用 fetchone()一行一行读取外,还可以使用fetchall()一次读取全部行。


例 7-2-3:fetchall()读取 students 表的全部记录


import pymysql
try:
 con = pymysql.connect(host="127.0.0.1", port=3306, user="root",passwd="1111", db="agvtest1", charset="utf8")
 cursor = con.cursor(pymysql.cursors.DictCursor)
 cursor.execute("select * from students")
 rows=cursor.fetchall()
 for row in rows:
 print(row["pName"],row["pGender"],row["pAge"])
 con.close()
except Exception as err:
 print(err)


结果:
1 A 男 20
2 B 女 21
由此可见 fetchall()能一次读取所有的行,读取的结果可以再次使用 for 循环得到每行数据。


7.2.3 【案例】学生数据表的管理 


1、案例描述
编写程序实现学生数据表 students 记录的查询、增加、更新、删除操作。
2、案例分析 
我们可以使用 insert,update,delete 等命令来更新数据库数据,每次执行:
cursor.execute(SQL)后可以使用 cursor.rowcount 来获取受影响的行数。
 
3、案例代码 


# /夜光
import pymysql
try:
 con = pymysql.connect(host="127.0.0.1", port=3306, user="root",passwd="1111",db="agvtest1", charset="utf8")
 cursor = con.cursor(pymysql.cursors.DictCursor)
 cursor.execute("delete from students")
 print(cursor.rowcount)
 cursor.execute("insert into students (pNo,pName,pGender,pAge) values ('1','A','男',20)")
 print(cursor.rowcount)
 cursor.execute("insert into students (pNo,pName,pGender,pAge) values ('2','B','女',21)")
 print(cursor.rowcount)
 cursor.execute("update students set pName='X' where pNo='1'")
 print(cursor.rowcount)
 cursor.execute("update students set pName='X' where pNo='3'")
 print(cursor.rowcount)
 cursor.execute("delete from students where pNo='1'")
 print(cursor.rowcount)
 cursor.execute("delete from students where pNo='3'")
 print(cursor.rowcount)
 con.commit()
 con.close()
except Exception as err:
 print(err)


结果:
2
1
1
1
0
1

由此可见 delete from students 命令删除 2 条记录,然后每条插入命令都插入 1 条记录,之后:

update students set pName='X' where pNo='1'
更新了一条记录,但是:
update students set pName='X' where pNo='3'
没有更新记录,因为没有 pNo='3'的记录。

同样:
delete from students where pNo='1'
删除了一条记录,但是:
delete from students where pNo='3'
没有删除记录,因为没有 pNo='3'的记录。
特别注意,所有数据库更新操作完毕后要记住执行 con.commit()命令,不然这些更新只在内存中,没有真正更新到数据库文件 students.db 中。

猜你喜欢

转载自blog.csdn.net/weixin_41987706/article/details/90200872