使用Python向表中添加數據

1.1 問題
向employees表插入數據
向salary表插入數據
插入的數據需要commit到數據庫中
1.2 步驟
實現此案例需要按照如下步驟進行。

步驟一:PyMySQL安裝

  1. 安裝gcc,有些軟件包是C的源碼
[root@localhost ~]# yum install -y gcc
已加載插件:fastestmirror, langpacks
dvd                                                      | 3.6 kB     00:00     
Loading mirror speeds from cached hostfile
匹配 gcc-4.8.5-16.el7.x86_64 的軟件包已經安裝。正在檢查更新。
無須任何處理

2)爲了加速下載,可以使用國內開源鏡像站點

[root@localhost ~]# mkdir ~/.pip
[root@localhost ~]# vim ~/.pip/pip.conf
[global]
index-url = http://pypi.douban.com/simple/
[install]
trusted-host=pypi.douban.com

3)安裝pymysql

[root@localhost ~]# pip3 install pymysql

步驟二:安裝mariadb-server

[root@localhost ~]# yum install –y mariadb-server
....
已安裝:
        mariadb-server.x86_64 1:5.5.56-2.el7
作爲依賴被安裝:
        mariadb.x86_64 1:5.5.56-2.el7
        perl-DBD-MySQL. x86_64 0:4.023-5.el7
完畢!
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# systemctl enable mariadb
[root@localhost ~]# mysqladmin password tedu.cn

步驟三:創建數據庫

1)創建數據庫

[root@localhost ~]# mysql -uroot -ptedu.cn    
MariaDB [(none)]> CREATE DATABASE tedu DEFAULT CHARSET 'utf8';
Query OK, 1 row affected (0.00 sec)

2)創建部門表

部門表字段:部門ID、部門名稱

MariaDB [(none)]> USE tedu;
Database changed
MariaDB [tedu]> CREATE TABLE departments(dep_id INT PRIMARY KEY, dep_name VARCHAR(20));
Query OK, 0 rows affected (0.04 sec)

3)創建員工表

員工表字段:員工編號、姓名、出生日期、部門ID、電話號碼、email、引用外鍵id

MariaDB [tedu]> CREATE TABLE employees (emp_id INT PRIMARY KEY, emp_name VARCHAR(20) NOT NULL, birth_date DATE, phone CHAR(11), email VARCHAR(50), dep_id INT, FOREIGN KEY(dep_id) REFERENCES departments(dep_id));
Query OK, 0 rows affected (0.05 sec)

4)創建工資表

工資表字段:auto_id、員工編號、日期、基本工資、獎金、工資總和

MariaDB [tedu]> CREATE TABLE salary(auto_id INT AUTO_INCREMENT PRIMARY KEY, date DATE, emp_id INT, basic INT, awards INT, FOREIGN KEY(emp_id) REFERENCES employees(emp_id));
Query OK, 0 rows affected (0.05 sec)

步驟四:向departments表插入數據

1)新建insert_data.py文件,編寫代碼如下:

[root@localhost day10]# vim insert_data.py
import pymysql

1)連接數據庫

conn = pymysql.connect(
    host='127.0.0.1',        #連接ip
    port=3306,            #端口號
    user='root',            #數據庫用戶名
    passwd='tedu.cn',        #數據庫密碼
    db='tedu',            #數據庫名
    charset='utf8'        #設置了數據庫的字符集
)

2)創建遊標

cursor = conn.cursor()

3)向部門表departments中插入數據

insert1 = "INSERT INTO departments(dep_id, dep_name) VALUES(%s, %s)"
result = cursor.execute(insert1, (1, '人事部'))        # execute執行insert語句

4)將更新提交到數據庫

conn.commit()    

5)關閉遊標

cursor.close()

6)關閉數據庫連接

conn.close()

2)執行insert_data.py文件:

[root@localhost day10]# python3 insert_data.py

3)登錄mariadb查看結果:

MariaDB [tedu]>> select * from departments;
+--------+-----------+
| dep_id | dep_name  |
+--------+-----------+
|      1  |  人事部    |
+--------+-----------+
1 row in set (0.00 sec) 
  1. 向部門表departments中插入數據還可以用如下方法:
#以上insert_data.py文件第3步可用如下代碼替換:
insert1 = "INSERT INTO departments(dep_id, dep_name) VALUES(%s, %s)"
data = [(2, '運維部'), (3, '開發部')]
cursor.executemany(insert1, data)
mariadb查看結果如下:

