1 | // http://dev.mysql.com/doc/refman/5.5/en/connector-cpp-examples-complete-example-2.html |
---|
2 | // g++ -Wall -g twee.cpp -lmysqlcppconn |
---|
3 | #include <stdlib.h> |
---|
4 | #include <iostream> |
---|
5 | |
---|
6 | /* |
---|
7 | Include directly the different |
---|
8 | headers from cppconn/ and mysql_driver.h + mysql_util.h |
---|
9 | (and mysql_connection.h). This will reduce your build time! |
---|
10 | */ |
---|
11 | #include "mysql_connection.h" |
---|
12 | |
---|
13 | #include <cppconn/driver.h> |
---|
14 | #include <cppconn/exception.h> |
---|
15 | #include <cppconn/resultset.h> |
---|
16 | #include <cppconn/statement.h> |
---|
17 | #include <cppconn/prepared_statement.h> |
---|
18 | |
---|
19 | using namespace std; |
---|
20 | |
---|
21 | int main(void) |
---|
22 | { |
---|
23 | cout << endl; |
---|
24 | |
---|
25 | try { |
---|
26 | sql::Driver *driver; |
---|
27 | sql::Connection *con; |
---|
28 | sql::Statement *stmt; |
---|
29 | sql::ResultSet *res; |
---|
30 | sql::PreparedStatement *pstmt; |
---|
31 | |
---|
32 | /* Create a connection */ |
---|
33 | driver = get_driver_instance(); |
---|
34 | con = driver->connect("tcp://127.0.0.1:3307", "stopos", "x"); |
---|
35 | /* Connect to the MySQL test database */ |
---|
36 | con->setSchema("stopos"); |
---|
37 | |
---|
38 | stmt = con->createStatement(); |
---|
39 | stmt->execute("DROP TABLE IF EXISTS test"); |
---|
40 | stmt->execute("CREATE TABLE test(pkey varchar(20),primary key(pkey)," |
---|
41 | "index(pkey),data varchar(1024))"); |
---|
42 | |
---|
43 | stmt->execute("replace test values('3','wassenaar')"); |
---|
44 | stmt->execute("replace test values('2','den haag')"); |
---|
45 | stmt->execute("replace test values('1','bodegraven')"); |
---|
46 | stmt->execute("replace test values('1','bodegraven')"); |
---|
47 | |
---|
48 | res = stmt->executeQuery("select data from test where pkey='3'"); |
---|
49 | res->beforeFirst(); |
---|
50 | if (res->next()) |
---|
51 | cout << "[" <<res->getString("data")<<"]" << endl; |
---|
52 | else |
---|
53 | cout << "NOT FOUND\n"; |
---|
54 | res = stmt->executeQuery("select data from test where pkey='30'"); |
---|
55 | res->beforeFirst(); |
---|
56 | if (res->next()) |
---|
57 | cout << "[" <<res->getString("data")<<"]" << endl; |
---|
58 | else |
---|
59 | cout << "NOT FOUND\n"; |
---|
60 | bool rc; |
---|
61 | rc = stmt->execute("delete from test where pkey ='1'"); |
---|
62 | cout << "rc:" << rc << endl; |
---|
63 | rc = stmt->execute("delete from test where pkey ='11'"); |
---|
64 | cout << "rc:" << rc << endl; |
---|
65 | cout << "Testing count:\n"; |
---|
66 | res = stmt->executeQuery("select count(*) as count " |
---|
67 | "from information_schema.tables " |
---|
68 | "where table_schema = 'stopos' " |
---|
69 | "and table_name = 'test'"); |
---|
70 | res->beforeFirst(); |
---|
71 | if (res->next()) |
---|
72 | cout << "table [" <<res->getString("count")<<"] found" << endl; |
---|
73 | else |
---|
74 | cout << "table NOT FOUND\n"; |
---|
75 | res = stmt->executeQuery("select count(*) as count " |
---|
76 | "from information_schema.tables " |
---|
77 | "where table_schema = 'stopos' " |
---|
78 | "and table_name = 'tost'"); |
---|
79 | res->beforeFirst(); |
---|
80 | if (res->next()) |
---|
81 | cout << "table [" <<res->getInt("count")<<"] found" << endl; |
---|
82 | else |
---|
83 | cout << "table NOT FOUND\n"; |
---|
84 | |
---|
85 | delete res; |
---|
86 | delete stmt; |
---|
87 | delete con; |
---|
88 | |
---|
89 | } catch (sql::SQLException &e) { |
---|
90 | cout << "# ERR: SQLException in " << __FILE__; |
---|
91 | cout << "(" << __FUNCTION__ << ") on line " |
---|
92 | << __LINE__ << endl; |
---|
93 | cout << "# ERR: " << e.what(); |
---|
94 | cout << " (MySQL error code: " << e.getErrorCode(); |
---|
95 | cout << ", SQLState: " << e.getSQLState() << |
---|
96 | " )" << endl; |
---|
97 | } |
---|
98 | |
---|
99 | cout << endl; |
---|
100 | |
---|
101 | return EXIT_SUCCESS; |
---|
102 | } |
---|