#include <iostream>
#include <vector>
#include <mysqlx/xdevapi.h> using std::cout;
using std::endl; int main(void)
try
{
mysqlx::Session sess("mysqlx://root:mysql@localhost:33060?ssl-mode=disabled"); //参数true表示检测给定的"D_COMPANY"是否存在, 如果不存在则抛出异常
//"D_COMPANY" 为使用 【CREATE DATABASE XXXX】 语法创建的数据库名
mysqlx::Schema db = sess.getSchema("D_COMPANY", true); //"C_DOCUMENT" 为表名
//true 表示返回与此字符串匹配的已存在的表,不存在则抛出异常
cout << ">> create collection.." << endl;
mysqlx::Collection coll = db.createCollection("C_DOCUMENT", true); #ifdef INSERT_DOCUMENT
cout << ">> add document.." << endl;
mysqlx::Result ret;
//注意: _id字段是必要的
ret = coll.add(
R"(
{
"_id": "test",
"name": "Awesome 4K",
"resolutions": [{
"width": 1280,
"height": 720
}, {
"width": 1920,
"height": 1080
}, {
"width": 3840,
"height": 2160
}]
}
)").execute(); std::vector<std::string> idList = ret.getGeneratedIds(); cout << ">> print ID..." << endl;
for (std::string str : idList) {
cout << str << endl;
}
#endif #ifdef DELETE_DOCUMENT
cout << ">> drop document..." << endl;
mysqlx::Result ret = coll.removeOne(std::string("test"));
cout << ">> Affect items: " << ret.getAffectedItemsCount() << endl;
#endif //得到_id为“test”的json数据
mysqlx::DbDoc doc = coll.getOne(std::string("test"));
if (doc.isNull()) {
cout << "the doc is null." << endl;
return 0;
} else {
cout << R"(">> Get a document with an identifier of "test")" << endl;
} cout << ">> print document..." << endl;
doc.print(cout);
cout << endl; if (doc.fieldType("name")
== int(mysqlx::Value::STRING))
{
cout << "name: " << doc["name"].get<std::string>() << endl;
} if (doc.fieldType("resolutions")
== int(mysqlx::Value::ARRAY))
{
mysqlx::DbDoc resDoc = doc["resolutions"].get<mysqlx::DbDoc>(); if (resDoc.isNull()) {
cout << "res doc is null." << endl;
return -1;
} cout << "-----------------------" << endl;
resDoc.print(cout);
cout << endl;
} cout << endl << ">> Done!" << endl;
}
catch(mysqlx::Error &err)
{
cout << "ERROR: " << err << endl;
return -1;
}

MySql Connector/c++8中JSON处理Demo的更多相关文章

  1. MySql Connector/C++8结果集处理Demo

    #include <iostream> #include <exception> #include <mysqlx/xdevapi.h> using std::co ...

  2. 安装MySQL Connector/C++并将其配置到VS2015中

    安装MySQL Connector/C++并将其配置到VS中 1.下载MySQL Connector/C++并安装 在下载地址:https://dev.mysql.com/downloads/conn ...

  3. MySQL中JSON字段的使用技巧

    mysql5.7.8之后开始原生支持json. 在类似mongodb这种nosql数据库中,json存储数据是非常自然的, 在mysql中合理的使用json,能够带来极大的便利 Json字段的使用场景 ...

  4. vc++2013中使用MySQL connector/C++ 1.1.4静态链接报错

    包含头文件 #include <mysql_connection.h> #include <mysql_driver.h> #include <cppconn/state ...

  5. 创建ASP.NET Core MVC应用程序(2)-利用MySQL Connector NET连接到MySQL

    创建ASP.NET Core MVC应用程序(2)-利用MySQL Connector NET连接到MySQL 用惯.NET的研发人员都习惯性地使用SQLServer作为数据库.然而.NET Core ...

  6. mysql Connector C++ 操作数据库 vs2012

    最近想写一个应用程序,要连接和操作mysql数据库,以前只是用c++ builder 操作过mysql数据库,那是用控件操作的,感觉比较弱智,但是c++ builder vcl控件感觉在多线程里比较坑 ...

  7. [转]MySQL Connector/C++(一)

    http://www.cnblogs.com/dvwei/archive/2013/04/18/3029464.html#undefined#undefined MySQL Connector/C++ ...

  8. Asp.net中Json的序列化和反序列化(一)

    JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍 ...

  9. mybatis/callablestatement调用存储过程mysql connector产生不必要的元数据查询

    INFO | jvm 1 | 2016/08/25 15:17:01 | 16-08-25 15:17:01 DEBUG pool-1-thread-371dao.ITaskDao.callProce ...

随机推荐

  1. Thrift笔记(四)--Thrift client源码分析

    thrift文件 namespace java com.gxf.demo namespace py tutorial typedef i32 int // We can use typedef to ...

  2. linux shell基础编程2

    while循环 语法1: while [ 条件 ] do 命令序列 done 语法2: while read -r line do 命令序列 done (切记while和左中括号一定要有空格) 例子 ...

  3. PAT 1062 Talent and Virtue

    #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #i ...

  4. 前端路由vue-router介绍

    一.前端路由vue-router介绍 Vue-Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌.包含的功能有: 嵌套的路由/视图表 模 ...

  5. Mybatis 的多个数据源

    主要思路: DataSource --> SqlSessionFactory --> SqlSessionTemplate 第一方式: 定义多套  DataSource --> Sq ...

  6. 《ArcGIS Runtime SDK for Android开发笔记》——(11)、ArcGIS Runtime SDK常见空间数据加载

    ArcGIS Runtime SDK for Android 支持多种类型空间数据源.每一种都提供了相应的图层来直接加载,图层Layer是空间数据的载体,其主要继承关系及类型说明如下图所示: 转载请注 ...

  7. 多线程(七)~join方法的使用

    作用:join()方法的作用是等待线程对象销毁.     join()方法具有能使线程排队运行的作用,有点类似于同步的效果.       join与synchronize的区别:         jo ...

  8. C#程序集

    我的理解: 程序集是一个物理上的概念,一个项目生成的exe或者dll都可以叫做一个程序集,internal修饰的内容可以在程序集内可见.

  9. 定时备份SQL Server数据库

    一.手动备份: 1.整个数据库备份:选择数据库 => 右键任务 => 备份: 2.导出一张表的框架:选择表 => 编写表脚本为 => CREATE到: 3.导出一张表的数据:  ...

  10. 笨办法学Python(四十)

    习题 40: 字典, 可爱的字典 接下来我要教你另外一种让你伤脑筋的容器型数据结构,因为一旦你学会这种容器,你将拥有超酷的能力.这是最有用的容器:字典(dictionary). Python 将这种数 ...