数据库及其python对数据库的操作

数据库

设置mysql的登陆密码

mysql_secure_installation
mysql -uroot -p

数据库的基本操作语句

show databases  ##显示数据库
show tables  ##显示数据库中的表
desc user  ##显示数据库中表的结构
select * fron user;  ##显示表中的内容
create databases (数据库的名称);  ##建立一个数据库
create table (表的名称)(
    -> username varchar(10not null,
    -> passed varchar(6) not null
    ->);    ##创建一个表,自定义表格的内容
insert into (表格的名称) values('  ', '  ')      ##向表格中插入内容
insert into (表格的名称)(passed,username) values(' ',' ')  #按照指定内容向表格中插入
update (表的名称) set passwd='  ' where username = ''user1  ##修改表中的内容
alter table (表的名称) add (添加列名称);  ##向存在的表格例添加选项
delete from  (表的名称) where username = '  '  ##删除某一行的内容
drop table (表的名称) ; ##删除表
drop database (数据库的名称)  ##删库

用户和访问权限

create user hello@localhost identified by 'hello';  #创建一个本地用户hello,密码为hello
create user hello@'%' identified by 'hello';  ##创建一个可远程登陆的用户hello
grant all on redhat.* to hello@localhost;  ##给本地用户hello赋予redhat数据库下面所有表的所有权限
show grants for hello@localhost;  #查看所有的用户授权
flush privileges;  ##刷新授权列表
revoke delete,update on redhat.* from hello@localhost;  ##取消本地用户hello对数据库redhat 下所有表格的删除和更改权限

密码的找回

systemctl stop mariadb  #关闭数据库服务
mysqld_safe --skip-grants-table &  #跳过授权列表
mysql
>update mysql.user set Password=password('westos') where User='root';  #重新开辟一个shell 直接输入mysql会直接进入数据库,然后对对数据库进行进行相应的修改
ps aux | grep mysql  #查看与和数据库相关的进程
kill -9 pid  ##删除与数据库相关的所有进程
systemctl start mariadb ##开启数据库服务
##密码修改成功

mysql 的备份

mysqldump -uroot -p redhat > redhat.dump  ##将数据库中的内容备份到redaht.dump中
mysqladmin -uroot -proot -pwestos create redaht2  #创建一个空的数据库
mysql -uroot -pwestos redhat2 < redhat.dump  ##将redhat 中的 内容恢复到redhat2中

python中对数据库的操作

连接数据库,实现增删改查的操作

#1.连接数据库
conn = pymysql.connect(host='localhost',user='root',password='westos',charset=
                       'utf8',autocommit=True)#自动提交对数据库的操作

#2.创建一个游标,用来给sql发送语句
cur = conn.cursor()

#3.对于数据实现实现增删改查
#选择需要进行操作的数据库
conn.select_db('redhat')  ##选择要进行操作的数据库
# create_sql = 'create table myuser(name varchar(10) not null,age int)'  #创建一个表格
users = [(j,'user' + str(i)) for j in range(20,30) for i in range(10)]
# insert_sql = 'insert into myuser values(%s,%s);'  #向表格中插入内容
# use = ['user'+ str(i) for i in range(10)]
# insert_sql = 'insert into myuser values(%s,%s);'  
update_sql = 'update myuser set age=%s where name=%s'

# del_sql = 'delete from myuser where name=%s;'
# cur.executemany(update_sql,users) 
# cur.executemany(insert_sql,users) #批量对数据进行修改
#查看表中的数据
# select_sqli = 'select * from redhat'
# res = cur.execute(select_sqli)
# print(cur.fetchone())

#4.先关闭游标
cur.close()

#5.关闭数据连接
conn.cursor()

数据库上下文管理器


#安全管理器with
#1.连接数据库
conn = pymysql.connect(host='localhost',user='root',password='westos',
                       charset='utf8',autocommit=True,db='redhat')

with conn:
    print('is open',conn.open)
    #2.创建一个游标,用来给数据库发送sql语句
    cur = conn.cursor()
    #3.对于数据库进行增删改查操作
    #显示有多少行记录
    res = cur.execute('select * from myuser')
    print(res)
    #显示每列的详细信息
    desc = cur.description
    print(desc)
    cur.close()

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_41911569/article/details/82504089
今日推荐