3 #include "/usr/local/pgsql/include/libpq/fe.h"
4 PGconn * p_connection;
5 PGresult * p_result;
6
7 #define MAX_QUERY_LENGTH 600
8
9 char * dbname;
10 char * password;
11 char * host;
12 char * user;
13 char * port;
14
15 int Insert(char *);
16 int Select(char *);
17 void Connect();
18
19 void main()
20 {
21 char *i2;
22
23 i2 = (char *)malloc(MAX_QUERY_LENGTH);
24
25 snprintf(i2, MAX_QUERY_LENGTH,"INSERT INTO tcphdr (sid,cid,th_sport,th_dport,th_flags,th_win,th_urp) VALUES ('%i','%i','%i','%i','%i','%i','%i')",
26 1, 99999, 9999,9999,0,0,99,99);
27
28 Connect();
29
30 Insert(i2); free(i2);
31 }
32
33 int Insert(char * query)
34 {
35 int result = 0;
36
37 p_result = PQexec(p_connection,query);
38 if(!(PQresultStatus(p_result) != PGRES_COMMAND_OK))
39 {
40 result = 1;
41 }
42 if(!result)
43 {
44 ErrorMessage("Postgres insert Error %s\n",PQerrorMessage(p_connection));
45 }
46
47 if (result) { printf("(%s) executed\n", query); }
48 else { printf("(%s) failed\n", query); }
49
50 return result;
51 }
52
53 int Select(char * query)
54 {
55 int result = 0;
56
57 p_result = PQexec(p_connection,query);
58 if((PQresultStatus(p_result) == PGRES_TUPLES_OK))
59 {
60 if(PQntuples(p_result))
61 {
62 if((PQntuples(p_result)) > 1)
63 {
64 ErrorMessage("ERROR (%s) returned more than one result\n", query);
65 result = 0;
66 }
67 else
68 {
69 result = atoi(PQgetvalue(p_result,0,0));
70 }
71 }
72 }
73 if(!result)
74 {
75 ErrorMessage("Error: %s\n",PQerrorMessage(p_connection));
76 }
77
78 return result;
79 }
80
81
82 void Connect()
83 {
84 strncpy(host,"localhost",10);
85 strncpy(port,"5432",5);
86 strncpy(dbname,"snort",6);
87 strncpy(user,"root",5);
88 strncpy(password,"wsp1005",8);
89
90 p_connection = PQsetdbLogin(host,port,NULL,NULL,dbname,user,password);
91 //p_connection = PQsetdbLogin('localhost','5432',NULL,NULL,'snort','root','wsp1005');
92 if (PQstatus(p_connection) == CONNECTION_BAD)
93 {
94 PQfinish(p_connection);
95 FatalError("Connection to database %s failed\n", "snort");
96 }
97 }
에러내용...
dbtest.c: In function `main':
dbtest.c:4: warning: return type of `main' is not `int'
/tmp/ccRthada.o: In function `Insert':
/tmp/ccRthada.o(.text+0x80): undefined reference to `PQexec'
/tmp/ccRthada.o(.text+0x95): undefined reference to `PQresultStatus'
/tmp/ccRthada.o(.text+0xb7): undefined reference to `PQerrorMessage'
/tmp/ccRthada.o(.text+0xc7): undefined reference to `ErrorMessage'
/tmp/ccRthada.o: In function `Select':
/tmp/ccRthada.o(.text+0x11c): undefined reference to `PQexec'
/tmp/ccRthada.o(.text+0x131): undefined reference to `PQresultStatus'
|