建網(wǎng)站公司成都百度服務中心投訴
提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔
文章目錄
- 一、MYSQL
- 二、MySQL管理
- 查看已有數(shù)據(jù)庫
- 創(chuàng)建數(shù)據(jù)庫
- 刪除數(shù)據(jù)庫
- 進入數(shù)據(jù)庫
- 創(chuàng)建表
- 刪除表
- 展示表的行列
- 插入數(shù)據(jù)
- 查看表中的數(shù)據(jù)
- 刪除數(shù)據(jù)
- 修改數(shù)據(jù)
- 三、python代碼執(zhí)行數(shù)據(jù)庫操作
- 動態(tài)創(chuàng)建
- 查詢數(shù)據(jù)
- 刪除數(shù)據(jù)
- 修改數(shù)據(jù)
一、MYSQL
數(shù)據(jù)庫相當于我們常用的文件夾,數(shù)據(jù)表相當于我們常用說的文件,文件夾中存放文件,即數(shù)據(jù)庫中存放數(shù)據(jù)表。當要創(chuàng)建一個新表時,需要選定某個數(shù)據(jù)庫才能進行創(chuàng)建。
二、MySQL管理
查看已有數(shù)據(jù)庫
show databases;
創(chuàng)建數(shù)據(jù)庫
create database 數(shù)據(jù)庫名字 DEFAULT CHARSET utf8 COLLATE utf8_general_ci ;
create database demo2 DEFAULT CHARSET utf8 COLLATE utf8_general_ci ;
刪除數(shù)據(jù)庫
drop database 數(shù)據(jù)庫名字;
drop database demo2;
進入數(shù)據(jù)庫
use 數(shù)據(jù)庫名字;
use demo1;
注意:查看數(shù)據(jù)表(show tables;);創(chuàng)建數(shù)據(jù)表(create table 數(shù)據(jù)表名),需要先進入文件夾再查看,即use 連接使用。
創(chuàng)建表
create table tb11(
-> id int primary key, //主鍵 不能為空,表示唯一標識
-> name varchar(16) not null, //不能為空,(16不能少)
-> age int null //可以為空 (最后一項不能有”,“)
-> )default charset=utf8;
create table tb11(
id int primary key,
name varchar(16) not null,
age int null
)default charset=utf8;
注意: -> id int auto_increment primary key, //自增 1 2… √
刪除表
drop table 表名稱;
drop table tb2;
展示表的行列
desc 表名稱;
desc tb1;
插入數(shù)據(jù)
insert into 表名字(name,age) values(“嬋嬋”,22),(“ww”,19);
insert into tb1(id,name,age) values(1,"aa",20),(2,"bb",21);
查看表中的數(shù)據(jù)
select * from 表名字;
select * from tb1;
刪除數(shù)據(jù)
delete from 表名字;
delete from 表名字 where 條件 ;
eg: delete from tb33 where id = 1;
delete from tb33 where id = 1 and name=”aa”;
delete from tb33 where id <=4;
delete from tb1 where id=1;
修改數(shù)據(jù)
update 表名字set 列=值;
update 表名字set 列=值,列=值;
update 表名字set age=age+10 where 條件;
update tb1 set name="cc";
三、python代碼執(zhí)行數(shù)據(jù)庫操作
打開pycharm --創(chuàng)建新項目–終端寫入pip(版本號) install pymysql
動態(tài)創(chuàng)建
(注意縮進)
import pymysql
while True:username=input("用戶名:")if username.upper()=='Q':breakpassword=input("密碼:")mobile=input("手機號:")
# 1.連接MySQLconn=pymysql.connect(host="127.0.0.1",port=3306,user='root',passwd="12345",charset='utf8',db='unicom')cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 2.發(fā)送指令 (不能用字符串格式化去做MySQL的拼接,有安全隱患)sql="insert into admin(username,password,mobile) values(%s,%s,%s)"cursor.execute(sql,[username,password,mobile])conn.commit()
# 3.關閉cursor.close()conn.close()
其中passwd="12345"表示進入數(shù)據(jù)庫的密碼,db='unicom’是新建的數(shù)據(jù)庫名字,admin是新建表的名字,均可自行設置。
查詢數(shù)據(jù)
全部數(shù)據(jù): fetchall() 無數(shù)據(jù)為空列表
符合條件的第一個數(shù)據(jù): fetchone() 無數(shù)據(jù)是none
import pymysql
# 1.連接MySQL
conn=pymysql.connect(host="127.0.0.1",port=3306,user='root',passwd="12345",charset='utf8',db='unicom')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)# 2.執(zhí)行查詢的指令
cursor.execute("select * from admin ",)
# %s是exceute的占位符,
rew=cursor.fetchone()
# print(data_list)
# 得出的結果是以字典形式展示列表中的數(shù)據(jù)
print(rew)
# 3.關閉
cursor.close()
conn.close()
刪除數(shù)據(jù)
cursor.execute("delete from admin where id =%s",[8,])
8后面的逗號不能省略掉。
修改數(shù)據(jù)
cursor.execute("update admin set mobile=%s where id =%s",["49999999999",7,])
7后面的逗號不能省略掉。