使用Ubuntu+Django+MySQL,有很多很多天坑!
这里首先尝试利用Python来对MySQL进行简单操作。
1、安装mysql
(1)去mysql官网下载社区版,选择Ubuntu系统及版本后(我用的是Ubuntu 18.04.3 LTS),会给出一个apt下载和管理的方案:MySQL APT Repository;
下载对应的deb文件:mysql-apt-config_0.8.13-1_all.deb,双击安装或者命令行安装都行,之后的步骤很反人类,反正我迄今都没搞清楚,总之只能选中ok,然后才能下一步;
mysql-apt-config_0.8.13-1_all.deb文件用来使Ubuntu用户通过apt-get的方式能够下载到最新的MySQL
(2)运行命令:sudo apt-get update
(3)运行命令:sudo apt-get install mysql-server
,这一步正式开始安装,中间要输入2次针对root用户的密码
(4)验证安装:
lzhshn@lzhshn-pc:~$ dpkg -l | grep mysql | grep ii ii libmysqlclient20:amd64 5.7.27-0ubuntu0.18.04.1 amd64 MySQL database client library ii libqt4-sql-mysql:amd64 4:4.8.7+dfsg-7ubuntu1 amd64 Qt 4 MySQL database driver ii mysql-apt-config 0.8.13-1 all Auto configuration for MySQL APT Repo. ii mysql-client 8.0.17-1ubuntu18.04 amd64 MySQL Client meta package depending on latest version ii mysql-common 8.0.17-1ubuntu18.04 amd64 Common files shared between packages ii mysql-community-client 8.0.17-1ubuntu18.04 amd64 MySQL Client ii mysql-community-client-core 8.0.17-1ubuntu18.04 amd64 MySQL Client Core Binaries ii mysql-community-server 8.0.17-1ubuntu18.04 amd64 MySQL Server ii mysql-community-server-core 8.0.17-1ubuntu18.04 amd64 MySQL Server Core Binaires ii mysql-server 8.0.17-1ubuntu18.04 amd64 MySQL Server meta package depending on latest version lzhshn@lzhshn-pc:~$
2、进入MySQL
lzhshn@lzhshn-pc:~$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 8 Server version: 8.0.17 MySQL Community Server - GPL Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql>
3、创建和查看数据库,这里创建了一个叫“test”的数据库
mysql> CREATE DATABASE test CHARACTER SET utf8; Query OK, 1 row affected, 1 warning (0.01 sec) mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.01 sec)
4、创建数据表(强调:在test数据库之下,创建数据表)
(1)使用use test,表示这个数据表在test数据库之下
(2)然后创建数据表,含表名,字段名,字段类型和长度,其中分号和逗号的书写方式一定不能出错
mysql> use test; Database changed mysql> CREATE TABLE user_info( -> username varchar(32), -> gender varchar(8) -> ); Query OK, 0 rows affected (0.03 sec)
5、安装pymysql,使用pip3 install pymysql --user,如果不加--user,会提示权限问题
6、尝试用python往数据表添加、插入一条数据
import pymysql conn = pymysql.connect( host='localhost', port=3306, user='root', password='19830427', database='test', charset='utf8', ) cursor = conn.cursor() sql = 'insert into user_info(username,gender) values(%s,%s);' username = 'jack' gender = 'male' cursor.execute(sql, [username, gender]) conn.commit() cursor.close() conn.close()
(1)建立和数据库的连接
(2)创建一个cursor
(3)赋值并把值写入数据表对应的字段下
(4)关闭事件和连接
7、查看添加的结果
mysql> select * from user_info; +----------+--------+ | username | gender | +----------+--------+ | jack | male | +----------+--------+ 1 row in set (0.00 sec) mysql>