|
| 1 | +# MySQL Connector/C++ |
| 2 | + |
| 3 | +This is a release of MySQL Connector/C++, [the C++ interface](https://dev.mysql.com/doc/dev/connector-cpp/8.0/) for communicating with MySQL servers. |
| 4 | + |
| 5 | +For detailed information please visit the official [MySQL Connector/C++ documentation](https://dev.mysql.com/doc/dev/connector-cpp/8.0/). |
| 6 | + |
| 7 | +## Licensing |
| 8 | + |
| 9 | +Please refer to files README and LICENSE, available in this repository, and [Legal Notices in documentation](https://dev.mysql.com/doc/connector-cpp/8.0/en/preface.html) for further details. |
| 10 | + |
| 11 | +## Download & Install |
| 12 | + |
| 13 | +MySQL Connector/C++ can be installed from pre-compiled packages that can be downloaded from the [MySQL downloads page](https://dev.mysql.com/downloads/connector/cpp/). |
| 14 | +The process of installing of Connector/C++ from a binary distribution is described in [MySQL online manuals](https://dev.mysql.com/doc/connector-cpp/8.0/en/connector-cpp-installation-binary.html) |
| 15 | + |
| 16 | +### Building from sources |
| 17 | + |
| 18 | +MySQL Connector/C++ can be installed from the source. Please check [MySQL online manuals](https://dev.mysql.com/doc/connector-cpp/8.0/en/connector-cpp-installation-source.html) |
| 19 | + |
| 20 | +### GitHub Repository |
| 21 | + |
| 22 | +This repository contains the MySQL Connector/C++ source code as per latest released version. You should expect to see the same contents here and within the latest released Connector/C++ package. |
| 23 | + |
| 24 | +## Sample Code |
| 25 | + |
| 26 | +``` |
| 27 | +#include <iostream> |
| 28 | +#include <mysqlx/xdevapi.h> |
| 29 | +
|
| 30 | +using ::std::cout; |
| 31 | +using ::std::endl; |
| 32 | +using namespace ::mysqlx; |
| 33 | +
|
| 34 | +
|
| 35 | +int main(int argc, const char* argv[]) |
| 36 | +try { |
| 37 | +
|
| 38 | + const char *url = (argc > 1 ? argv[1] : "mysqlx://root@127.0.0.1"); |
| 39 | +
|
| 40 | + cout << "Creating session on " << url |
| 41 | + << " ..." << endl; |
| 42 | +
|
| 43 | + Session sess(url); |
| 44 | +
|
| 45 | + cout <<"Session accepted, creating collection..." <<endl; |
| 46 | +
|
| 47 | + Schema sch= sess.getSchema("test"); |
| 48 | + Collection coll= sch.createCollection("c1", true); |
| 49 | +
|
| 50 | + cout <<"Inserting documents..." <<endl; |
| 51 | +
|
| 52 | + coll.remove("true").execute(); |
| 53 | +
|
| 54 | + { |
| 55 | + DbDoc doc(R"({ "name": "foo", "age": 1 })"); |
| 56 | +
|
| 57 | + Result add = |
| 58 | + coll.add(doc) |
| 59 | + .add(R"({ "name": "bar", "age": 2, "toys": [ "car", "ball" ] })") |
| 60 | + .add(R"({ "name": "bar", "age": 2, "toys": [ "car", "ball" ] })") |
| 61 | + .add(R"({ |
| 62 | + "name": "baz", |
| 63 | + "age": 3, |
| 64 | + "date": { "day": 20, "month": "Apr" } |
| 65 | + })") |
| 66 | + .add(R"({ "_id": "myuuid-1", "name": "foo", "age": 7 })") |
| 67 | + .execute(); |
| 68 | +
|
| 69 | + std::list<string> ids = add.getGeneratedIds(); |
| 70 | + for (string id : ids) |
| 71 | + cout <<"- added doc with id: " << id <<endl; |
| 72 | + } |
| 73 | +
|
| 74 | + cout <<"Fetching documents..." <<endl; |
| 75 | +
|
| 76 | + DocResult docs = coll.find("age > 1 and name like 'ba%'").execute(); |
| 77 | +
|
| 78 | + int i = 0; |
| 79 | + for (DbDoc doc : docs) |
| 80 | + { |
| 81 | + cout <<"doc#" <<i++ <<": " <<doc <<endl; |
| 82 | +
|
| 83 | + for (Field fld : doc) |
| 84 | + { |
| 85 | + cout << " field `" << fld << "`: " <<doc[fld] << endl; |
| 86 | + } |
| 87 | +
|
| 88 | + string name = doc["name"]; |
| 89 | + cout << " name: " << name << endl; |
| 90 | +
|
| 91 | + if (doc.hasField("date") && Value::DOCUMENT == doc.fieldType("date")) |
| 92 | + { |
| 93 | + cout << "- date field" << endl; |
| 94 | + DbDoc date = doc["date"]; |
| 95 | + for (Field fld : date) |
| 96 | + { |
| 97 | + cout << " date `" << fld << "`: " << date[fld] << endl; |
| 98 | + } |
| 99 | + string month = doc["date"]["month"]; |
| 100 | + int day = date["day"]; |
| 101 | + cout << " month: " << month << endl; |
| 102 | + cout << " day: " << day << endl; |
| 103 | + } |
| 104 | +
|
| 105 | + if (doc.hasField("toys") && Value::ARRAY == doc.fieldType("toys")) |
| 106 | + { |
| 107 | + cout << "- toys:" << endl; |
| 108 | + for (auto toy : doc["toys"]) |
| 109 | + { |
| 110 | + cout << " " << toy << endl; |
| 111 | + } |
| 112 | + } |
| 113 | +
|
| 114 | + cout << endl; |
| 115 | + } |
| 116 | + cout <<"Done!" <<endl; |
| 117 | +} |
| 118 | +catch (const mysqlx::Error &err) |
| 119 | +{ |
| 120 | + cout <<"ERROR: " <<err <<endl; |
| 121 | + return 1; |
| 122 | +} |
| 123 | +catch (std::exception &ex) |
| 124 | +{ |
| 125 | + cout <<"STD EXCEPTION: " <<ex.what() <<endl; |
| 126 | + return 1; |
| 127 | +} |
| 128 | +catch (const char *ex) |
| 129 | +{ |
| 130 | + cout <<"EXCEPTION: " <<ex <<endl; |
| 131 | + return 1; |
| 132 | +} |
| 133 | +
|
| 134 | +``` |
| 135 | + |
| 136 | +## Documentation |
| 137 | + |
| 138 | +* [MySQL](http://www.mysql.com/) |
| 139 | +* [Connector/C++ API Reference](https://dev.mysql.com/doc/dev/connector-cpp/8.0/) |
| 140 | + |
| 141 | +## Questions/Bug Reports |
| 142 | + |
| 143 | +* [Discussion Forum](https://forums.mysql.com/list.php?167) |
| 144 | +* [Slack](https://mysqlcommunity.slack.com) |
| 145 | +* [Bugs](https://bugs.mysql.com) |
| 146 | + |
0 commit comments