PyMySQL介绍
PyMySql包含一个纯python的MySQL客户端库。PyMySQL的目标是成为MySQLdb的替代品,并在CPython、PyPy和IronPython上工作。
版本要求
python 下列之一
CPython >= 2.6 or >= 3.3PyPy >= 4.0IronPython 2.7
mysql 下列之一
MySQL >= 4.1 (tested with only 5.5~)MariaDB >= 5.1
我的环境版本如下:
python
Mariadb
安装
直接pip方式安装:
pip install PyMySQL
pycharm安装与前面类似,如下图:
数据库增删改查
创建数据库testdb和表users
首先要先创建一个testdb 数据库,并且创建users表,创建语句如下:
MariaDB [(none)]> create database testdb;Query OK, 1 row affected (0.01 sec)MariaDB [(none)]> use testdb# 创建users表命令CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `email` varchar(255) COLLATE utf8_bin NOT NULL, `password` varchar(255) COLLATE utf8_bin NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_binAUTO_INCREMENT=1 ;
查看创建完的表结构:
MariaDB [testdb]> describe users;+----------+--------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------+--------------+------+-----+---------+----------------+| id | int(11) | NO | PRI | NULL | auto_increment || email | varchar(255) | NO | | NULL | || password | varchar(255) | NO | | NULL | |+----------+--------------+------+-----+---------+----------------+3 rows in set (0.00 sec)
连接数据库
连接数据库之前要确认以下条件是否满足:
已经创建了数据库 testdb;在testdb数据库中已经创建了users表;连接数据库TESTDB使用的用户名为 "testdb" ,密码为 "123456