1. 程式人生 > >資料庫及其python對資料庫的操作

資料庫及其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 [email protected] identified by 'hello';  #建立一個本地使用者hello,密碼為hello
create user [email protected]'%' identified by 'hello';  ##建立一個可遠端登陸的使用者hello
grant all on redhat.* to [email protected]
;
##給本地使用者hello賦予redhat資料庫下面所有表的所有許可權 show grants for [email protected]; #檢視所有的使用者授權 flush privileges; ##重新整理授權列表 revoke delete,update on redhat.* from [email protected]; ##取消本地使用者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()

這裡寫圖片描述