python连接数据库
Python需要有一个模块,来实现与MySQL数据库的连接:PyMySQL
1.Python3 MySQL 数据库连接 - PyMySQL 驱动
PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb。
2.先安装(配置python环境变量)
a)进入cmd -->pip install pymysql
b)或pyCharm下安装
3.在.py文件中导入这个模块:
import pymysql
4.mysql数据库确认已安装
mysql -u root -p
Enter password:123456
show databases;
mysql> use testdb;
Database changed
mysql> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| employee |
+------------------+
#!/usr/bin/python
# -*- coding: UTF-8
import pymysql