0)Python3安装Mysql扩展命令:
pip install pymysql
1)建数据库:
CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8 */;
2)给库授权:
grant all privileges on test.* to root@127.0.0.1 identified by "test123";
flush privileges;
3)建表:
4)灌入数据:
5)查询:
mysql> select * from person;
+----+------------+------+--------+----------+------------+---------+
| id | name | age | sex | salary | hire_date | dept_id |
+----+------------+------+--------+----------+------------+---------+
| 1 | alex | 28 | 人妖 | 53000.00 | 2010-06-21 | 1 |
| 2 | wupeiqi | 23 | 男 | 8000.00 | 2011-02-21 | 1 |
| 3 | egon | 30 | 男 | 6500.00 | 2015-06-21 | 1 |
| 4 | jingnvshen | 18 | 女 | 6680.00 | 2014-06-21 | 1 |
+----+------------+------+--------+----------+------------+---------+
6)写Python连接代码,mysqlconn.py:
7)运行mysqlconn.py:
python mysqlconn.py
ID: 1 名字: alex 性别: 28
ID: 2 名字: wupeiqi 性别: 23
ID: 3 名字: egon 性别: 30
ID: 4 名字: jingnvshen 性别: 18
最后备注,mysql -h localhost -u root -p (/root/.my.cnf里默认是Localhost的密码)
cat /root/.my.cnf 里的密码,可以登录。
pip install pymysql
1)建数据库:
CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8 */;
2)给库授权:
grant all privileges on test.* to root@127.0.0.1 identified by "test123";
flush privileges;
3)建表:
4)灌入数据:
5)查询:
mysql> select * from person;
+----+------------+------+--------+----------+------------+---------+
| id | name | age | sex | salary | hire_date | dept_id |
+----+------------+------+--------+----------+------------+---------+
| 1 | alex | 28 | 人妖 | 53000.00 | 2010-06-21 | 1 |
| 2 | wupeiqi | 23 | 男 | 8000.00 | 2011-02-21 | 1 |
| 3 | egon | 30 | 男 | 6500.00 | 2015-06-21 | 1 |
| 4 | jingnvshen | 18 | 女 | 6680.00 | 2014-06-21 | 1 |
+----+------------+------+--------+----------+------------+---------+
6)写Python连接代码,mysqlconn.py:
7)运行mysqlconn.py:
python mysqlconn.py
ID: 1 名字: alex 性别: 28
ID: 2 名字: wupeiqi 性别: 23
ID: 3 名字: egon 性别: 30
ID: 4 名字: jingnvshen 性别: 18
最后备注,mysql -h localhost -u root -p (/root/.my.cnf里默认是Localhost的密码)
cat /root/.my.cnf 里的密码,可以登录。
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/3922/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2018-6-26 12:00
评论列表