-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaskdbfunc.cpp
More file actions
118 lines (117 loc) · 2.3 KB
/
Copy pathaskdbfunc.cpp
File metadata and controls
118 lines (117 loc) · 2.3 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
//***Function of searching in db Bellegar data fields with incoming word with searching in tabl1,2,3
#ifndef ASKDB
#define ASKDB
#include <postgresql/libpq-fe.h>
#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;
char* askdb(char* word, int tablenum)
{
PGconn *conn;
PGresult *res;
char out[300] = "";
int col, row;
conn = PQconnectdb(
"dbname=bellegar host=localhost user=bellegar password=bugboom");
if (conn == NULL)
{
strcpy(out, "no connection to bellegar DB\n");
return out;
}
string namecol;
switch (tablenum)
{
case 1:
namecol = "name";
break;
case 2:
namecol = "type";
break;
case 3:
namecol = "book";
break;
}
string comm = "select * from table";
comm += char(tablenum + (int) '0');
comm = comm + " where " + string(namecol) + " like '%" + string(word)
+ "%';";
res = PQexec(conn, comm.c_str());
row = PQntuples(res);
col = PQnfields(res);
if (row != 0)
{
for (int icol = 0; icol < col; icol++)
{
strcat(out, PQfname(res, icol));
strcat(out, "\t");
}
strcat(out, "\n");
}
for (int irow = 0; irow < row; irow++)
{
for (int icol = 0; icol < col; icol++)
{
strcat(out, PQgetvalue(res, irow, icol));
strcat(out, "\t");
}
strcat(out, "\n");
}
return out;
}
void askdb(char* word, int tablenum, char* output)
{
PGconn *conn;
PGresult *res;
char out[300] = "";
int col, row;
conn = PQconnectdb(
"dbname=bellegar host=localhost user=bellegar password=bugboom");
if (conn == NULL)
{
strcpy(out, "no connection to bellegar DB\n");
strcpy(output, out);
return;
}
string namecol;
switch (tablenum)
{
case 1:
namecol = "name";
break;
case 2:
namecol = "type";
break;
case 3:
namecol = "book";
break;
}
string comm = "select * from table";
comm += char(tablenum + (int) '0');
comm = comm + " where " + string(namecol) + " like '%" + string(word)
+ "%';";
res = PQexec(conn, comm.c_str());
row = PQntuples(res);
col = PQnfields(res);
if (row != 0)
{
for (int icol = 0; icol < col; icol++)
{
strcat(out, PQfname(res, icol));
strcat(out, "\t");
}
strcat(out, "\n");
}
for (int irow = 0; irow < row; irow++)
{
for (int icol = 0; icol < col; icol++)
{
strcat(out, PQgetvalue(res, irow, icol));
strcat(out, "\t");
}
strcat(out, "\n");
}
strcpy(output, out);
return;
}
#endif