新秀篇 ##python中数据库的应用与管理##

数据库:

MySQL 是 Web 世界中使用最广泛的数据库服务器。
MySQL 内部有多种数据库引擎,最常用的引擎是支持数
据库事务的 InnoDB。

一.数据库的安装与搭建:

• 一定要进行设置密码,确保数据库安全;
• 配置文件为/etc/my.cnf;
• 修改数据库默认的编码全部改为 UTF­8;
• 查看编码格式:show variables like ‘%char%’;
• 安装MySQL­python,封装了 MySQL C 驱动的 Python 驱动。
1.主机上下载数据库:



[root@foundation39 ~]# yum install mariadb-server -y
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-
              : manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
[root@foundation39 ~]# systemctl start mariadb
[root@foundation39 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> quit
Bye
[root@foundation39 ~]# mysql_secure_installation      #修改超级用户密码

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
[root@foundation39 ~]# mysql -uroot -p    ##重新登陆检测密码
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> quit
Bye

2.下载数据库与python的附属包:

[root@foundation39 ~]# yum install gcc -y
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-
              : manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Resolving Dependencies
[root@foundation39 ~]# yum install MySQL-python.x86_64 -y
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-
              : manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Resolving Dependencies
[root@foundation39 ~]# pip install MySQL-Python
You are using pip version 7.1.0, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Requirement already satisfied (use --upgrade to upgrade): MySQL-Python in /usr/lib64/python2.7/site-packages
**然后在pycharm上执行import MySQLdb可以导入**

3.打开网络数据库:

[root@foundation39 Desktop]# cd /var/www/html
[root@foundation39 html]# tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 
[root@foundation39 html]# ls
phpMyAdmin-3.4.0-all-languages  phpMyAdmin-3.4.0-all-languages.tar.bz2
[root@foundation39 html]# rm -fr *.bz2
[root@foundation39 html]# ls
phpMyAdmin-3.4.0-all-languages
[root@foundation39 html]# mv phpMyAdmin-3.4.0-all-languages/ mysqladmin
[root@foundation39 html]# ls
mysqladmin
[root@foundation39 html]# cd mysqladmin
[root@foundation39 mysqladmin]# cp config.sample.inc.php config.sample.inc.php 
cp: ‘config.sample.inc.php’ and ‘config.sample.inc.php’ are the same file


再在真机的火狐上输入172.25.254.39/mysqladmin
出现数据库页面

4.尝试数据库使用:

[root@foundation39 Desktop]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> create database python;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| python             |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> use python;
Database changed
MariaDB [python]> creat table userInfo(id int,name varchar(10));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'creat table userInfo(id int,name varchar(10))' at line 1
MariaDB [python]> create  table userInfo(id int,name varchar(10));
Query OK, 0 rows affected (0.19 sec)

MariaDB [python]> show tables;
+------------------+
| Tables_in_python |
+------------------+
| userInfo         |
+------------------+
1 row in set (0.00 sec)

MariaDB [python]> select * from userInfo;
Empty set (0.00 sec)

MariaDB [python]> insert into userInfo(id,name)value(1,'tom');
Query OK, 1 row affected (0.03 sec)

MariaDB [python]> select * from userInfo;
+------+------+
| id   | name |
+------+------+
|    1 | tom  |
+------+------+
1 row in set (0.01 sec)

MariaDB [python]> select id from userInfo;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

MariaDB [python]> select name from userInfo;
+------+
| name |
+------+
| tom  |
+------+
1 row in set (0.00 sec)

MariaDB [python]> update userInfo set name = 'harry';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [python]> select name from userInfo;
+-------+
| name  |
+-------+
| harry |
+-------+
1 row in set (0.00 sec)

MariaDB [python]> update userInfo set name = 'tom'where id = 1;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [python]> select name from userInfo;
+------+
| name |
+------+
| tom  |
+------+
1 row in set (0.00 sec)

二.数据库数据管理:

1.数据的查看:
(1)查看数据库里数据的数量:

import MySQLdb
#打开门
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='951124',db='python')

#伸出手
cur = conn.cursor()

#拿东西
recont = cur.execute('select * from userInfo')

#把手伸回来
cur.close()

#把门关上
conn.close()

print recont

这里写图片描述
(2)查看数据库里数据【元祖显示】

import MySQLdb
#打开门
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='951124',db='python')

#伸出手
cur = conn.cursor() #创建了一个手

#拿东西
#这个操作影响了多少行数(有多少行被操作了)
recont = cur.execute('select * from userInfo')
data = cur.fetchall()

#把手伸回来
cur.close()

#把门关上
conn.close()
print recont
print data

这里写图片描述
(3)查看数据库数据【字典显示】

import MySQLdb
#   打开门
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='951124',db='python')

#   伸出手
#   cur = conn.cursor() #创建了一个手
cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)
#   拿东西
#   这个操作影响了多少行数(有多少行被操作了)
recont = cur.execute('select * from userInfo')
data = cur.fetchall()

#   把手伸回来
cur.close()

#   把门关上
conn.close()
print recont
print data

这里写图片描述

2.数据的添加:
(1)单个数据的添加:

import  MySQLdb

#打开门
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='westos',db='python')

#伸出手
cur = conn.cursor()

#操作数据
sql = 'insert into usermg(id,name,address)values(%s,%s,%s)'
params=('1','zgd','china')
recount = cur.execute(sql,params)

#提交申请
conn.commit()

#把手伸回来
cur.close()

#把门关上
conn.close()

print recount

这里写图片描述

(2)单次添加多个数据:

import  MySQLdb

#打开门
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='westos',db='python')

#伸出手
cur = conn.cursor()

#操作数据
li = [
    ('2','hahaha','wwww'),
    ('3','lalala','qqqq'),
]
recount = cur.executemany('insert into usermg(id,name,address) values(%s,%s,%s)',li)

#提交申请
conn.commit()

#把门关上
conn.close()

print recount

这里写图片描述

3.数据的删除:

import  MySQLdb

#打开门
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='westos',db='python')

#伸出手
cur = conn.cursor()

#操作数据
sql = 'delete from usermg where id = %s'
params=(1,)
recount = cur.execute(sql,params)

#提交申请
conn.commit()

#把门关上
conn.close()

print recount

这里写图片描述

4.数据的修改:

import  MySQLdb

#打开门
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='westos',db='python')

#伸出手
cur = conn.cursor()

#操作数据
sql = 'update usermg set name =%s where id = %s'
params=('uu','2',)
recount = cur.execute(sql,params)

#提交申请
conn.commit()

#把门关上
conn.close()

print recount

这里写图片描述

5.提交和回滚 在数据库里交事务操作
在同一张数据表一增一减时若有一个出现错误,则两个都不会执行,会返回原来的结果,这种操作叫做回滚,不会出现数据丢失的情况。
手动添加一张count的数据表,添加id=1 2 money= 100 0
这里写图片描述

import MySQLdb
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='westos',db='python')
cur = conn.cursor()

sql='update count set money = %s where id = 1'
params1 =  ('0',)
recount = cur.execute(sql,params1)
conn.commit()

sql='update count set money = %s where id = 2'
params2 =  ('100',)
recount = cur.execute(sql,params2)
conn.commit()

这里写图片描述

猜你喜欢

转载自blog.csdn.net/China_zgd/article/details/81219013
今日推荐