-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
256 lines (200 loc) · 5.15 KB
/
main.cpp
File metadata and controls
256 lines (200 loc) · 5.15 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
#include <sstream>
#include<string>
#include <sqlite3.h>
#include<cstdlib>
using namespace std;
/*Global Variable */
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;
const char* data = "Callback function called";
static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
int i;
for(i = 0; i<argc; i++)
{
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
class Student
{
protected:
string roll,clas;
string name,phone;
};
class Result:public Student
{
string m1,m2,m3;
string total,avg;
public:
void accept();
void display();
void update();
void deletep();
void create();
};
void Result::create()
{
/* Create SQL statement */
sql = "CREATE TABLE STUDENT(" \
"ROLL_NO INT PRIMARY KEY ," \
"NAME CHAR(20)," \
"CLASS INT NOT NULL," \
"PHONENO BIGINT," \
"MATH CHAR(20),"\
"SCIENCE CHAR(20),"\
"ENGLISH CHAR(20),"\
"TOTAL INT ,"\
"AVERAGE FLOAT );";
/* Execute SQL statement */
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
if( rc != SQLITE_OK )
{
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
else
{
fprintf(stdout, "Table created successfully\n");
}
}
void Result::accept()
{
cout<<"\n\n Enter Roll number and Name: ";
cin>>roll>>name;
cout<<"\n\n Enter class and phone number: ";
cin>>clas>>phone;
cout<<"\n\n Enter 3 subject marks ";
cout<<"\n Order Math,Science and English: ";
cin>>m1>>m2>>m3;
int temp_m1 = atoi(m1.c_str());
int temp_m2 = atoi(m2.c_str());
int temp_m3 = atoi(m3.c_str());
int temp_total=temp_m1+temp_m2+temp_m3;
float temp_avg=temp_total/3.0;
// declaring output string stream
ostringstream str1,str2;
str1 << temp_total;
str2 << temp_avg;
total=str1.str();
avg=str2.str();
string query="insert into student values('"+roll+"','"+name+"','"+clas+"','"+phone+"','"+m1+"','"+m2+"','"+m3+"','"+total+"','"+avg+"');";
const char* q=query.c_str();
/* Execute SQL statement */
rc = sqlite3_exec(db,q, callback, 0, &zErrMsg);
if( rc != SQLITE_OK )
{
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
else
{
fprintf(stdout, "Records created successfully\n");
}
}
void Result::display()
{
/* Create SQL statement */
sql = "SELECT * from STUDENT";
/* Execute SQL statement */
rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
if( rc != SQLITE_OK )
{
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
else
{
fprintf(stdout, "Operation done successfully\n");
}
}
void Result:: update()
{
cout<<"\n\n Enter Class to update enter roll no where to update :";
cin>>clas>>roll;
/* Create merged SQL statement */
string query="update student set class='"+clas+"' where roll_no='"+roll+"' ";
const char* q=query.c_str();
/* Execute SQL statement */
rc = sqlite3_exec(db, q, callback, (void*)data, &zErrMsg);
if( rc != SQLITE_OK )
{
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
else
{
fprintf(stdout, "Operation done successfully\n");
}
sqlite3_close(db);
}
void Result:: deletep()
{
cout<<"\n\n Enter Student Roll Number to Delete: ";
cin>>roll;
/* Create merged SQL statement */
string query="delete from student where roll_no='"+roll+"';";
const char* q=query.c_str();
/* Execute SQL statement */
rc = sqlite3_exec(db, q, callback, (void*)data, &zErrMsg);
if( rc != SQLITE_OK )
{
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
else
{
fprintf(stdout, "Operation done successfully\n");
}
}
int main(int argc, char* argv[])
{
int ch;
Result R;
/* Open database */
rc = sqlite3_open("test.db", &db);
if( rc )
{
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return(0);
}
else
{
fprintf(stdout, "Opened database successfully\n");
}
while(1)
{
cout<<"\n\n Menu: ";
cout<<"\n 1.Create Table ";
cout<<"\n 2.Update ";
cout<<"\n 3.Delete ";
cout<<"\n 4.Display ";
cout<<"\n 5.Insert";
cout<<"\n 6.Exit";
cout<<"\n\n Enter your Choice(1/2/3/4/5): ";
cin>>ch;
switch(ch)
{
case 1: R.create();
break;
case 2: R.update();
break;
case 3: R.deletep();
break;
case 4: R.display();
break;
case 5: R.accept();
break;
case 6: exit(0);
default: cout<<"\n\n Wrong Choice. ";
}
}
sqlite3_close(db);
return 0;
}