MariaDB [tedu]>> select * from departments;
+--------+-----------+
| dep_id | dep_name  |
+--------+-----------+
|      1  |  人事部    |
|      2  |  運維部    |
|      3  |  開發部    |
+--------+-----------+
3 rows in set (0.01 sec)

步驟五:向employees表插入數據

1)新建insert_emp.py文件,編寫代碼如下:

[root@localhost day10]# vim insert_emp.py
import pymysql

1)連接數據庫

conn = pymysql.connect(
    host='127.0.0.1',        #連接ip
    port=3306,            #端口號
    user='root',            #數據庫用戶名
    passwd='tedu.cn',        #數據庫密碼
    db='tedu',            #數據庫名
    charset='utf8'        #設置了數據庫的字符集
)

2)創建遊標

cursor = conn.cursor()

3)向部門表employees中插入數據

insert1 = "INSERT INTO employees(emp_id, emp_name, birth_date,phone, email, dep_id) VALUES(%s, %s, %s, %s, %s, %s)"
result = cursor.execute(insert1, (1, '王君', '2018-9-30',\
 '15678789090', '[email protected]', 3))        # execute執行insert語句

4)將更新提交到數據庫

conn.commit()    

5)關閉遊標

cursor.close()

6)關閉數據庫連接

conn.close()

2)執行insert_emp.py文件:

[root@localhost day10]# python3 insert_emp.py

3)登錄mariadb查看結果:

MariaDB [tedu]>> select * from employees;
+--------+----------+------------+-------------+------------+--------+
| emp_id | emp_name | birth_date |  phone       | email      | dep_id |
+--------+----------+------------+-------------+------------+--------+
|      1  |    王君   | 2018-09-30 | 15678789090 | [email protected] |      3 |
+--------+----------+------------+-------------+------------+--------+
 1 row in set (0.00 sec) 
  1. 向部門表employees中插入數據還可以用如下方法:
#以上insert_emp.py文件第3步可用如下代碼替換:
insert1 = "INSERT INTO employees (dep_id, dep_name) VALUES(%s, %s)"
data = [(2, '運維部'), (3, '開發部')]
cursor.executemany(insert1, data)
mariadb查看結果如下:

MariaDB [tedu]>> select * from departments;
+--------+----------+------------+-------------+------------+--------+
| emp_id | emp_name | birth_date |  phone       | email      | dep_id |
+--------+----------+------------+-------------+------------+--------+
|      1  |   王君    | 2018-09-30 | 15678789090 | [email protected] |      3 |
|      2  |   李雷    | 2018-09-30 | 15678789090 | [email protected] |      2 |
|      3  |   張美    | 2018-09-30 | 15678789090 | [email protected] |      1 |
+--------+----------+------------+-------------+------------+--------+
3 rows in set (0.00 sec)

步驟六:向salary表插入數據

1)新建insert_sal.py文件,編寫代碼如下:

[root@localhost day10]# vim insert_sal.py
import pymysql
1)連接數據庫
conn = pymysql.connect(
    host='127.0.0.1',        #連接ip
    port=3306,            #端口號
    user='root',            #數據庫用戶名
    passwd='tedu.cn',        #數據庫密碼
    db='tedu',            #數據庫名
    charset='utf8'        #設置了數據庫的字符集
)

2)創建遊標

cursor = conn.cursor()

3)向部門表salary中插入數據

insert2 = "INSERT INTO salary(date, emp_id,basic, awards) VALUES(%s, %s, %s, %s)"
data = [('2018-9-30', 2, 1000, 2000), ('2018-9-30', 3, 3000, 6000),('2018-9-30', 1, 8000, 9000)]
cursor.executemany(insert2, data)

4)將更新提交到數據庫

conn.commit()    

5)關閉遊標

cursor.close()

6)關閉數據庫連接

conn.close()

2)執行insert_sal.py文件:

[root@localhost day10]# python3 insert_sal.py

3)登錄mariadb查看結果:

MariaDB [tedu]>> select * from salary;
+---------+------------+--------+-------+--------+
| auto_id | date       | emp_id | basic | awards |
+---------+------------+--------+-------+--------+
|       1 | 2018-09-30 |      2  |  1000 |   2000 |
|       2 | 2018-09-30 |      3  |  3000 |   6000 |
|       3 | 2018-09-30 |      1  |  8000 |   9000 |
+---------+------------+--------+-------+--------+
3 rows in set (0.01 sec) 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章