-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearSearch.cpp
More file actions
149 lines (121 loc) · 3.54 KB
/
linearSearch.cpp
File metadata and controls
149 lines (121 loc) · 3.54 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
#include <sqlite3.h>
#include <stdio.h>
#include <iostream>
using namespace std;
string rollNo[10];
int c = 0;
static int createDB(const char* s);
static int createTable(const char* s);
static int insertData(const char* s);
static int selectData(const char* s);
static int callback(void* NotUsed, int argc, char** argv, char** azColName);
void linearSearch(string toSearch) {
int i = 0;
while (i < 6) {
if (rollNo[i] == toSearch) {
cout << "Roll no. " << toSearch << " found at position " << i + 1 << endl;
break;
}
i++;
if (i == 6) {
cout << "Roll no. " << toSearch << " not found!\n";
}
}
}
int main()
{
const char* dir = R"(D:\DeleteMe\STUDENTS.db)";
createDB(dir);
//createTable(dir);
//insertData(dir); // uncomment the deleteData above to avoid duplicates
selectData(dir);
string toSearch;
cout << "Enter Element to Search: ";
cin >> toSearch;
linearSearch(toSearch);
return 0;
}
static int createDB(const char* s)
{
sqlite3* DB;
int exit = 0;
exit = sqlite3_open(s, &DB);
sqlite3_close(DB);
return 0;
}
static int createTable(const char* s)
{
sqlite3* DB;
char* messageError;
string sql = "CREATE TABLE IF NOT EXISTS StudentData("
"ROLLNO INT NOT NULL, "
"FNAME TEXT NOT NULL, "
"LNAME TEXT NOT NULL); ";
try
{
int exit = 0;
exit = sqlite3_open(s, &DB);
/* An open database, SQL to be evaluated, Callback function, 1st argument to callback, Error msg written here */
exit = sqlite3_exec(DB, sql.c_str(), callback, 0, &messageError);
if (exit != SQLITE_OK) {
cerr << "Error in createTable function." << endl;
sqlite3_free(messageError);
}
else {
cout << "Table created Successfully" << endl;
}
sqlite3_close(DB);
}
catch (const exception& e)
{
cerr << e.what();
}
return 0;
}
static int insertData(const char* s)
{
sqlite3* DB;
char* messageError;
string sql(
"INSERT INTO StudentData(ROLLNO, FNAME, LNAME) VALUES(1915084, 'Varanpreet', 'Kaur');"
"INSERT INTO StudentData (ROLLNO, FNAME, LNAME) VALUES(1915085, 'Vishal', 'Dutta');"
"INSERT INTO StudentData (ROLLNO, FNAME, LNAME) VALUES(1915086, 'Vrishti', 'Gupta');"
"INSERT INTO StudentData(ROLLNO, FNAME, LNAME) VALUES(1915087, 'Yuvraj', 'Khanna');"
"INSERT INTO StudentData (ROLLNO, FNAME, LNAME) VALUES(1915103, 'Priyanka', 'Jhamb');"
"INSERT INTO StudentData (ROLLNO, FNAME, LNAME) VALUES(1915105, 'Amanjot', 'Singh');"
);
int exit = sqlite3_open(s, &DB);
/* An open database, SQL to be evaluated, Callback function, 1st argument to callback, Error msg written here */
exit = sqlite3_exec(DB, sql.c_str(), NULL, 0, &messageError);
if (exit != SQLITE_OK) {
cerr << "Error in insertData function." << endl;
sqlite3_free(messageError);
}
else
cout << "Records inserted Successfully!" << endl;
return 0;
}
static int selectData(const char* s)
{
sqlite3* DB;
char* messageError;
string sql = "SELECT * FROM StudentData;";
int exit = sqlite3_open(s, &DB);
/* An open database, SQL to be evaluated, Callback function, 1st argument to callback, Error msg written here*/
exit = sqlite3_exec(DB, sql.c_str(), callback, NULL, &messageError);
if (exit != SQLITE_OK) {
cerr << "Error in selectData function." << endl;
sqlite3_free(messageError);
}
else
cout << "Records selected Successfully!" << endl;
return 0;
}
// retrieve contents of database used by selectData()
/* argc: holds the number of results, argv: holds each value in array, azColName: holds each column returned in array, */
static int callback(void* NotUsed, int argc, char** argv, char** azColName)
{
rollNo[c] = argv[0];
c++;
return 0;
}