准备工作
1、确保Python环境已安装mysql,py3.*版本:pip install PyMySQL
- PyMySQL是在Python3.x中用于连接MySQL服务器的一个库。
2、已经创建了数据库testDB,相关表table,以及表字段
3、数据库连接的host、用户名、密码
基础简单实现demo

- connect():连接到数据库。
- cursor():创建游标,用途->Create a new cursor to execute queries with。
- execute():执行sql,可执行数据库的增删改查等各种操作。
- commit():更新游标的所有操作。
- rollback():回滚当前游标的所有操作。
- db.close():关闭数据库连接。
占位符 %
举个例子

数据库查询
- fetchone():获取下一个查询结果集。
- fetchall():接收全部的返回结果行。通过for循环可以访问。
- cursor.execute(sql):返回执行后记录的条数。
例如:

--- 未完待续,持续更新ing ---
网友评论