MySql 基礎 pymysql的基本使用方法
阿新 • • 發佈:2018-09-09
字段 avg -- avi com chm 啟動 efault cut
安裝: 初始化 D:\mysql-8.0.12-winx64\mysql-8.0.12-winx64\bin\mysqld --initialize-insecure 運行服務端 D:\mysql-8.0.12-winx64\mysql-8.0.12-winx64\bin\mysqld 運行客戶端 D:\mysql-8.0.12-winx64\mysql-8.0.12-winx64\bin\mysql -u root -p # 查看數據庫 show databases; # 創建數據庫 creat database db1; #安裝windows服務: # C:\Windows\system32>D:\mysql-8.0.12-winx64\mysql-8.0.12-winx64\bin\mysqld --install [--remove移除] # 添加後可以在windows的服務裏看到“MySQL”,管理員身份運行cmd,輸入net start/stop MySQL即可啟動 登陸數據庫後創建用戶 create user ‘username‘@‘ip‘ identified by ‘userpwd‘; # ip 填 % 表示所有的ip地址都可登陸 為用戶授權 grant all privileges on db.t to‘username‘@‘ip‘; # all privileges表示除授權權限外擁有所有的權限 也可填select,delete (查權限和刪權限)等 # db.t 表示對哪個數據庫下的哪個表有此權限,填*.*意味著擁有 所有數據庫下的所有表 的權限 取消授權: revoke update,delete on db.t1 from ‘username‘@‘ip‘; 創建數據庫 create database db2 default charset utf8; # 設置默認編碼 刪除 drop database 數據庫名; 查 show databases; 創建表 create table t1(id int auto_increment primary key,name char(10))engine=innodb default charset=utf8; 刪除表 drop table 表名; 清空表: delete from 表名; # 自增的id不重置 truncate table 表名; # 自增的id重置 表的增刪改查 insert into 表名(列名,列名2) value(列1值,列2值); delete from 表名 where 條件; update 表名 set 列=值 where 條件; select * from 表名; 外鍵: create table t1( uid int auto_increment primary key, name varchar(32), t2_id int, constraint fk_user_userinfo foreign key (t2_id) references userinfo(id) )engine=innodb default charset=utf8; create table t2( id int auto_increment primary key, sex char(1) )engine=innodb default charset=utf8; innodb原子操作 navicat 鏈接數據庫報錯 # 2059 - Authentication plugin ‘caching_sha2_password‘ cannot be loaded: ...... # use mysql # select user,host,plugin,authentication_string from user; # alter user ‘mingyaun‘@‘%‘ identified with mysql_native_password by ‘123456‘;
desc tabl_name;
顯示表結構,字段類型,主鍵,是否為空等屬性,但不顯示外鍵。
show create table table_name;
該命令把創建表的DDL顯示出來,於是表結構、類型,外鍵,備註全部顯示出來了。
關鍵字 - in not in - between and - limit - group by having - order by - like "%a" - left join xx on 關系 - select * from (select * from tb where id< 10) as B; - select (select num from score as s2 where s2.student_id=s1.student_id and course_id = 1) as y,from score as s1; - case when min(num) <10 THEN 0 ELSE min(num) END - select avg(num),sum(case when num <60 THEN 0 ELSE 1 END) from score GROUP BY course_id order by AVG(num) asc,jgl desc;
import pymysql # conn = pymysql.connect(host="localhost",user=‘root‘,password=‘‘,database="db666") # cursor = conn.cursor() # # sql = "select * from userinfo where username=‘%s‘ and password=‘%s‘" %(user,pwd,) # # select * from userinfo where username=‘uu‘ or 1=1 -- ‘ and password=‘%s‘ # # ret = cursor.execute(sql) # # result = cursor.fetchone() # sql = "select * from userinfo where username=%s and password=%(p)s" # cursor.execute(sql,user,pwd) # cursor.execute(sql,[user,pwd]) # cursor.execute(sql,{‘u‘:user,‘p‘:pwd}) # cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)]) # conn.commit() # 增加,刪,改 # result = cursor.fetchone() # result = cursor.fetchall() # result = cursor.fetchmany(4) # 新插入數據的自增ID: cursor.lastrowid # cursor.close() # conn.close()
MySql 基礎 pymysql的基本使用方法