Day42-数据库入门学习-正则表达式、用户管理、pymysql模块

 

一、正则表达式

正则表达式用于模糊查询,模糊查询已经讲过了
like 仅支持 % 和 _ 远没有正则表达式灵活
当然绝大多数情况下 like足够使用

#语法
select *from table where name regexp "正则表达式";

#实例
#准备数据
create table emp (id int,name char(10),sex char,age int,dept_id int,job char(10),salary double);
insert into emp values
(1,"刘备","",26,1,"总监",5800),
(2,"张飞","",24,1,"员工",3000),
(3,"关羽","",30,1,"员工",4000),
(4,"孙权","",25,2,"总监",6000),
(10,"刘备2","",26,2,"总监",5800),
(5,"周瑜","",22,2,"员工",5000),
(6,"小乔","",31,2,"员工",4000),
(7,"曹操","",19,3,"总监",10000),
(8,"司马懿","",24,3,"员工",6000);


mysql> select *from emp where name regexp('司*');
+------+-----------+------+------+---------+--------+--------+
| id   | name      | sex  | age  | dept_id | job    | salary |
+------+-----------+------+------+---------+--------+--------+
|    8 | 司马懿    | 男   |   24 |       3 | 员工   |   6000 |
+------+-----------+------+------+---------+--------+--------+


mysql> select *from emp where name regexp('懿$');
+------+-----------+------+------+---------+--------+--------+
| id   | name      | sex  | age  | dept_id | job    | salary |
+------+-----------+------+------+---------+--------+--------+
|    8 | 司马懿    | 男   |   24 |       3 | 员工   |   6000 |
+------+-----------+------+------+---------+--------+--------+

二、用户管理

MYSQL 是一个tcp 服务器,用于操作服务器上的文件数据,接收用户端发送的指令, 接收指令时需要考虑安全问题

atm 购物车中的用户认证和mysql的用户认证原理是一样的,mysql中把文件称为表


在mysql自带的mysql数据库中有4个表用于用户管理的
优先级从高到低,分别是: user -> db -> tables_priv -> columns_priv

注:对用户管理进行操作后,要使用flush privileges;刷新

1.创建用户

#语法
create user 用户名@'客户端地址' identified by '密码';


#实际操作(在本地上操作)
mysql> create user xcq@'localhost' identified by '123';
Query OK, 0 rows affected (0.13 sec)


#查看是否创建完成
mysql> use mysql

mysql> select *from user G;
************************** 4. row ***************************
                  Host: localhost
                  User: xcq
              Password: *23AE809DDACAF96AF0FD78ED04B6A265E05AA257
#但此时的用户没有任何的权限

2.授权

#语法
grant 全部[all]/权限名称[select(字段名),updata(字段名),alter(字段名),delete(字段名)] on 数据库名.表名 to 用户名@'主机地址'#可以访问 所有库和表
grant all on *.* to xcq@"localhost" identified by "123";
#可以访问day42库的所有表
grant all on day42.* to xcq@"localhost" identified by "123";  
#可以访问day42库的emp表
grant all on day42.emp to xcq@"localhost" identified by "123"; 
#仅能查看和添加 day42库的emp表中的 id和name字段
grant select(id,name),insert(id,name) on day42.emp to xcq@"localhost" identified by "123";

#all可以给用户添加除了grant(赋予权限)以外的所有权限
#如果想把拥有的权限赋予别人
#使用 with grant option 
#如果授权时,被授权的用户不存在,直接创建用户
grant 全部[all]/权限名称[select(字段名),updata(字段名),alter(字段名),delete(字段名)] on 数据库名.表名 to 用户名@'主机地址' with grant option;

3.删除权限

#语法
revoke 权限的名称 on 数据库名.表名  from 用户名@'主机地址';

#实例
revoke all on *.* from xcq@'localhost';

4.删除用户

#语法
 drop user 用户名@"主机地址";

#实例
drop user xcq@'localhost';

注:每次对用户进行操作都要刷新权限flush privileges;

三、pymysql模块

1.pymysql

python编写的mysql客户端
pip install pymysql

2.基本使用

import pymysql

#1.链接数据库
conn=pymysql.Connect(
    host='127.0.0.1',#服务器地址
    user='root',#用户名
    password='root',#密码
    database='day41',#数据库名称
    port=3306,#端口号,可选
    charset='utf8'#编码,可选
)

#2.获取游标对象
cursor=conn.cursor(pymysql.cursors.DictCursor)

#3.sql语句
sql='select *from emp'


#4.执行语句
res=cursor.execute(sql)#返回执行的行数
print(res)


# fetchallfetchonefetchmany   获取查询结果
# print(cursor.fetchall())#全部取出来

# print(cursor.fetchone())#每次取一个,然后游标往下一位
# print(cursor.fetchone())#每次取一个,然后游标往下一位
#
# print(cursor.fetchmany(1))#取的时候指定行数
# print(cursor.fetchall())#将剩下全部取出来

#scroll    游标移动
# cursor.scroll(1,'relative')#个数与模式。relative相对位置;absolute绝对位置



#5.关闭游标
cursor.close()

#6.关闭链接
conn.close()

3.commit和rollback

import pymysql

#1.链接数据库
conn=pymysql.Connect(
    host='127.0.0.1',#服务器地址
    user='root',#用户名
    password='root',#密码
    database='day41',#数据库名称
    port=3306,#端口号,可选
    charset='utf8'#编码,可选
)

#2.获取游标对象
cursor=conn.cursor(pymysql.cursors.DictCursor)

#3.sql语句
sql='insert into emp value(15,"黄月英","女","市场","员工",4500)'


#4.执行语句
res=cursor.execute(sql)#返回执行的行数
print(res)


cur=cursor.fetchall()
for i in cur:
    print(i)

#撤销数据,只要数据还没提交,都可以撤销
#conn.rollback()

#提交数据,因为pymysql 默认是启用事务,对记录操作的sql语句如果不提交,不会执行。
#但是对于库和表得操作会默认提交
conn.commit();

#5.关闭游标
cursor.close()

#6.关闭链接
conn.close()
原文地址:https://www.cnblogs.com/xvchengqi/p/9662818.html