├── Experiment1 ├── 1.sql └── data │ ├── department.txt │ ├── employee.txt │ ├── location.txt │ ├── name.txt │ ├── project.txt │ ├── test.c │ └── works_on.txt ├── Experiment2 └── lab2.py ├── Experiment3&4 ├── 3&4.sql ├── pic │ ├── ER.png │ └── stupid.gif ├── refer.sql └── window.py ├── Experiment4 ├── exm.cpp ├── exm.h └── main.cpp ├── Experiment5 ├── main.cpp ├── query.cpp ├── query.h └── sql.txt └── README.md /Experiment1/1.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yinizhizhu/Database/022f62454ab7995d98ea645bff653f18d749d4c7/Experiment1/1.sql -------------------------------------------------------------------------------- /Experiment1/data/department.txt: -------------------------------------------------------------------------------- 1 | 老君庙 1 111111111111111100 2016-02-03 2 | 水帘洞 2 111111111111111101 2016-02-04 3 | 火焰山 3 111111111111111102 2016-02-05 4 | 哈工大 4 111111111111111103 2016-02-06 5 | 黑洞 5 111111111111111104 2016-02-07 6 | -------------------------------------------------------------------------------- /Experiment1/data/employee.txt: -------------------------------------------------------------------------------- 1 | 杨朝来 111111111111111100 丽江 1000.00 111111111111111102 1 2 | 蒋平 111111111111111101 等待一 1010.00 111111111111111103 2 3 | 唐灿华 111111111111111102 生生世 1020.00 111111111111111104 3 4 | 马达 111111111111111103 诺言 1030.00 111111111111111105 4 5 | 赵小雪 111111111111111104 凤凰 1040.00 111111111111111106 5 6 | 薛文泉 111111111111111105 如果 1050.00 111111111111111107 1 7 | 丁建伟 111111111111111106 有一天 1060.00 111111111111111108 2 8 | 凡小芬 111111111111111107 请将我 1070.00 111111111111111109 3 9 | 文明 111111111111111108 这里 1080.00 111111111111111100 4 10 | 文彭凤 111111111111111109 和顺 1090.00 111111111111111101 5 11 | 王丽 111111111111111110 马蹄声 1100.00 111111111111111100 1 12 | 王建华 111111111111111111 梦遇归 1110.00 111111111111111101 2 13 | 王梓人 111111111111111112 婺源 1120.00 111111111111111102 3 14 | 王震 111111111111111113 最后 1130.00 111111111111111103 4 15 | 王保真 111111111111111114 最美家 1140.00 111111111111111104 5 16 | 王景亮 111111111111111115 平遥 1150.00 111111111111111105 1 17 | 王丹 111111111111111116 内敛沉 1160.00 111111111111111106 2 18 | 邓志勇 111111111111111117 世家气 1170.00 111111111111111107 3 19 | 邓婕尹 111111111111111118 乌镇 1180.00 111111111111111108 4 20 | 会南 111111111111111119 水乡里 1190.00 111111111111111109 5 21 | 叶汝红 111111111111111120 轻吟浅 1200.00 111111111111111100 1 22 | 付伟娜 111111111111111121 西递 1210.00 111111111111111101 2 23 | 付双红 111111111111111122 桃花 1220.00 111111111111111102 3 24 | 毕泗迁 111111111111111123 渐欲 1230.00 111111111111111103 4 25 | 孙平 111111111111111124 迷人眼 1240.00 111111111111111104 5 26 | 毛华强 111111111111111125 西江 1250.00 111111111111111105 1 27 | 孙益奇 111111111111111126 苗寨 1260.00 111111111111111106 2 28 | 孙媛媛 111111111111111127 荣辱 1270.00 111111111111111107 3 29 | 伍婷阳 111111111111111128 兴衰 1280.00 111111111111111108 4 30 | 娣阳 111111111111111129 话苗疆 1290.00 111111111111111109 5 31 | 倩莹 111111111111111130 丹巴藏 1300.00 111111111111111100 1 32 | 刘小梅 111111111111111131 造物 1310.00 111111111111111101 2 33 | 刘俊鸣 111111111111111132 极致恩 1320.00 111111111111111102 3 34 | 刘海兵 111111111111111133 图瓦村 1330.00 111111111111111103 4 35 | 刘伟华 111111111111111134 一方 1340.00 111111111111111104 5 36 | 刘启龙 111111111111111135 家园 1350.00 111111111111111105 1 37 | 刘勇辉 111111111111111136 净土 1360.00 111111111111111106 2 38 | 吕红朱 111111111111111137 九寨沟 1370.00 111111111111111107 3 39 | 智新 111111111111111138 画壁 1380.00 111111111111111108 4 40 | 张建辉 111111111111111139 奇异世 1390.00 111111111111111109 5 41 | 李湘群 111111111111111140 青海湖 1400.00 111111111111111100 1 42 | 李自新 111111111111111141 九天神 1410.00 111111111111111101 2 43 | 李俊 111111111111111142 情癫之 1420.00 111111111111111102 3 44 | 李娟 111111111111111143 纳木措 1430.00 111111111111111103 4 45 | 李君琳 111111111111111144 尘世污 1440.00 111111111111111104 5 46 | 李桂富 111111111111111145 漓江 1450.00 111111111111111105 1 47 | 陈其虎 111111111111111146 迷失 1460.00 111111111111111106 2 48 | 陈明 111111111111111147 笔墨 1470.00 111111111111111107 3 49 | 邵彩云 111111111111111148 丹青 1480.00 111111111111111108 4 50 | 杨知 111111111111111149 黄山 1490.00 111111111111111109 5 51 | 杨波 111111111111111150 长白山 1500.00 111111111111111100 1 52 | 杨开基 111111111111111151 天池 1510.00 111111111111111101 2 53 | 杨友成 111111111111111152 壶口瀑 1520.00 111111111111111102 3 54 | 杨巧敏 111111111111111153 西湖 1530.00 111111111111111103 4 55 | 杨国栋 111111111111111154 泰山 1540.00 111111111111111104 5 56 | 罗新磊 111111111111111155 张家界 1550.00 111111111111111105 1 57 | 小红 111111111111111156 桃花源 1560.00 111111111111111106 2 58 | -------------------------------------------------------------------------------- /Experiment1/data/location.txt: -------------------------------------------------------------------------------- 1 | 丽江 2 | 等待一 3 | 生生世 4 | 诺言 5 | 凤凰 6 | 如果 7 | 有一天 8 | 请将我 9 | 这里 10 | 和顺 11 | 马蹄声 12 | 梦遇归 13 | 婺源 14 | 最后 15 | 最美家 16 | 平遥 17 | 内敛沉 18 | 世家气 19 | 乌镇 20 | 水乡里 21 | 轻吟浅 22 | 西递 23 | 桃花 24 | 渐欲 25 | 迷人眼 26 | 西江 27 | 苗寨 28 | 荣辱 29 | 兴衰 30 | 话苗疆 31 | 丹巴藏 32 | 造物 33 | 极致恩 34 | 图瓦村 35 | 一方 36 | 家园 37 | 净土 38 | 九寨沟 39 | 画壁 40 | 奇异世 41 | 青海湖 42 | 九天神 43 | 情癫之 44 | 纳木措 45 | 尘世污 46 | 漓江 47 | 迷失 48 | 笔墨 49 | 丹青 50 | 黄山 51 | 长白山 52 | 天池 53 | 壶口瀑 54 | 西湖 55 | 泰山 56 | 张家界 57 | 桃花源 -------------------------------------------------------------------------------- /Experiment1/data/name.txt: -------------------------------------------------------------------------------- 1 | 杨朝来 2 | 蒋平 3 | 唐灿华 4 | 马达 5 | 赵小雪 6 | 薛文泉 7 | 丁建伟 8 | 凡小芬 9 | 文明 10 | 文彭凤 11 | 王丽 12 | 王建华 13 | 王梓人 14 | 王震 15 | 王保真 16 | 王景亮 17 | 王丹 18 | 邓志勇 19 | 邓婕尹 20 | 会南 21 | 叶汝红 22 | 付伟娜 23 | 付双红 24 | 毕泗迁 25 | 孙平 26 | 毛华强 27 | 孙益奇 28 | 孙媛媛 29 | 伍婷阳 30 | 娣阳 31 | 倩莹 32 | 刘小梅 33 | 刘俊鸣 34 | 刘海兵 35 | 刘伟华 36 | 刘启龙 37 | 刘勇辉 38 | 吕红朱 39 | 智新 40 | 张建辉 41 | 李湘群 42 | 李自新 43 | 李俊 44 | 李娟 45 | 李君琳 46 | 李桂富 47 | 陈其虎 48 | 陈明 49 | 邵彩云 50 | 杨知 51 | 杨波 52 | 杨开基 53 | 杨友成 54 | 杨巧敏 55 | 杨国栋 56 | 罗新磊 57 | 小红 58 | -------------------------------------------------------------------------------- /Experiment1/data/project.txt: -------------------------------------------------------------------------------- 1 | 打妖精 P1 大唐 1 2 | 吃饭 P2 丽江 3 3 | 睡觉 P3 和顺 4 4 | 喝水 P4 婺源 1 5 | 开黑 P5 乌镇 2 6 | 敲代码 P6 水乡里 5 7 | 看电影 P7 西递 2 8 | 扫地 P8 西江 1 9 | 挣钱 P9 苗寨 3 10 | 旅游 P10 净土 4 11 | -------------------------------------------------------------------------------- /Experiment1/data/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void getEmployee() 6 | { 7 | int i=0, j; 8 | float salary = 1000; 9 | char location[57][15]; 10 | char nameB[57][15]; 11 | char ssn[19] = "1111111111111111"; 12 | FILE* name = fopen("name.txt", "r"); 13 | FILE* loc = fopen("location.txt", "r"); 14 | FILE* out = fopen("employee.txt", "w"); 15 | while (fscanf(loc, "%s", location[i]) == 1 && fscanf(name, "%s", nameB[i]) == 1) 16 | { 17 | printf("%d: %s-->", strlen(location[i]), location[i]); 18 | printf("%d: %s, name\n", strlen(nameB[i]), nameB[i]); 19 | if (i > 9) 20 | fprintf(out, "%s\t%s%d\t%s\t%0.2f\t%s0%d\t%d\n", nameB[i], ssn, i, location[i], salary, ssn, i%10, i%5+1); 21 | else 22 | fprintf(out, "%s\t%s0%d\t%s\t%0.2f\t%s0%d\t%d\n", nameB[i], ssn, i, location[i], salary, ssn, (i+12)%10, i%5+1); 23 | i++; 24 | salary += 10; 25 | } 26 | fclose(out); 27 | fclose(loc); 28 | fclose(name); 29 | return; 30 | } 31 | 32 | int main() 33 | { 34 | getEmployee(); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Experiment1/data/works_on.txt: -------------------------------------------------------------------------------- 1 | 111111111111111100 P1 7 2 | 111111111111111101 P2 8 3 | 111111111111111102 P3 9 4 | 111111111111111103 P4 10 5 | 111111111111111104 P5 5 6 | 111111111111111105 P6 8 7 | 111111111111111106 P7 11 8 | 111111111111111107 P8 12 9 | 111111111111111108 P9 6 10 | 111111111111111109 P10 9 11 | -------------------------------------------------------------------------------- /Experiment2/lab2.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yinizhizhu/Database/022f62454ab7995d98ea645bff653f18d749d4c7/Experiment2/lab2.py -------------------------------------------------------------------------------- /Experiment3&4/3&4.sql: -------------------------------------------------------------------------------- 1 | insert into works_on 2 | values("111111111", "P1", 12); 3 | 4 | load data local infile "C:/Users/11303/Desktop/LeetCode-Medium/people.txt" into table people; 5 | 6 | create table people 7 | (p_id char(6), 8 | p_name varchar(12), 9 | p_sex varchar(6), 10 | primary key (p_id)); 11 | 12 | load data local infile "C:/Users/11303/Desktop/LeetCode-Medium/people.txt" into table people; 13 | 14 | create table supermarket 15 | (sm_name varchar(12), 16 | sm_ssn char(6), 17 | primary key (sm_ssn)); 18 | 19 | load data local infile "C:/Users/11303/Desktop/LeetCode-Medium/supermarket.txt" into table supermarket; 20 | 21 | create table goods 22 | (g_name varchar(12), 23 | g_ssn char(6), 24 | g_class varchar(12), 25 | g_price int, 26 | primary key (g_ssn)); 27 | 28 | load data local infile "C:/Users/11303/Desktop/LeetCode-Medium/goods.txt" into table goods; 29 | 30 | create table work_ 31 | (w_name varchar(12), 32 | w_ssn char(6), 33 | w_hour int, 34 | w_salary numeric(8,2), 35 | primary key (w_ssn)); 36 | 37 | load data local infile "C:/Users/11303/Desktop/LeetCode-Medium/work_.txt" into table work_; 38 | 39 | create table bank 40 | (b_class varchar(12), 41 | b_ssn char(6), 42 | primary key (b_ssn)); 43 | 44 | load data local infile "C:/Users/11303/Desktop/LeetCode-Medium/bank.txt" into table bank; 45 | 46 | create table restaurant 47 | (r_name varchar(12), 48 | r_ssn char(6), 49 | primary key (r_ssn)); 50 | 51 | load data local infile "C:/Users/11303/Desktop/LeetCode-Medium/restaurant.txt" into table restaurant; 52 | 53 | create table school 54 | (s_name varchar(12), 55 | s_ssn char(6), 56 | s_level varchar(12), 57 | primary key (s_ssn)); 58 | 59 | load data local infile "C:/Users/11303/Desktop/LeetCode-Medium/school.txt" into table school; 60 | 61 | create table hospital 62 | (h_name varchar(12), 63 | h_ssn char(6), 64 | h_level varchar(12), 65 | primary key (h_ssn)); 66 | 67 | load data local infile "C:/Users/11303/Desktop/LeetCode-Medium/hospital.txt" into table hospital; 68 | 69 | -------------------------------------------------------------------------------- /Experiment3&4/pic/ER.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yinizhizhu/Database/022f62454ab7995d98ea645bff653f18d749d4c7/Experiment3&4/pic/ER.png -------------------------------------------------------------------------------- /Experiment3&4/pic/stupid.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yinizhizhu/Database/022f62454ab7995d98ea645bff653f18d749d4c7/Experiment3&4/pic/stupid.gif -------------------------------------------------------------------------------- /Experiment3&4/refer.sql: -------------------------------------------------------------------------------- 1 | 2 | hospital-people 3 | create table h_p_d 4 | (h_ssn char(6), 5 | p_id char(6), 6 | primary key (h_ssn, p_id), 7 | foreign key (h_ssn) references hospital(h_ssn) on delete cascade on update cascade, 8 | foreign key (p_id) references people(p_id) on delete cascade on update cascade); 9 | 10 | insert into h_p_d 11 | values("111111", "111110"); 12 | hospital natural join h_p_d natural join people 13 | 14 | create table h_p_w 15 | (h_ssn char(6), 16 | p_id char(6), 17 | primary key (p_id), 18 | foreign key (h_ssn) references hospital(h_ssn) on delete cascade on update cascade, 19 | foreign key (p_id) references people(p_id) on delete cascade on update cascade); 20 | 21 | insert into h_p_w 22 | values("111111", "111110"); 23 | 24 | create table h_w_o 25 | (h_ssn char(6), 26 | w_ssn char(6), 27 | primary key (h_ssn, w_ssn), 28 | foreign key (h_ssn) references hospital(h_ssn) on delete cascade on update cascade, 29 | foreign key (w_ssn) references work_(w_ssn) on delete cascade on update cascade); 30 | 31 | insert into h_w_o 32 | values("111110", "111110"); 33 | 34 | restaurant-people 35 | create table r_p_d 36 | (r_ssn char(6), 37 | p_id char(6), 38 | primary key (r_ssn, p_id), 39 | foreign key (r_ssn) references restaurant(r_ssn) on delete cascade on update cascade, 40 | foreign key (p_id) references people(p_id) on delete cascade on update cascade); 41 | 42 | r_p_d(r_ssn, p_id) 43 | insert into r_p_d 44 | values("111110", "111112"); 45 | delete from r_p_d; 46 | select * from r_p_d; 47 | 111110, 111112 48 | "111110", "111112" 49 | 50 | create table r_p_w 51 | (r_ssn char(6), 52 | p_id char(6), 53 | primary key (p_id), 54 | foreign key (r_ssn) references restaurant(r_ssn) on delete cascade on update cascade, 55 | foreign key (p_id) references people(p_id) on delete cascade on update cascade); 56 | 57 | create table r_w_o 58 | (r_ssn char(6), 59 | w_ssn char(6), 60 | primary key (r_ssn, w_ssn), 61 | foreign key (r_ssn) references restaurant(r_ssn) on delete cascade on update cascade, 62 | foreign key (w_ssn) references work_(w_ssn) on delete cascade on update cascade); 63 | 64 | restaurant-school 65 | create table r_s_h 66 | (r_ssn char(6), 67 | s_ssn char(6), 68 | primary key (r_ssn), 69 | foreign key (r_ssn) references restaurant(r_ssn) on delete cascade on update cascade, 70 | foreign key (s_ssn) references school(s_ssn) on delete cascade on update cascade); 71 | 72 | school-people 73 | create table s_p_d 74 | (s_ssn char(6), 75 | p_id char(6), 76 | primary key (s_ssn, p_id), 77 | foreign key (s_ssn) references school(s_ssn) on delete cascade on update cascade, 78 | foreign key (p_id) references people(p_id) on delete cascade on update cascade); 79 | 80 | create table s_p_w 81 | (s_ssn char(6), 82 | p_id char(6), 83 | primary key (p_id), 84 | foreign key (s_ssn) references school(s_ssn) on delete cascade on update cascade, 85 | foreign key (p_id) references people(p_id) on delete cascade on update cascade); 86 | 87 | create table s_w_o 88 | (s_ssn char(6), 89 | w_ssn char(6), 90 | primary key (s_ssn, w_ssn), 91 | foreign key (s_ssn) references school(s_ssn) on delete cascade on update cascade, 92 | foreign key (w_ssn) references work_(w_ssn) on delete cascade on update cascade); 93 | 94 | people-work_ 95 | create table p_w_h 96 | (p_id char(6), 97 | w_ssn char(6), 98 | primary key (p_id, w_ssn), 99 | foreign key (p_id) references people(p_id) on delete cascade on update cascade, 100 | foreign key (w_ssn) references work_(w_ssn) on delete cascade on update cascade); 101 | 102 | people-goods 103 | create table p_g_u 104 | (g_ssn char(6), 105 | p_id char(6), 106 | primary key (g_ssn, p_id), 107 | foreign key (g_ssn) references goods(g_ssn) on delete cascade on update cascade, 108 | foreign key (p_id) references people(p_id) on delete cascade on update cascade); 109 | 110 | goods-supermarket 111 | create table g_s_p 112 | (g_ssn char(6), 113 | sm_ssn char(6), 114 | primary key (g_ssn, sm_ssn), 115 | foreign key (g_ssn) references goods(g_ssn) on delete cascade on update cascade, 116 | foreign key (sm_ssn) references supermarket(sm_ssn) on delete cascade on update cascade); 117 | 118 | supermarket-people 119 | create table sm_p_d 120 | (sm_ssn char(6), 121 | p_id char(6), 122 | primary key (sm_ssn, p_id), 123 | foreign key (sm_ssn) references supermarket(sm_ssn) on delete cascade on update cascade, 124 | foreign key (p_id) references people(p_id) on delete cascade on update cascade); 125 | 126 | create table sm_p_w 127 | (sm_ssn char(6), 128 | p_id char(6), 129 | primary key (p_id), 130 | foreign key (sm_ssn) references supermarket(sm_ssn) on delete cascade on update cascade, 131 | foreign key (p_id) references people(p_id) on delete cascade on update cascade); 132 | 133 | create table sm_w_o 134 | (sm_ssn char(6), 135 | w_ssn char(6), 136 | primary key (sm_ssn, w_ssn), 137 | foreign key (sm_ssn) references supermarket(sm_ssn) on delete cascade on update cascade, 138 | foreign key (w_ssn) references work_(w_ssn) on delete cascade on update cascade); 139 | 140 | bank-people 141 | create table b_p_d 142 | (b_ssn char(6), 143 | p_id char(6), 144 | primary key (b_ssn, p_id), 145 | foreign key (b_ssn) references bank(b_ssn) on delete cascade on update cascade, 146 | foreign key (p_id) references people(p_id) on delete cascade on update cascade); 147 | 148 | create table b_p_w 149 | (b_ssn char(6), 150 | p_id char(6), 151 | primary key (p_id), 152 | foreign key (b_ssn) references bank(b_ssn) on delete cascade on update cascade, 153 | foreign key (p_id) references people(p_id) on delete cascade on update cascade); 154 | 155 | create table b_w_o 156 | (b_ssn char(6), 157 | w_ssn char(6), 158 | primary key (b_ssn, w_ssn), 159 | foreign key (b_ssn) references bank(b_ssn) on delete cascade on update cascade, 160 | foreign key (w_ssn) references work_(w_ssn) on delete cascade on update cascade); -------------------------------------------------------------------------------- /Experiment3&4/window.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import sys 3 | from PyQt4.QtGui import * 4 | from PyQt4.QtCore import * 5 | import MySQLdb 6 | 7 | conn=MySQLdb.connect( 8 | host="localhost", 9 | port=3306, 10 | user="root", 11 | passwd="", 12 | db="live", 13 | charset='utf8' 14 | ) 15 | cur=conn.cursor() 16 | 17 | QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8")) 18 | class StockDialog(QDialog): 19 | def __init__(self,parent=None): 20 | super(StockDialog,self).__init__(parent) 21 | 22 | self.setWindowTitle('Flyos') 23 | self.setWindowIcon(QIcon('pic/stupid.gif')) 24 | 25 | mainSplitter=QSplitter(Qt.Horizontal) 26 | mainSplitter.setOpaqueResize(True) 27 | 28 | listWidget=QListWidget(mainSplitter) 29 | listWidget.insertItem(0,self.tr("ER-Diagram")) 30 | listWidget.insertItem(1,self.tr("View")) 31 | listWidget.insertItem(2,self.tr("Insert")) 32 | listWidget.insertItem(3,self.tr("Delete")) 33 | listWidget.insertItem(4,self.tr("Query")) 34 | 35 | frame=QFrame(mainSplitter) 36 | stack=QStackedWidget() 37 | stack.setFrameStyle(QFrame.Panel|QFrame.Raised) 38 | 39 | show_ER=showER() 40 | view = View() 41 | insert = Insert() 42 | delete = Delete() 43 | query = Query() 44 | stack.addWidget(show_ER) 45 | stack.addWidget(view) 46 | stack.addWidget(insert) 47 | stack.addWidget(delete) 48 | stack.addWidget(query) 49 | 50 | closePushButton=QPushButton(self.tr("Exit")) 51 | 52 | buttonLayout=QHBoxLayout() 53 | buttonLayout.addStretch(1) 54 | buttonLayout.addWidget(closePushButton) 55 | 56 | mainLayout=QVBoxLayout(frame) 57 | mainLayout.setMargin(10) 58 | mainLayout.setSpacing(6) 59 | mainLayout.addWidget(stack) 60 | mainLayout.addLayout(buttonLayout) 61 | 62 | self.connect(listWidget,SIGNAL("currentRowChanged(int)"),stack,SLOT("setCurrentIndex(int)")) 63 | self.connect(closePushButton,SIGNAL("clicked()"),self,SLOT("close()")) 64 | 65 | layout=QHBoxLayout(self) 66 | layout.addWidget(mainSplitter) 67 | self.setLayout(layout) 68 | 69 | class showER(QWidget): 70 | def __init__(self, parent=None): 71 | super(showER,self).__init__(parent) 72 | pixmap = QPixmap("pic/ER.png") 73 | self.label = QLabel(self) 74 | self.label.setPixmap(pixmap) 75 | layout=QGridLayout(self) 76 | layout.addWidget(self.label,0,0) 77 | def excute(self): 78 | print "Hou hava pressed the button to excute." 79 | 80 | class View(QWidget): 81 | def __init__(self,parent=None): 82 | super(View,self).__init__(parent) 83 | 84 | label1=QLabel(self.tr("视图名:")) 85 | label2=QLabel(self.tr("要查询属性:")) 86 | label3=QLabel(self.tr("限制:")) 87 | button=QPushButton(self.tr("Excute")) 88 | label4=QLabel(self.tr("执行结果:")) 89 | 90 | self.viewEdit=QLineEdit() 91 | self.attrEdit=QLineEdit() 92 | self.limitEdit=QLineEdit() 93 | self.resultEdit=QTextEdit() 94 | 95 | layout=QGridLayout(self) 96 | layout.addWidget(label1,0,0) 97 | layout.addWidget(self.viewEdit,0,1) 98 | layout.addWidget(label2,1,0) 99 | layout.addWidget(self.attrEdit,1,1) 100 | layout.addWidget(label3,2,0) 101 | layout.addWidget(self.limitEdit,2,1) 102 | layout.addWidget(button,3,0) 103 | layout.addWidget(label4,4,0) 104 | layout.addWidget(self.resultEdit,4,1) 105 | self.connect(button,SIGNAL("clicked()"), self.excute) 106 | def excute(self): 107 | view = self.viewEdit.text() 108 | attr = self.attrEdit.text() 109 | query = "select "+attr+" from "+view 110 | limit = self.limitEdit.text() 111 | if len(str(limit)) > 0: 112 | query +=" where "+limit 113 | print query 114 | # query = "select * from people" 115 | a2=cur.execute(str(query).decode("gbk").encode("utf8")) 116 | info = cur.fetchmany(a2) 117 | result = "" 118 | for i in info: 119 | for j in i: 120 | result += " " 121 | result += j 122 | result += '\n' 123 | if len(result) > 0: 124 | self.resultEdit.setText(result) 125 | else: 126 | self.resultEdit.setText("Cannot excute\""+str(query)+"\"") 127 | 128 | class Insert(QWidget): 129 | def __init__(self,parent=None): 130 | super(Insert,self).__init__(parent) 131 | 132 | label1=QLabel(self.tr("表名:")) 133 | label2=QLabel(self.tr("值:")) 134 | button=QPushButton(self.tr("Excute")) 135 | label3=QLabel(self.tr("执行结果:")) 136 | 137 | self.tableEdit=QLineEdit() 138 | self.attrEdit=QLineEdit() 139 | self.resultEdit=QTextEdit() 140 | 141 | layout=QGridLayout(self) 142 | layout.addWidget(label1,0,0) 143 | layout.addWidget(self.tableEdit,0,1) 144 | layout.addWidget(label2,1,0) 145 | layout.addWidget(self.attrEdit,1,1) 146 | layout.addWidget(button, 2, 0) 147 | layout.addWidget(label3,3,0) 148 | layout.addWidget(self.resultEdit,3,1) 149 | self.connect(button,SIGNAL("clicked()"), self.excute) 150 | def excute(self): 151 | table = self.tableEdit.text() 152 | value = self.attrEdit.text() 153 | query = "insert into "+str(table)+" values("+str(value)+")" 154 | print query 155 | # query = "select * from people" 156 | a2=cur.execute(query.decode("gbk").encode("utf8")) 157 | info = cur.fetchmany(a2) 158 | result = "" 159 | for i in info: 160 | for j in i: 161 | result += " " 162 | result += j 163 | result += '\n' 164 | self.resultEdit.setText("Successed to insert the values into "+table) 165 | 166 | class Delete(QWidget): 167 | def __init__(self,parent=None): 168 | super(Delete,self).__init__(parent) 169 | 170 | label1=QLabel(self.tr("表名:")) 171 | label2=QLabel(self.tr("选择删除属性值:")) 172 | label3=QLabel(self.tr("限制:")) 173 | button=QPushButton(self.tr("Excute")) 174 | label4=QLabel(self.tr("执行结果:")) 175 | 176 | self.tableEdit=QLineEdit() 177 | self.attrEdit=QLineEdit() 178 | self.limitEdit=QLineEdit() 179 | self.resultEdit=QTextEdit() 180 | 181 | layout=QGridLayout(self) 182 | layout.addWidget(label1,0,0) 183 | layout.addWidget(self.tableEdit,0,1) 184 | layout.addWidget(label2,1,0) 185 | layout.addWidget(self.attrEdit,1,1) 186 | layout.addWidget(label3,2,0) 187 | layout.addWidget(self.limitEdit,2,1) 188 | layout.addWidget(button, 3, 0) 189 | layout.addWidget(label4,4,0) 190 | layout.addWidget(self.resultEdit,4,1) 191 | self.connect(button,SIGNAL("clicked()"), self.excute) 192 | def excute(self): 193 | table = self.tableEdit.text() 194 | attr = self.attrEdit.text() 195 | query = "delete "+attr+" from "+table 196 | limit = self.limitEdit.text() 197 | if len(limit) > 0: 198 | query +=" where "+limit 199 | print query 200 | # query = "select * from people" 201 | a2=cur.execute(str(query).decode("gbk").encode("utf8")) 202 | info = cur.fetchmany(a2) 203 | result = "" 204 | for i in info: 205 | for j in i: 206 | result += " " 207 | result += j 208 | result += '\n' 209 | self.resultEdit.setText("Successed to delete the values on "+table) 210 | 211 | class Query(QWidget): 212 | def __init__(self, parent=None): 213 | super(Query,self).__init__(parent) 214 | 215 | label1=QLabel(self.tr("表名:")) 216 | label2=QLabel(self.tr("要查询属性值:")) 217 | label3=QLabel(self.tr("限制:")) 218 | label4=QLabel(self.tr("分组:")) 219 | button=QPushButton(self.tr("Excute")) 220 | label5=QLabel(self.tr("执行结果:")) 221 | 222 | self.tableEdit=QLineEdit() 223 | self.attrEdit=QLineEdit() 224 | self.limitEdit=QLineEdit() 225 | self.groupEdit=QLineEdit() 226 | self.resultEdit=QTextEdit() 227 | 228 | layout=QGridLayout(self) 229 | layout.addWidget(label1,0,0) 230 | layout.addWidget(self.tableEdit,0,1) 231 | layout.addWidget(label2,1,0) 232 | layout.addWidget(self.attrEdit,1,1) 233 | layout.addWidget(label3,2,0) 234 | layout.addWidget(self.limitEdit,2,1) 235 | layout.addWidget(label4, 3, 0) 236 | layout.addWidget(self.groupEdit, 3, 1) 237 | layout.addWidget(button, 4, 0) 238 | layout.addWidget(label5,5,0) 239 | layout.addWidget(self.resultEdit,5,1) 240 | self.connect(button,SIGNAL("clicked()"), self.excute) 241 | def excute(self): 242 | table = self.tableEdit.text() 243 | attr = self.attrEdit.text() 244 | query = "select "+attr+" from "+table 245 | limit = self.limitEdit.text() 246 | if len(limit) > 0: 247 | query +=" where "+limit 248 | group = self.groupEdit.text() 249 | if len(group) > 0: 250 | query += " group by "+group 251 | print query 252 | # query = "select * from people" 253 | a2=cur.execute(str(query).decode("gbk").encode("utf8")) 254 | info = cur.fetchmany(a2) 255 | result = "" 256 | for i in info: 257 | for j in i: 258 | result += " " 259 | result += j 260 | result += '\n' 261 | if len(result) > 0: 262 | self.resultEdit.setText(result) 263 | else: 264 | self.resultEdit.setText("Cannot excute\""+str(query)+"\""+". Or no values return.") 265 | 266 | app=QApplication(sys.argv) 267 | main=StockDialog() 268 | main.show() 269 | app.exec_() 270 | conn.commit() 271 | cur.close() 272 | conn.close() -------------------------------------------------------------------------------- /Experiment4/exm.cpp: -------------------------------------------------------------------------------- 1 | #include "exm.h" 2 | 3 | char dataR[LEN] = "R/data.txt"; 4 | char dataS[LEN] = "S/data.txt"; 5 | 6 | char selectR[LEN] = "R/selectA.txt"; 7 | char selectS[LEN] = "S/selectC.txt"; 8 | 9 | char projectR[LEN] = "R/projectA.txt"; 10 | char projectS[LEN] = "S/projectC.txt"; 11 | 12 | //for R:112, S:224 13 | Buffer::Buffer(int addr, int num, int t) 14 | { 15 | int tmpAddr = addr, counter = num; 16 | int i, divider, basis; 17 | 18 | FILE *fp = NULL; 19 | if (t == 0) 20 | { 21 | fp = fopen(dataR, "w"); 22 | basis = 1; 23 | divider = 40; 24 | } 25 | else 26 | { 27 | fp = fopen(dataS, "w"); 28 | basis = 20; 29 | divider = 41; 30 | } 31 | if (fp == NULL) 32 | { 33 | printf("Cannot create the file: data.txt\n"); 34 | return; 35 | } 36 | tag = t; 37 | numIO = 0; 38 | srand((unsigned int)time(NULL));//the seed for random 39 | while (counter > 0) 40 | { 41 | for (i = 0; i < 7; i++) 42 | { 43 | container[i].first = rand() % divider + basis; 44 | container[i].second = rand() % 1000 + 1; 45 | fprintf(fp, "%d %d\n", container[i].first, container[i].second); 46 | } 47 | counter -= 7; 48 | container[i].first = 0; 49 | if (counter > 0) 50 | container[i].second = tmpAddr + 1; 51 | else 52 | container[i].second = 0; 53 | writeBlk(tmpAddr); 54 | tmpAddr++; 55 | } 56 | headAddr = addr; 57 | numBlk = tmpAddr - addr; 58 | fclose(fp); 59 | return; 60 | } 61 | 62 | Buffer::~Buffer() 63 | { 64 | return; 65 | } 66 | 67 | bool Buffer::readBlk(int addr) 68 | { 69 | char filename[LEN]; 70 | if (tag == 0) 71 | sprintf(filename, "R/%d.txt", addr); 72 | else 73 | sprintf(filename, "S/%d.txt", addr); 74 | FILE *fp = fopen(filename, "r"); 75 | if (fp == NULL) 76 | { 77 | printf("Cannot open the file: %s\n", filename); 78 | return false; 79 | } 80 | for (int i = 0; i < BLK; i++) 81 | fscanf(fp, "%d %d", &(container[i].first), &(container[i].second)); 82 | fclose(fp); 83 | numIO++; 84 | return true; 85 | } 86 | 87 | bool Buffer::writeBlk(int addr) 88 | { 89 | char filename[LEN]; 90 | if (tag == 0) 91 | sprintf(filename, "R/%d.txt", addr); 92 | else 93 | sprintf(filename, "S/%d.txt", addr); 94 | FILE *fp = fopen(filename, "w"); 95 | if (fp == NULL) 96 | { 97 | printf("Cannot create the file: %s\n", filename); 98 | return false; 99 | } 100 | for (int i = 0; i < BLK; i++) 101 | fprintf(fp, "%d %d\n", container[i].first, container[i].second); 102 | fclose(fp); 103 | numIO++; 104 | return true; 105 | } 106 | 107 | void Buffer::selectFirst(int f) 108 | { 109 | int next = getHead(); 110 | 111 | FILE *fp = NULL; 112 | if (tag == 0) 113 | { 114 | fp = fopen(selectR, "w"); 115 | //printf("select B from R where A = %d\n", f); 116 | //printf(" B\n"); 117 | } 118 | else 119 | { 120 | fp = fopen(selectS, "w"); 121 | //printf("select D from S where C = %d\n", f); 122 | //printf(" D\n"); 123 | } 124 | if (fp == NULL) 125 | { 126 | printf("Cannot create the file: select.txt\n"); 127 | return; 128 | } 129 | while (next > 0) 130 | { 131 | if (readBlk(next)) 132 | { 133 | for (int i = 0; i < BLK; i++) 134 | if (f == container[i].first) 135 | { 136 | //printf(" %d\n", container[i].second); 137 | fprintf(fp, " %d\n", container[i].second); 138 | } 139 | } 140 | else 141 | return; 142 | next = getNext(); 143 | } 144 | //printf("\n"); 145 | fclose(fp); 146 | return; 147 | } 148 | 149 | void Buffer::projectFirst() 150 | { 151 | int next = getHead(); 152 | 153 | FILE *fp = NULL; 154 | if (tag == 0) 155 | { 156 | fp = fopen(projectR, "w"); 157 | //printf("select B from R\n"); 158 | //printf(" B\n"); 159 | fprintf(fp, "select A from R\n\n A\n"); 160 | } 161 | else 162 | { 163 | fp = fopen(projectS, "w"); 164 | //printf("select D from S\n"); 165 | //printf(" D\n"); 166 | fprintf(fp, "select C from S\n\n C\n"); 167 | } 168 | if (fp == NULL) 169 | { 170 | printf("Cannot create the file: project.txt\n"); 171 | return; 172 | } 173 | while (next > 0) 174 | { 175 | if (readBlk(next)) 176 | { 177 | for (int i = 0; i < BLK; i++) 178 | { 179 | //printf(" %d\n", container[i].first); 180 | fprintf(fp, " %d\n", container[i].first); 181 | } 182 | } 183 | else 184 | return; 185 | next = getNext(); 186 | } 187 | //printf("\n"); 188 | fclose(fp); 189 | return; 190 | } 191 | 192 | bool Buffer::getAllPair(pair* contain) 193 | { 194 | int counter = 0; 195 | int next = getHead(); 196 | while (next > 0) 197 | { 198 | if (readBlk(next)) 199 | { 200 | for (int i = 0; i < (BLK - 1); i++) 201 | { 202 | contain[counter].first = container[i].first; 203 | contain[counter].second = container[i].second; 204 | counter++; 205 | } 206 | } 207 | else 208 | return false; 209 | next = getNext(); 210 | } 211 | return true; 212 | } 213 | 214 | int Buffer::getHead() 215 | { 216 | return headAddr; 217 | } 218 | 219 | int Buffer::getNext() 220 | { 221 | return container[BLK-1].second; 222 | } 223 | 224 | int Buffer::getNumBlk() 225 | { 226 | return numBlk; 227 | } 228 | 229 | int Buffer::getNumIO() 230 | { 231 | return numIO; 232 | } 233 | 234 | pair *Buffer::getContainer() 235 | { 236 | return container; 237 | } -------------------------------------------------------------------------------- /Experiment4/exm.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #if !defined EXM_H 3 | #define EXM_H 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define BLK 8 11 | #define LEN 39 12 | 13 | typedef struct pair 14 | { 15 | int first; 16 | int second; 17 | } pair; 18 | 19 | class Buffer 20 | { 21 | public: 22 | Buffer(int head, int num, int tag); 23 | ~Buffer(); 24 | bool readBlk(int addr); 25 | bool writeBlk(int addr); 26 | void selectFirst(int f); 27 | void projectFirst(); 28 | bool getAllPair(pair* contain); 29 | int getHead(); 30 | int getNext(); 31 | int getNumBlk(); 32 | int getNumIO(); 33 | pair *getContainer(); 34 | private: 35 | int headAddr; 36 | int numBlk; 37 | int numIO; 38 | int tag; 39 | pair container[BLK]; 40 | }; 41 | 42 | #endif -------------------------------------------------------------------------------- /Experiment4/main.cpp: -------------------------------------------------------------------------------- 1 | #include "exm.h" 2 | 3 | #define LENR 112 4 | #define LENS 224 5 | #define TAGR 0 6 | #define TAGS 1 7 | 8 | typedef struct next 9 | { 10 | int total; 11 | int index[40]; 12 | next() 13 | { 14 | total = 0; 15 | } 16 | } next; 17 | 18 | char joinA_CN[LEN] = "joinA&CN.txt"; 19 | char joinA_CM[LEN] = "joinA&CM.txt"; 20 | char joinA_CH[LEN] = "joinA&CH.txt"; 21 | char unionA_C[LEN] = "unionA&C.txt"; 22 | 23 | void nestLoopJoin(Buffer& R, Buffer& S); 24 | void mergeJoin(Buffer& R, Buffer& S); 25 | void hashJoin(Buffer& R, Buffer& S); 26 | void getHash(pair* container, next* hash, int len); 27 | void sortUnion(Buffer& R, Buffer& S); 28 | 29 | int cmp(const void *a, const void *b) 30 | { 31 | return ((*(pair*)a).first > (*(pair*)b).first ? 1 : -1); 32 | } 33 | 34 | int countSame(pair *contain, int index, int end) 35 | { 36 | int counter = 1; 37 | for (int i = index + 1; i < end; i++) 38 | { 39 | if (contain[i].first != contain[index].first) 40 | return counter; 41 | counter++; 42 | } 43 | return counter++; 44 | } 45 | 46 | int main() 47 | { 48 | Buffer R = Buffer(1, LENR, TAGR);//0 for R, 1 for S 49 | R.selectFirst(30); 50 | R.projectFirst(); 51 | 52 | Buffer S = Buffer(1, LENS, TAGS); 53 | S.selectFirst(30); 54 | S.projectFirst(); 55 | 56 | nestLoopJoin(R, S); 57 | mergeJoin(R, S); 58 | hashJoin(R, S); 59 | sortUnion(R, S); 60 | return 0; 61 | } 62 | 63 | void nestLoopJoin(Buffer& R, Buffer& S) 64 | { 65 | FILE *fp = fopen(joinA_CN, "w"); 66 | if (fp == NULL) 67 | { 68 | printf("Cannot create the file: %s\n", joinA_CN); 69 | return; 70 | } 71 | fprintf(fp, "select * from R, S where R.A = S.C\n"); 72 | fprintf(fp, " A B C D\n"); 73 | //pair* containerR = new pair[LENR]; 74 | //pair* containerS = new pair[LENS]; 75 | //if (R.getAllPair(containerR) && S.getAllPair(containerS)) 76 | //{ 77 | // for (int i = 0; i < LENR; i++) 78 | // for (int j = 0; j < LENS; j++) 79 | // { 80 | // if (containerR[i].first == containerS[j].first) 81 | // fprintf(fp, " %d %d %d %d\n", containerR[i].first, containerR[i].second, 82 | // containerS[j].first, containerS[j].second); 83 | // } 84 | //} 85 | //delete[]containerR; 86 | //delete[]containerS; 87 | int counterR = 0; 88 | int nextR = R.getHead(); 89 | while (nextR > 0) 90 | { 91 | if (R.readBlk(nextR)) 92 | { 93 | int counterS = 0; 94 | int nextS = S.getHead(); 95 | while (nextS > 0) 96 | { 97 | if (S.readBlk(nextS)) 98 | { 99 | pair *tmpR = R.getContainer(); 100 | pair *tmpS = S.getContainer(); 101 | for (int i = 0; i < (BLK - 1); i++) 102 | for (int j = 0; j < (BLK - 1); j++) 103 | { 104 | if (tmpR[i].first == tmpS[j].first) 105 | fprintf(fp, " %d %d %d %d\n", tmpR[i].first, tmpR[i].second, 106 | tmpS[j].first, tmpS[j].second); 107 | } 108 | } 109 | nextS = S.getNext(); 110 | } 111 | } 112 | nextR = R.getNext(); 113 | } 114 | fclose(fp); 115 | return; 116 | } 117 | 118 | void mergeJoin(Buffer& R, Buffer& S) 119 | { 120 | FILE *fp = fopen(joinA_CM, "w"); 121 | if (fp == NULL) 122 | { 123 | printf("Cannot create the file: %s\n", joinA_CM); 124 | return; 125 | } 126 | fprintf(fp, "select * from R, S where R.A = S.C\n"); 127 | fprintf(fp, " A B C D\n"); 128 | //pair* containerR = new pair[LENR]; 129 | //pair* containerS = new pair[LENS]; 130 | //if (R.getAllPair(containerR) && S.getAllPair(containerS)) 131 | //{ 132 | // qsort(containerR, LENR, sizeof(containerR[0]), cmp); 133 | // //for (int i = 0; i < LENR; i++) 134 | // // printf("%d\n", containerR[i].first); 135 | // qsort(containerS, LENS, sizeof(containerS[0]), cmp); 136 | // int i = 0, j = 0; 137 | // int lenI, lenJ; 138 | // while (i < LENR && j < LENS) 139 | // { 140 | // if (containerR[i].first == containerS[j].first) 141 | // { 142 | // lenI = countSame(containerR, i, LENR); 143 | // lenJ = countSame(containerS, j, LENS); 144 | // for (int r = 0; r < lenI; r++) 145 | // for (int s = 0; s < lenJ; s++) 146 | // fprintf(fp, " %d %d %d %d\n", containerR[r+i].first, containerR[r+i].second, 147 | // containerS[s+j].first, containerS[s+j].second); 148 | // i += lenI; 149 | // j += lenJ; 150 | // } 151 | // else if (containerR[i].first < containerS[j].first) 152 | // i++; 153 | // else 154 | // j++; 155 | // } 156 | //} 157 | //delete[]containerR; 158 | //delete[]containerS; int counterR = 0; 159 | int nextR = R.getHead(); 160 | while (nextR > 0) 161 | { 162 | if (R.readBlk(nextR)) 163 | { 164 | int counterS = 0; 165 | int nextS = S.getHead(); 166 | while (nextS > 0) 167 | { 168 | if (S.readBlk(nextS)) 169 | { 170 | int len = BLK - 1; 171 | pair *tmpR = R.getContainer(); 172 | pair *tmpS = S.getContainer(); 173 | qsort(tmpR, len, sizeof(tmpR[0]), cmp); 174 | qsort(tmpS, len, sizeof(tmpS[0]), cmp); 175 | int i = 0, j = 0; 176 | int lenI, lenJ; 177 | while (i < len && j < len) 178 | { 179 | if (tmpR[i].first == tmpS[j].first) 180 | { 181 | lenI = countSame(tmpR, i, len); 182 | lenJ = countSame(tmpS, j, len); 183 | for (int r = 0; r < lenI; r++) 184 | for (int s = 0; s < lenJ; s++) 185 | fprintf(fp, " %d %d %d %d\n", tmpR[r + i].first, tmpR[r + i].second, 186 | tmpS[s + j].first, tmpS[s + j].second); 187 | i += lenI; 188 | j += lenJ; 189 | } 190 | else if (tmpR[i].first < tmpS[j].first) 191 | i++; 192 | else 193 | j++; 194 | } 195 | } 196 | nextS = S.getNext(); 197 | } 198 | } 199 | nextR = R.getNext(); 200 | } 201 | fclose(fp); 202 | return; 203 | } 204 | 205 | void hashJoin(Buffer& R, Buffer& S) 206 | { 207 | FILE *fp = fopen(joinA_CH, "w"); 208 | if (fp == NULL) 209 | { 210 | printf("Cannot create the file: %s\n", joinA_CH); 211 | return; 212 | } 213 | fprintf(fp, "select * from R, S where R.A = S.C\n"); 214 | fprintf(fp, " A B C D\n"); 215 | 216 | next* hashR = new next[61]; 217 | next* hashS = new next[61]; 218 | pair* containerR = new pair[LENR]; 219 | pair* containerS = new pair[LENS]; 220 | if (containerR && containerS && hashR && hashS) 221 | { 222 | if (R.getAllPair(containerR) && S.getAllPair(containerS)) 223 | { 224 | getHash(containerR, hashR, LENR); 225 | getHash(containerS, hashS, LENS); 226 | 227 | int s, r, indexR, indexS; 228 | for (int i = 0; i < 61; i++) 229 | { 230 | if (hashR[i].total > 0 && hashS[i].total > 0) 231 | { 232 | for (r = 0; r < hashR[i].total; r++) 233 | { 234 | indexR = hashR[i].index[r]; 235 | for (s = 0; s < hashS[i].total; s++) 236 | { 237 | indexS = hashS[i].index[s]; 238 | fprintf(fp, " %d %d %d %d\n", containerR[indexR].first, containerR[indexR].second, 239 | containerS[indexS].first, containerS[indexS].second); 240 | } 241 | } 242 | } 243 | } 244 | } 245 | } 246 | else 247 | printf("The room is not enough for the buffer!\n"); 248 | delete[]containerR; 249 | delete[]containerS; 250 | delete[]hashR; 251 | delete[]hashS; 252 | fclose(fp); 253 | return; 254 | } 255 | 256 | void getHash(pair* container, next* hash, int len) 257 | { 258 | int index, total; 259 | qsort(container, len, sizeof(container[0]), cmp); 260 | for (int i = 0; i < len; i++) 261 | { 262 | index = container[i].first; 263 | total = hash[index].total; 264 | hash[index].index[total] = i; 265 | hash[index].total++; 266 | } 267 | return; 268 | } 269 | 270 | void sortUnion(Buffer& R, Buffer& S) 271 | { 272 | FILE *fp = fopen(unionA_C, "w"); 273 | if (fp == NULL) 274 | { 275 | printf("Cannot create the file: %s\n", unionA_C); 276 | return; 277 | } 278 | fprintf(fp, "select A from R union select C from S\n"); 279 | fprintf(fp, " A\n"); 280 | 281 | next* hashR = new next[61]; 282 | next* hashS = new next[61]; 283 | pair* containerR = new pair[LENR]; 284 | pair* containerS = new pair[LENS]; 285 | if (containerR && containerS && hashR && hashS) 286 | { 287 | if (R.getAllPair(containerR) && S.getAllPair(containerS)) 288 | { 289 | getHash(containerR, hashR, LENR); 290 | getHash(containerS, hashS, LENS); 291 | for (int i = 1; i < 61; i++) 292 | { 293 | if (hashR[i].total > 0 || hashS[i].total > 0) 294 | fprintf(fp, " %d\n", i); 295 | } 296 | } 297 | } 298 | else 299 | printf("The room is not enough for the buffer!\n"); 300 | delete[]containerR; 301 | delete[]containerS; 302 | delete[]hashR; 303 | delete[]hashS; 304 | fclose(fp); 305 | fclose(fp); 306 | } -------------------------------------------------------------------------------- /Experiment5/main.cpp: -------------------------------------------------------------------------------- 1 | #include "query.h" 2 | //#define TEST 3 | 4 | int main() 5 | { 6 | database a; 7 | #ifdef TEST 8 | a.showDepartment(); 9 | a.showEmployee(); 10 | a.showProject(); 11 | a.showWorks_on(); 12 | #endif 13 | 14 | a.readInput(); 15 | 16 | #ifdef TEST 17 | a.showInput(); 18 | #endif 19 | 20 | a.lexSql(); 21 | a.showSql(); 22 | a.parseSql(); 23 | a.outputOld(); 24 | a.optimizeTree(); 25 | a.outputOld(); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Experiment5/query.cpp: -------------------------------------------------------------------------------- 1 | #include "query.h" 2 | 3 | database::database() 4 | { 5 | department.name = "department"; 6 | department.pro.push_back("dname"); 7 | department.pro.push_back("dno"); 8 | department.pro.push_back("mgrssn"); 9 | department.pro.push_back("mgrstartdate"); 10 | 11 | employee.name = "employee"; 12 | employee.pro.push_back("ename"); 13 | employee.pro.push_back("essn"); 14 | employee.pro.push_back("address"); 15 | employee.pro.push_back("salary"); 16 | employee.pro.push_back("superssn"); 17 | employee.pro.push_back("dno"); 18 | 19 | project.name = "project"; 20 | project.pro.push_back("pname"); 21 | project.pro.push_back("pno"); 22 | project.pro.push_back("plocation"); 23 | project.pro.push_back("dno"); 24 | 25 | works_on.name = "works_on"; 26 | works_on.pro.push_back("essn"); 27 | works_on.pro.push_back("pno"); 28 | works_on.pro.push_back("hours"); 29 | 30 | treeHead = NULL; 31 | } 32 | 33 | database::~database() 34 | { 35 | department.pro.clear(); 36 | employee.pro.clear(); 37 | project.pro.clear(); 38 | works_on.pro.clear(); 39 | sql.clear(); 40 | } 41 | 42 | void database::readInput() 43 | { 44 | cout << "Please input the sql:"; 45 | getline(cin, input); 46 | cout << input << endl; 47 | return; 48 | } 49 | 50 | void database::lexSql() 51 | { 52 | string move = ""; 53 | for (unsigned int i = 0; i < input.size(); i++) 54 | { 55 | if (input[i] == ' ') 56 | { 57 | if (move.size()) 58 | { 59 | sql.push_back(move); 60 | move = ""; 61 | } 62 | } 63 | else 64 | move += input[i]; 65 | } 66 | if (move.size()) 67 | sql.push_back(move); 68 | return; 69 | } 70 | 71 | //we get the analysis tree to get the result of experiment 72 | void database::parseSql() //seperate the sql with the key words and the separators 73 | { 74 | unsigned int i = 0; 75 | for (; i < sql.size();) 76 | { 77 | if (sql[i] == "select") 78 | { 79 | cout << "select you are" << endl; 80 | treeNode *newOne = new treeNode; 81 | newOne->state = 2; 82 | i += 2; 83 | while (sql[i] != "]") 84 | { 85 | newOne->op2.cond.push_back(condition(sql[i], sql[i + 1], sql[i + 2])); 86 | if (sql[i + 3] == "&") 87 | i += 4; 88 | else 89 | i += 3; 90 | } 91 | i += 1; 92 | newOne->next = NULL; 93 | insertNode(newOne); 94 | } 95 | else if (sql[i] == "projection") 96 | { 97 | cout << "projection you are" << endl; 98 | treeNode *newOne = new treeNode; 99 | newOne->state = 3; 100 | i += 2; 101 | while (sql[i] != "]") 102 | { 103 | newOne->op3.pro.push_back(sql[i]); 104 | i += 1; 105 | } 106 | i += 1; 107 | newOne->next = NULL; 108 | insertNode(newOne); 109 | } 110 | else if (sql[i] == "(" || sql[i] == ")") //other: employee join department 111 | i+=1; 112 | else 113 | { 114 | cout << "join you are" << endl; 115 | treeNode *newOne = new treeNode; 116 | newOne->state = 1; 117 | newOne->op1.name1 = sql[i]; 118 | newOne->op1.name2 = sql[i + 2]; 119 | newOne->next = NULL; 120 | insertNode(newOne); 121 | i += 3; 122 | } 123 | } 124 | return; 125 | } 126 | 127 | void database::optimizeTree() 128 | { 129 | treeNode *head = getHead(); 130 | while (head) 131 | { 132 | if (head->state == 2) 133 | { 134 | treeNode *lastOne = getLastNode(); 135 | int j = 0; 136 | lastOne->op1.name1 += "("; 137 | lastOne->op1.name1 += head->op2.cond[j].pro; 138 | lastOne->op1.name1 += head->op2.cond[j].op; 139 | lastOne->op1.name1 += head->op2.cond[j].value; 140 | lastOne->op1.name1 += ")"; 141 | j = 1; 142 | if (head->op2.cond.size() == 2) 143 | { 144 | lastOne->op1.name2 += "("; 145 | lastOne->op1.name2 += head->op2.cond[j].pro; 146 | lastOne->op1.name2 += head->op2.cond[j].op; 147 | lastOne->op1.name2 += head->op2.cond[j].value; 148 | lastOne->op1.name2 += ")"; 149 | } 150 | if (head == getHead()) 151 | { 152 | treeHead = treeHead->next; 153 | } 154 | else 155 | { 156 | treeHead->next = head->next; 157 | } 158 | break; 159 | } 160 | head = head->next; 161 | } 162 | return; 163 | } 164 | 165 | void database::insertNode(treeNode *node) 166 | { 167 | cout << "insertNode you are" << endl; 168 | treeNode *lastNode = getLastNode(); 169 | if (lastNode == NULL) 170 | { 171 | treeHead = node; 172 | return; 173 | } 174 | lastNode->next = node; 175 | return; 176 | } 177 | 178 | treeNode *database::getHead() 179 | { 180 | return treeHead; 181 | } 182 | 183 | treeNode *database::getLastNode() 184 | { 185 | if (treeHead == NULL) 186 | return NULL; 187 | treeNode *lastOne = treeHead; 188 | while (lastOne->next) 189 | lastOne = lastOne->next; 190 | return lastOne; 191 | } 192 | 193 | void database::clearTree(treeNode *node) 194 | { 195 | if (node == NULL) 196 | return; 197 | treeNode *move = treeHead; 198 | while (move) 199 | { 200 | treeHead = treeHead->next; 201 | delete[]move; 202 | move = treeHead; 203 | } 204 | return; 205 | } 206 | 207 | void database::clearSql() 208 | { 209 | sql.clear(); 210 | return; 211 | } 212 | 213 | void database::outputOld() 214 | { 215 | cout << endl << endl; 216 | treeNode *move = getHead(); 217 | while (move) 218 | { 219 | //cout << move->state << endl; 220 | if (move->state == 1) 221 | { 222 | cout << "x" << endl; 223 | cout << move->op1.name1 << " " << move->op1.name2 << endl; 224 | } 225 | else if (move->state == 2) 226 | { 227 | cout << "theta " << move->op2.cond[0].pro << " " << move->op2.cond[0].op << " " << move->op2.cond[0].value << " "; 228 | for (unsigned int i = 1; i < move->op2.cond.size(); i++) 229 | cout << "& " << move->op2.cond[i].pro << " " << move->op2.cond[i].op << " " << move->op2.cond[i].value << " "; 230 | cout << endl; 231 | } 232 | else //state = 3 233 | { 234 | cout << "projection "; 235 | for (unsigned int i = 0; i < move->op3.pro.size(); i++) 236 | cout << move->op3.pro[i] << " "; 237 | cout << endl; 238 | } 239 | move = move->next; 240 | } 241 | return; 242 | } 243 | 244 | void database::outputNew() 245 | { 246 | return; 247 | } 248 | 249 | void database::showDepartment() 250 | { 251 | cout << endl << endl; 252 | cout << "The Department is:" << endl; 253 | for (unsigned int i = 0; i < department.pro.size(); i++) 254 | cout << department.pro[i] << " "; 255 | cout << endl << endl; 256 | return; 257 | } 258 | 259 | void database::showEmployee() 260 | { 261 | cout << endl << endl; 262 | cout << "The Employee is:" << endl; 263 | for (unsigned int i = 0; i < employee.pro.size(); i++) 264 | cout << employee.pro[i] << " "; 265 | cout << endl << endl; 266 | return; 267 | } 268 | 269 | void database::showProject() 270 | { 271 | cout << endl << endl; 272 | cout << "The Project is:" << endl; 273 | for (unsigned int i = 0; i < project.pro.size(); i++) 274 | cout << project.pro[i] << " "; 275 | cout << endl << endl; 276 | return; 277 | } 278 | 279 | void database::showWorks_on() 280 | { 281 | cout << endl << endl; 282 | cout << "The Works_on is:" << endl; 283 | for (unsigned int i = 0; i < works_on.pro.size(); i++) 284 | cout << works_on.pro[i] << " "; 285 | cout << endl << endl; 286 | return; 287 | } 288 | 289 | void database::showInput() 290 | { 291 | cout << endl << endl; 292 | cout << "The sentence is:" << endl; 293 | cout << input << endl; 294 | cout << endl; 295 | return; 296 | } 297 | 298 | void database::showSql() 299 | { 300 | cout << endl << endl; 301 | cout << "The sql is:" << endl; 302 | for (unsigned int i = 0; i < sql.size(); i++) 303 | cout << sql[i] << endl; 304 | cout << endl; 305 | return; 306 | } -------------------------------------------------------------------------------- /Experiment5/query.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #if !defined TREE_H 3 | #define TREE_H 4 | 5 | #include 6 | #include 7 | #include 8 | #define N 100 9 | 10 | using namespace std; 11 | 12 | typedef struct condition 13 | { 14 | string pro; 15 | string op; 16 | string value; 17 | struct condition(string a, string b, string c) 18 | { 19 | pro = a; 20 | op = b; 21 | value = c; 22 | } 23 | } condition; 24 | 25 | typedef struct relation //the struct of relationship 26 | { 27 | string name; 28 | vector pro; 29 | } relation; 30 | 31 | typedef struct projection //the struct of projection 32 | { 33 | vector pro; 34 | } projection; 35 | 36 | typedef struct select //the struct of select 37 | { 38 | vector cond; 39 | } select; 40 | 41 | typedef struct join 42 | { 43 | string name1; 44 | string name2; 45 | } join; 46 | 47 | typedef struct treeNode 48 | { 49 | int state; // 1>>join, 2>>select, 3>>projection 50 | join op1; 51 | select op2; 52 | projection op3; 53 | struct treeNode *next; 54 | } treeNode; 55 | 56 | class database 57 | { 58 | public: 59 | database(); 60 | ~database(); 61 | void readInput(); //read the input 62 | 63 | void lexSql(); //lex analysis 64 | void parseSql(); //syntax analysis: get the basic query tree 65 | void clearSql(); //clear the sql for the next test 66 | 67 | void optimizeTree(); //optimize the query tree with heri.. 68 | void insertNode(treeNode *node); 69 | treeNode *getHead(); 70 | treeNode *getLastNode(); 71 | void clearTree(treeNode *node); 72 | 73 | void outputOld(); //output the basic tree 74 | void outputNew(); //output the optimize tree 75 | 76 | void showDepartment(); //show the pro of department 77 | void showEmployee(); //show .. 78 | void showProject(); //show .. 79 | void showWorks_on(); //show .. 80 | void showInput(); //show .. 81 | void showSql(); //show .. 82 | private: 83 | string input; 84 | vector sql; 85 | treeNode *treeHead; 86 | relation department; 87 | relation employee; 88 | relation project; 89 | relation works_on; 90 | }; 91 | 92 | #endif -------------------------------------------------------------------------------- /Experiment5/sql.txt: -------------------------------------------------------------------------------- 1 | select [ ename = 'marry' & dname = 'research' ] ( employee join department ) 2 | 3 | theta ename = 'marry' & dname = 'research' 4 | x 5 | employee department 6 | 7 | x 8 | employee(ename = 'marry') department(dname = 'research') 9 | 10 | 11 | 12 | 13 | projection [ bdate ] ( select [ ename = 'jhon' & dname = 'research' ] ( employee join department ) ) 14 | 15 | projection bdate 16 | theta ename = 'jhon' & dname = 'research' 17 | x 18 | employee department 19 | 20 | projection bdate 21 | x 22 | employee(ename = 'john') department(dname = 'research') 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | select [ essn = '01' ] ( projection [ essn , pname ] ( works_on join project ) ) 31 | theta essn = '01' 32 | projection essn , pname 33 | x 34 | works_on project 35 | 36 | projection essn, panme 37 | x 38 | works_on(essn = '01') project -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Database 2 | 哈工大数据库实验:2016年 3 | 4 | #实验一 5 | 正常的sql语句 6 | 7 | #实验二 8 | Python中链接数据库:执行sql语句并将结果输出 9 | 10 | #实验三 11 | 实现一个数据库系统:基于PyQt4写的界面,并实现实验要求 12 | 13 | #实验四 14 | 1)实现关系选择算法; 15 | 2)实现关系投影算法; 16 | 3)实现连接操作算法(要求实现三种连接操作算法:Nest-Loop- Join算法,Hash-Join算法); 17 | 4)实现集合并操作算法(要求实现基于排序的并操作执行算法); 18 | 19 | #实验五 20 | select [ ename = 'marry' & dname = 'research' ] ( employee join department ) 21 | projection [ bdate ] ( select [ ename = 'jhon' & dname = 'research' ] ( employee join department ) ) 22 | select [ essn = '01' ] ( projection [ essn , pname ] ( works_on join project ) ) 23 | 24 | select * from table order by table_feature into outfile 'filename'; --------------------------------------------------------------------------------