怎么做網(wǎng)站信息合肥網(wǎng)站優(yōu)化搜索
MySQL本地安裝和相關(guān)操作
-
Python相關(guān):基礎(chǔ)、函數(shù)、數(shù)據(jù)類型、面向、模塊。
-
前端開發(fā):HTML、CSS、JavaScript、jQuery。【靜態(tài)頁面】
Java+前端; Python+前端; Go+前端 -> 【動(dòng)態(tài)頁面】
直觀:
- 靜態(tài),寫死了,頁面永遠(yuǎn)長一個(gè)樣子。
- 動(dòng)態(tài)頁面,頁面上的數(shù)據(jù)可以實(shí)時(shí)的修改和展示。
1. 初識(shí)網(wǎng)站
- 默認(rèn)編寫的靜態(tài)的效果
- 動(dòng)態(tài):需要用到Web框架的功能。
對于目前的我們來看,都什么可以做數(shù)據(jù)的存儲(chǔ):
-
txt文件
-
excel文件
-
專業(yè)的軟件:數(shù)據(jù)庫管理系統(tǒng)。
MySQL(*)免費(fèi) Oracle/SQLServer/DB2/Access...
今日概要:
- MySQL安裝 & 配置
- MySQL的啟動(dòng)和關(guān)閉
- 指令(*)
- Python第三方模塊,發(fā)送指令并獲取MySQL返回的結(jié)果。
2.安裝MySQL
MySQL,本質(zhì)上就是一個(gè)軟件。2024年之后推薦使用8版本以上
- 8.x
2.1 下載
https://downloads.mysql.com/archives/community/
- MySQL壓縮包
2.3 安裝
mysql-8.0.4.0-winx64.zip 是免安裝的版本。
- 解壓zip文件
- 將解壓后的文件夾放入路徑(不要有中文路徑)
2.4 創(chuàng)建配置文件
在根目錄下創(chuàng)建一個(gè)txt文件,名字叫my,文件后綴為ini
之后復(fù)制下面這個(gè)代碼放在文件下
(新解壓的文件沒有my.ini文件,需自行創(chuàng)建)
以下代碼除安裝目錄和數(shù)據(jù)的存放目錄需修改,其余不用修改
[mysqld]
# 設(shè)置3306端口
port=3306
# 設(shè)置mysql的安裝目錄 ----------是你的文件路徑-------------
basedir=E:\mysql\mysql
# 設(shè)置mysql數(shù)據(jù)庫的數(shù)據(jù)的存放目錄 ---------是你的文件路徑data文件夾自行創(chuàng)建
datadir=E:\mysql\mysql\data
# 允許最大連接數(shù)
max_connections=200
# 允許連接失敗的次數(shù)。
max_connect_errors=10
# 服務(wù)端使用的字符集默認(rèn)為utf8mb4
character-set-server=utf8mb4
# 創(chuàng)建新表時(shí)將使用的默認(rèn)存儲(chǔ)引擎
default-storage-engine=INNODB
# 默認(rèn)使用“mysql_native_password”插件認(rèn)證
#mysql_native_password
default_authentication_plugin=mysql_native_password
[mysql]
# 設(shè)置mysql客戶端默認(rèn)字符集
default-character-set=utf8mb4
[client]
# 設(shè)置mysql客戶端連接服務(wù)端時(shí)默認(rèn)使用的端口
port=3306
default-character-set=utf8mb4
2.5 初始化
-
打開終端 & 以管理員的權(quán)限去運(yùn)行
-
輸入初始化的命令
"D:\ProgramData\mysql8\mysql-8.0.40-winx64\bin\mysqld.exe" --initialize-insecure
至此,MySQL的安裝已完成。
3.啟動(dòng)MySQL
啟動(dòng)MySQL一般有兩種方式:
- 臨時(shí)啟動(dòng)(不建議)
"C:\Program Files\mysql-5.7.31-winx64\bin\mysqld.exe"
-
制作成Windows服務(wù),服務(wù)來進(jìn)行關(guān)閉和開啟。
- 制作服務(wù)
"C:\Program Files\mysql-5.7.31-winx64\bin\mysqld.exe" --install mysql57
net start mysql57net stop mysql57
-
啟動(dòng)和關(guān)閉服務(wù)
-
也可以在window的服務(wù)管理中點(diǎn)擊按鈕啟動(dòng)和關(guān)閉服務(wù)。例如:
4.連接測試
>>>"C:\Program Files\mysql-5.7.31-winx64\bin\mysql.exe" -h 127.0.0.1 -P 3306 -u root -p
>>>"C:\Program Files\mysql-5.7.31-winx64\bin\mysql.exe" -u root -p
如果你將 C:\Program Files\mysql-5.7.31-winx64\bin\
添加到了系統(tǒng)環(huán)境變量。
>>> mysql -u root -p
4.1 設(shè)置密碼
set password = password('root123');
4.2 查看已有的文件夾(數(shù)據(jù)庫)
show databases;
4.3 退出(關(guān)閉連接)
exit;
4.4. 再連接MySQL
匯總命令:
C:\Users\Administrator>mysql -u root -p
mysql> set password = password('root123');
mysql> show databases;
mysql> exit;
C:\Users\Administrator>mysql -u root -p
輸入密碼
mysql>exit;
5.忘記密碼
默認(rèn)情況下,啟動(dòng)MySQL時(shí),需要用戶輸入賬戶名、密碼。修改MySQL配置,重新啟動(dòng)MySQL(無賬號(hào)模式)mysql -u root -p 重新設(shè)置密碼退出再重新修MySQL配置文件,重新啟動(dòng)MySQL(需要賬號(hào)的模式)mysql -u root -p 新密碼
-
停止現(xiàn)在MySQL服務(wù)
-
修改MySQL配置文件(以無賬號(hào)模式)
-
重新啟動(dòng)MySQL
-
再次登錄MySQL(無需密碼)
-
執(zhí)行命令設(shè)置密碼
use mysql;
update user set authentication_string = password('新密碼'),password_last_changed=now() where user='root';update user set authentication_string = password('root123'),password_last_changed=now() where user='root';
-
重新修改配置文件(需要賬號(hào)的模式登錄)【服務(wù)停掉】
-
重新啟動(dòng)MySQL
-
登錄時(shí)候輸入新的密碼即可。
小結(jié)
支持,MySQL的環(huán)境搭建相關(guān)的事全部搞定了。
- 安裝
- 配置
- 啟動(dòng)
- 連接(密碼、忘記密碼)
以后我們再操作MySQL時(shí):
-
關(guān)閉和開啟MySQL服務(wù)
-
用MySQL自動(dòng)工具連接MySQL并發(fā)送指令
myql -u root -p
6.MySQL指令
在MySQL和我們平時(shí)認(rèn)知不同的概念。
MySQL | 認(rèn)知 |
---|---|
數(shù)據(jù)庫 | 文件夾 |
數(shù)據(jù)表 | 文件(Excel文件) |
6.1 數(shù)據(jù)庫管理(文件夾)
-
查看已有的數(shù)據(jù)庫(文件夾)
show databases;
-
創(chuàng)建數(shù)據(jù)庫(文件夾)
create database 數(shù)據(jù)庫名字 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
create database gx_day14 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
-
刪除數(shù)據(jù)庫(文件夾)
drop database gx_day14;
-
進(jìn)入數(shù)據(jù)庫(進(jìn)入文件夾)
use gx_day14;
-
查看文件夾下所有的數(shù)據(jù)表(文件)
show tables;
6.2 數(shù)據(jù)表的管理(文件)
-
進(jìn)入數(shù)據(jù)庫(進(jìn)入文件夾)
use 數(shù)據(jù)庫; use gx_day14;
-
查看當(dāng)前數(shù)據(jù)庫下的所有 表(文件)
show tables;
-
創(chuàng)建表(文件文件)
create table 表名稱(列名稱 類型,列名稱 類型,列名稱 類型 )default charset=utf8;
create table tb1(id int, name varchar(16),age int) default charset=utf8;
create table tb1(id int, name varchar(16),age int ) default charset=utf8;
create table tb1(id int, name varchar(16) not null, -- 不允許為空age int null, -- 允許為空(默認(rèn)) ) default charset=utf8;
create table tb1(id int, name varchar(16),age int default 3 -- 插入數(shù)據(jù)時(shí),age列的值默認(rèn)3 ) default charset=utf8;
create table tb1(id int primary key, -- 主鍵(不允許為空,不允許重復(fù))name varchar(16),age int ) default charset=utf8;
主鍵一般用于表示當(dāng)前行的數(shù)據(jù)的編號(hào)(類似于人的身份證)。
create table tb1(id int auto_increment primary key, -- 內(nèi)部維護(hù),自增name varchar(16),age int ) default charset=utf8;
一般情況下,我們再創(chuàng)建表時(shí)都會(huì)這樣來寫:【標(biāo)準(zhǔn)】
create table tb1(id int not null auto_increment primary key,name varchar(16),age int ) default charset=utf8;
mysql> desc tb1; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(16) | YES | | NULL | | | age | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
-
刪除表
drop table 表名稱;
常用數(shù)據(jù)類型:
-
tinyint
有符號(hào),取值范圍:-128 ~ 127 (有正有負(fù))【默認(rèn)】 無符號(hào),取值范圍:0 ~ 255(只有正)
create table tb2(id int not null auto_increment primary key,age tinyint -- 有符號(hào):取值范圍:-128 ~ 127 ) default charset=utf8;
create table tb3(id int not null auto_increment primary key,age tinyint unsigned -- 無符號(hào):取值范圍:0 ~ 255 ) default charset=utf8;
-
int
int 表示有符號(hào),取值范圍:-2147483648 ~ 2147483647 int unsigned 表示無符號(hào),取值范圍:0 ~ 4294967295
-
bigint
有符號(hào),取值范圍:-9223372036854775808 ~ 9223372036854775807 無符號(hào),取值范圍:0 ~ 18446744073709551615
練習(xí)題:
# 創(chuàng)建表 create table tb2(id bigint not null auto_increment primary key,salary int,age tinyint ) default charset=utf8;# 插入數(shù)據(jù) insert into tb2(salary,age) values(10000,18); insert into tb2(salary,age) values(20000,28); insert into tb2(salary,age) values(30000,38),(40000,40);# 查看表中的數(shù)據(jù) select * from tb2;
mysql> show tables; +--------------------+ | Tables_in_gx_day14 | +--------------------+ | tb1 | +--------------------+ 1 row in set (0.00 sec)mysql> create table tb2(-> id bigint not null auto_increment primary key,-> salary int,-> age tinyint-> ) default charset=utf8; Query OK, 0 rows affected (0.03 sec)mysql> show tables; +--------------------+ | Tables_in_gx_day14 | +--------------------+ | tb1 | | tb2 | +--------------------+ 2 rows in set (0.00 sec)mysql> insert into tb2(salary,age) values(10000,18); Query OK, 1 row affected (0.00 sec)mysql> insert into tb2(salary,age) values(20000,28); Query OK, 1 row affected (0.00 sec)mysql> insert into tb2(salary,age) values(30000,38),(40000,40); Query OK, 2 rows affected (0.01 sec) Records: 2 Duplicates: 0 Warnings: 0mysql> select * from tb2; +----+--------+------+ | id | salary | age | +----+--------+------+ | 1 | 10000 | 18 | | 2 | 20000 | 28 | | 3 | 30000 | 38 | | 4 | 40000 | 40 | +----+--------+------+ 4 rows in set (0.00 sec)
-
float
-
double
-
decimal (用于表示精準(zhǔn)數(shù))
準(zhǔn)確的小數(shù)值,m是數(shù)字總個(gè)數(shù)(負(fù)號(hào)不算),d是小數(shù)點(diǎn)后個(gè)數(shù)。 m最大值為65,d最大值為30。例如: create table tb3(id int not null primary key auto_increment,salary decimal(8,2) )default charset=utf8;insert into tb3(salary) values(1.28); insert into tb3(salary) values(5.289); insert into tb3(salary) values(5.282); insert into tb3(salary) values(122115.11);select * from tb3;
-
char(m),速度快。
定長字符串,m代表字符串的長度,最多可容納255個(gè)字符。char(11),固定用11個(gè)字符串進(jìn)行存儲(chǔ),哪怕真是沒有11個(gè)字符,也會(huì)按照11存儲(chǔ)。create table tb4(id int not null primary key auto_increment,mobile char(11) )default charset=utf8;insert into tb4(mobile) values("151"); insert into tb4(mobile) values("15131255555");
-
varchar(m),節(jié)省空間。
變長字符串,m代表字符的長度。 最大65535字節(jié)/3 = 最大的mvarchar(11),真實(shí)數(shù)據(jù)有多少長久按照多長存儲(chǔ)。create table tb5(id int not null primary key auto_increment,mobile varchar(11) )default charset=utf8;insert into tb5(mobile) values("151"); insert into tb5(mobile) values("15131255555");
-
text
text數(shù)據(jù)類型用于保存變長的大字符串,可以組多到65535 (2**16 ? 1)個(gè)字符。一般情況下,長文本會(huì)用text類型。例如:文章、新聞等。create table tb6(id int not null primary key auto_increment,title varchar(128),content text )default charset=utf8;
-
mediumtext 可承載的字符更多
A TEXT column with a maximum length of 16,777,215 (2**24 ? 1) characters.
-
longtext
A TEXT column with a maximum length of 4,294,967,295 or 4GB (2**32 ? 1)
-
datetime 用于承載年月日時(shí)分秒
YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59)
-
date
YYYY-MM-DD(1000-01-01/9999-12-31)
練習(xí)題:用戶表
create table tb7(id int not null primary key auto_increment,name varchar(64) not null,password char(64) not null,email varchar(64) not null,age tinyint,salary decimal(10,2),ctime datetime
)default charset=utf8;insert into tb7(name,password,email,age,salary,ctime) values("武沛齊","123","xx@live.com",19,1000.20,"2011-11-11 11:11:10");insert into tb7(name,password,email,age,salary,ctime) values("張電摩","123","xx@live.com",19,1000.20,"2011-11-11 11:11:10");insert into tb7(name,password,email,age,salary,ctime) values("龐小青","123","xx@live.com",19,1000.20,"2011-11-11 11:11:10");insert into tb7(name,password,email,age,salary,ctime) values("謝濤","123","xx@live.com",19,1000.20,"2011-11-11 11:11:10");insert into tb7(name,password,email,age,salary,ctime) values("謝鵬","123","xx@live.com",19,1000.20,"2011-11-11 11:11:10");select * from tb7;+----+-----------+----------+-------------+------+---------+---------------------+
| id | name | password | email | age | salary | ctime |
+----+-----------+----------+-------------+------+---------+---------------------+
| 1 | 武沛齊 | 123 | xx@live.com | 19 | 1000.20 | 2011-11-11 11:11:10 |
+----+-----------+----------+-------------+------+---------+---------------------+
1 row in set (0.00 sec)
MySQL還有很多其他的數(shù)據(jù)類型,例如:set、enum、TinyBlob、Blob、MediumBlob、LongBlob 等,詳細(xì)見官方文檔:https://dev.mysql.com/doc/refman/5.7/en/data-types.html
我們平時(shí)開發(fā)系統(tǒng)時(shí),一般情況下:
- 創(chuàng)建數(shù)據(jù)庫
- 創(chuàng)建表結(jié)構(gòu)
都是需要提前通過上述命令創(chuàng)建。
6.3 數(shù)據(jù)行操作
1. 新增數(shù)據(jù)
insert into 表名(列名,列名) values(值,值);
insert into 表名(列名,列名) values(值,值),(值,值),(值,值),(值,值);
2.刪除數(shù)據(jù)
delete from 表名;
delete from 表名 where 條件;
delete from tb7;
delete from tb7 where id = 3;
delete from tb7 where id = 4 and name="謝濤";
delete from tb7 where id = 4 or name="謝濤";
delete from tb7 where id > 4;
delete from tb7 where id >= 4;
delete from tb7 where id != 4;
delete from tb7 where id in (1,5);
3.修改數(shù)據(jù)
update 表名 set 列=值;
update 表名 set 列=值,列=值;
update 表名 set 列=值 where 條件;
update tb7 set password="哈哈哈";
update tb7 set email="哈哈哈" where id > 5;update tb7 set age=age+10 where id > 5;
4.查詢數(shù)據(jù)
select * from 表名稱;
select 列名稱,列名稱 from 表名稱;select 列名稱,列名稱 from 表名稱 where 條件;
select * from tb7;
select id,name from tb7;
select id,name from tb7 where id > 10;
select id,name from tb7 where name="xx" and password="xx";
小結(jié)
我們平時(shí)開發(fā)系統(tǒng)時(shí),一般情況下:
- 創(chuàng)建數(shù)據(jù)庫
- 創(chuàng)建表結(jié)構(gòu)
都是需要提前通過工具+命令創(chuàng)建。
但是,表中的數(shù)據(jù)一般情況下都是通過程序來實(shí)現(xiàn)增刪改查。
7.案例:員工管理
-
使用MySQL內(nèi)置工具(命令)
-
創(chuàng)建數(shù)據(jù)庫:unicom
-
數(shù)據(jù)一張表:admin
表名:admin 列:id,整型,自增,主鍵。username 字符串 不為空,password 字符串 不為空,mobile 字符串 不為空
-
-
Python代碼實(shí)現(xiàn):
- 添加用戶
- 刪除用戶
- 查看用戶
- 更新用戶信息
7.1 創(chuàng)建表結(jié)構(gòu)
create database unicom DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
use unicom;
create table admin(id int not null auto_increment primary key,username varchar(16) not null,password varchar(64) not null,mobile char(11) not null
) default charset=utf8;
7.2 Python操作MySQL
打開pycahrm用Python代碼連接MySQL并發(fā)送指令。
pip install pymysql
1.創(chuàng)建數(shù)據(jù)
import pymysql# 1.連接MySQL
conn = pymysql.connect(host="127.0.0.1", port=3306, user='root', passwd="root123", charset='utf8', db='unicom')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)# 2.發(fā)送指令
cursor.execute("insert into admin(username,password,mobile) values('wupeiqi','qwe123','15155555555')")
conn.commit()# 3.關(guān)閉
cursor.close()
conn.close()
import pymysql# 1.連接MySQL
conn = pymysql.connect(host="127.0.0.1", port=3306, user='root', passwd="root123", charset='utf8', db='unicom')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)# 2.發(fā)送指令(千萬不要用字符串格式化去做SQL的拼接,安全隱患SQL注入)
# sql = "insert into admin(username,password,mobile) values(%s,%s,%s)"
# cursor.execute(sql, ["韓超", "qwe123", "1999999999"])# sql = "insert into admin(username,password,mobile) values( %(n1)s, %(n2)s, %(n3)s)"
# cursor.execute(sql, {"n1": "集寧", "n2": "qwe123", "n3": "1999999999"})conn.commit()# 3.關(guān)閉
cursor.close()
conn.close()
import pymysqlwhile True:user = input("用戶名:")if user.upper() == 'Q':breakpwd = input("密碼:")mobile = input("手機(jī)號(hào):")# 1.連接MySQLconn = pymysql.connect(host="127.0.0.1", port=3306, user='root', passwd="root123", charset='utf8', db='unicom')cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)# 2.發(fā)送指令(千萬不要用字符串格式化去做SQL的拼接,安全隱患SQL注入)sql = "insert into admin(username,password,mobile) values(%s,%s,%s)"cursor.execute(sql, [user, pwd, mobile])conn.commit()# 3.關(guān)閉cursor.close()conn.close()
2.查詢數(shù)據(jù)
import pymysql# 1.連接MySQL
conn = pymysql.connect(host="127.0.0.1", port=3306, user='root', passwd="root123", charset='utf8', db='unicom')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)# 2.發(fā)送指令( *** 千萬不要用字符串格式化去做SQL的拼接,安全隱患SQL注入***)
cursor.execute("select * from admin where id > %s", [2, ])# 獲取符合條件的所有數(shù)據(jù),得到的是 [ 字典,字典, ] 空列表
data_list = cursor.fetchall()
for row_dict in data_list:print(row_dict)# 3.關(guān)閉連接
cursor.close()
conn.close()
import pymysql# 1.連接MySQL
conn = pymysql.connect(host="127.0.0.1", port=3306, user='root', passwd="root123", charset='utf8', db='unicom')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)# 2.發(fā)送指令( *** 千萬不要用字符串格式化去做SQL的拼接,安全隱患SQL注入***)
cursor.execute("select * from admin where id > %s", [2, ])# 獲取符合條件的第一條數(shù)據(jù),字典 None
res = cursor.fetchone()
print(res) # {'id': 3, 'username': '集寧', 'password': 'qwe123', 'mobile': '1999999999'}# 3.關(guān)閉連接
cursor.close()
conn.close()
3.刪除數(shù)據(jù)
import pymysql# 1.連接MySQL
conn = pymysql.connect(host="127.0.0.1", port=3306, user='root', passwd="root123", charset='utf8', db='unicom')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)# 2.發(fā)送指令( *** 千萬不要用字符串格式化去做SQL的拼接,安全隱患SQL注入***)
cursor.execute("delete from admin where id=%s", [3, ])
conn.commit()# 3.關(guān)閉
cursor.close()
conn.close()
4.修改數(shù)據(jù)
import pymysql# 1.連接MySQL
conn = pymysql.connect(host="127.0.0.1", port=3306, user='root', passwd="root123", charset='utf8', db='unicom')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)# 2.發(fā)送指令( *** 千萬不要用字符串格式化去做SQL的拼接,安全隱患SQL注入***)
cursor.execute("update admin set mobile=%s where id=%s", ["1888888888", 4, ])
conn.commit()# 3.關(guān)閉
cursor.close()
conn.close()
強(qiáng)調(diào):
-
在進(jìn)行 新增、刪除、修改時(shí),一定要記得commit,不然數(shù)據(jù)庫么有數(shù)據(jù)。
cursor.execute("..") conn.commit()
-
在查詢時(shí),不需要commit,執(zhí)行fetchall / fetchone
cursor.execute("...")# 第一條數(shù)據(jù),字典,無數(shù)據(jù)時(shí)是空列表 v1 = cursor.fetchone()# 所有數(shù)據(jù),列表套字典,無數(shù)據(jù)時(shí)是None v1 = cursor.fetchall()
-
對于SQL語句不要用Python的字符串格式化進(jìn)行拼接(會(huì)被SQL注入),一定要用execute+參數(shù)
cursor.execute(".%s..... %s", ["xx","xx"])
8.案例:Flask+MySQL
8.1 新增用戶
補(bǔ)充代碼實(shí)現(xiàn)