2
Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
4
The MySQL Connector/C++ is licensed under the terms of the GPLv2
5
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
6
MySQL Connectors. There are special exceptions to the terms and
7
conditions of the GPLv2 as it is applied to this software, see the
8
FLOSS License Exception
9
<http://www.mysql.com/about/legal/licensing/foss-exception.html>.
11
This program is free software; you can redistribute it and/or modify
12
it under the terms of the GNU General Public License as published
13
by the Free Software Foundation; version 2 of the License.
15
This program is distributed in the hope that it will be useful, but
16
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20
You should have received a copy of the GNU General Public License along
21
with this program; if not, write to the Free Software Foundation, Inc.,
22
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28
* Basic example demonstrating how to check the type of a result set column
32
/* Standard C++ includes */
38
/* Public interface of the MySQL Connector/C++ */
39
#include <driver/mysql_public_iface.h>
40
/* Connection parameter and sample data */
51
int main(int argc, const char **argv)
53
const string url(argc >= 2 ? argv[1] : EXAMPLE_HOST);
54
const string user(argc >= 3 ? argv[2] : EXAMPLE_USER);
55
const string pass(argc >= 4 ? argv[3] : EXAMPLE_PASS);
56
const string database(argc >= 5 ? argv[4] : EXAMPLE_DB);
61
struct _test_data min, max;
63
bool c_bool1 = true, c_bool2;
64
/* TODO: long long is not C++, its C99 !!! */
65
int64_t c_long1 = L64(9223372036854775807), c_long2;
66
double c_double1 = -999.9999, c_double2;
69
cout << "1..1" << endl;
70
cout << "# Connector/C++ result set.." << endl;
73
/* Using the Driver to create a connection */
74
driver = sql::mysql::get_driver_instance();
75
std::auto_ptr< sql::Connection > con(driver->connect(url, user, pass));
76
con->setSchema(database);
78
std::auto_ptr< sql::Statement > stmt(con->createStatement());
79
stmt->execute("DROP TABLE IF EXISTS test");
82
Note that MySQL has its very own mapping from SQL type (e.g. BOOLEAN)
83
specified in a SQL statement and type actually used. Check the MySQL
84
manual - conversions are a common cause of false bug reports!
85
Also, don't get confused by the precision of float/double columns.
86
For precision math use DECIMAL!
88
stmt->execute("CREATE TABLE test(id INT, label CHAR(1), c_bool BOOLEAN, "
89
"c_long BIGINT, c_double DOUBLE, c_null INT DEFAULT NULL)");
90
cout << "#\t Test table created" << endl;
92
std::auto_ptr< sql::PreparedStatement> prep_stmt(
93
con->prepareStatement("INSERT INTO test(id, label, c_bool, c_long, "
94
" c_double) VALUES (?, ?, ?, ?, ?)"));
96
/* Populate the test table with data */
97
min = max = test_data[0];
98
for (unsigned int i = 0; i < EXAMPLE_NUM_TEST_ROWS; i++) {
99
/* Remember min/max for further testing */
100
if (test_data[i].id < min.id) {
103
if (test_data[i].id > max.id) {
107
prep_stmt->setInt(1, test_data[i].id);
108
prep_stmt->setString(2, test_data[i].label);
109
prep_stmt->setBoolean(3, c_bool1);
110
prep_stmt->setInt64(4, c_long1);
111
prep_stmt->setDouble(5, c_double1);
112
prep_stmt->execute();
114
cout << "#\t Test table populated" << endl;
116
std::auto_ptr< sql::ResultSet > res(stmt->executeQuery("SELECT id, label, c_bool, c_long, c_double, c_null FROM test ORDER BY id ASC"));
117
while (res->next()) {
118
/* sql::ResultSet.rowsCount() returns size_t */
119
size_t row = res->getRow() - 1;
121
cout << "#\t\t Row " << res->getRow() << endl;
122
cout << "#\t\t\t id INT = " << res->getInt("id") << endl;
123
cout << "#\t\t\t id (as Integer) = " << res->getInt("id") << endl;
124
cout << "#\t\t\t id (as String) = " << res->getString("id") << endl;
125
cout << "#\t\t\t id (as Boolean) = " << res->getBoolean("id") << endl;
126
cout << "#\t\t\t id (as Long) = " << res->getInt64("id") << endl;
127
cout << "#\t\t\t id (as Double) = " << res->getDouble("id") << endl;
129
if (test_data[row].id != res->getInt(1)) {
130
throw runtime_error("Wrong results for column id");
133
std::string c_string = res->getString(2);
134
cout << "#\t\t\t label CHAR(1) = " << c_string << endl;
135
cout << "#\t\t\t label (as Integer) = " << res->getInt(2) << endl;
136
cout << "#\t\t\t label (as String) = " << res->getString(2) << endl;
137
cout << "#\t\t\t label (as Boolean) = " << res->getBoolean(2) << endl;
138
cout << "#\t\t\t label (as Long) = " << res->getInt64(2) << endl;
139
cout << "#\t\t\t label (as Double) = " << res->getDouble(2) << endl;
141
if (test_data[row].label != c_string) {
142
throw runtime_error("Wrong result for column label");
145
c_bool2 = res->getBoolean("c_bool");
146
cout << "#\t\t\t c_bool CHAR(1) = " << c_bool2 << endl;
147
cout << "#\t\t\t c_bool (as Integer) = " << res->getInt(3) << endl;
148
cout << "#\t\t\t c_bool (as String) = " << res->getString(3) << endl;
149
cout << "#\t\t\t c_bool (as Boolean) = " << res->getBoolean(3) << endl;
150
cout << "#\t\t\t c_bool (as Long) = " << res->getInt64(3) << endl;
151
cout << "#\t\t\t c_bool (as Double) = " << res->getDouble(3) << endl;
153
if (c_bool1 != c_bool2) {
154
throw runtime_error("Wrong result for column c_bool");
157
c_long2 = res->getInt64("c_long");
158
cout << "#\t\t\t c_long BIGINT = " << c_long2 << endl;
159
cout << "#\t\t\t c_long (as Integer) = " << res->getInt("c_long") << endl;
160
cout << "#\t\t\t c_long (as String) = " << res->getString("c_long") << endl;
161
cout << "#\t\t\t c_long (as Boolean) = " << res->getBoolean("c_long") << endl;
162
cout << "#\t\t\t c_long (as Long) = " << res->getInt64("c_long") << endl;
163
cout << "#\t\t\t c_long (as Double) = " << res->getDouble("c_long") << endl;
165
if (c_long1 != c_long2) {
166
throw runtime_error("Wrong result for column c_long");
169
c_double2 = res->getDouble("c_double");
170
cout << "#\t\t\t c_double DOUBLE = " << c_double2 << endl;
171
cout << "#\t\t\t c_double (as Integer) = " << res->getInt("c_double") << endl;
172
cout << "#\t\t\t c_double (as String) = " << res->getString("c_double") << endl;
173
cout << "#\t\t\t c_double (as Boolean) = " << res->getBoolean("c_double") << endl;
174
cout << "#\t\t\t c_double (as Long) = " << res->getInt64("c_double") << endl;
175
cout << "#\t\t\t c_double (as Double) = " << res->getDouble("c_double") << endl;
176
cout << "#\t\t\t c_double wasNull() = " << res->wasNull() << endl;
178
if (c_double1 != c_double2) {
179
throw runtime_error("Wrong result for column c_double");
182
c_int1 = res->getInt("c_null");
183
cout << "#\t\t\t c_null INT DEFAULT NULL = " << c_int1;
184
cout << " (isNull = " << res->isNull("c_null") << ")" << endl;
185
cout << "#\t\t\t c_null (as Integer) = " << res->getInt("c_null") << endl;
186
cout << "#\t\t\t c_null (as String) = " << res->getString("c_null") << endl;
187
cout << "#\t\t\t c_null (as Boolean) = " << res->getBoolean("c_null") << endl;
188
cout << "#\t\t\t c_null (as Long) = " << res->getInt64("c_null") << endl;
189
cout << "#\t\t\t c_null (as Double) = " << res->getDouble("c_null") << endl;
190
cout << "#\t\t\t c_null wasNull() = " << res->wasNull() << endl;
192
if (!res->isNull(6) || !res->wasNull()) {
193
throw runtime_error("isNull() or wasNull() has not reported NULL value of column c_null");
199
stmt->execute("DROP TABLE IF EXISTS test");
201
cout << "# done!" << endl;
203
} catch (sql::SQLException &e) {
205
The MySQL Connector/C++ throws three different exceptions:
207
- sql::MethodNotImplementedException (derived from sql::SQLException)
208
- sql::InvalidArgumentException (derived from sql::SQLException)
209
- sql::SQLException (derived from std::runtime_error)
211
cout << "# ERR: SQLException in " << __FILE__;
212
cout << "(" << EXAMPLE_FUNCTION << ") on line " << __LINE__ << endl;
213
/* Use what(), getErrorCode() and getSQLState() */
214
cout << "# ERR: " << e.what();
215
cout << " (MySQL error code: " << e.getErrorCode();
216
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
217
cout << "not ok 1 - examples/resultset_types.cpp" << endl;
220
} catch (std::runtime_error &e) {
222
cout << "# ERR: runtime_error in " << __FILE__;
223
cout << "(" << EXAMPLE_FUNCTION << ") on line " << __LINE__ << endl;
224
cout << "# ERR: " << e.what() << endl;
225
cout << "not ok 1 - examples/resultset_types.cpp" << endl;
230
cout << "ok 1 - examples/resultset_types.cpp" << endl;