#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. Introduction of Servlet Filter(了解Servlet之Filter)

    API文档中介绍了public Interface Filter(公共接口过滤器) Servlet API文档中是这样介绍的: ‘A filter is an object that performs ...

  2. 关于controller和apicontroller的跨域实现过滤器的不同

    1.controller的跨域访问 filter的实现请继承System.Web.Mvc.ActionFilterAttribute 2.apicontroller的跨域访问 filter的实现请继承 ...

  3. 位运算(3)——Reverse Bits

    翻转32位无符号二进制整数 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (r ...

  4. drupal node机制理解

    [1]根据结构的功能结构的不同,drupal划分为,node,user,comment等不同的结构,他们的结构是不同的.他们可以作为四个不同的抽象类,根据这个抽象类,分别有一套hook函数去控制实现的 ...

  5. HBase伪分布式安装(HDFS)+ZooKeeper安装+HBase数据操作+HBase架构体系

    HBase1.2.2伪分布式安装(HDFS)+ZooKeeper-3.4.8安装配置+HBase表和数据操作+HBase的架构体系+单例安装,记录了在Ubuntu下对HBase1.2.2的实践操作,H ...

  6. Android FlycoDialog 简单实用的自定义Android弹窗对话框之Dialog篇

    效果图镇楼   FlycoDialog是一款非常棒的弹窗对话框处理框架,今天在这里主要讲一下他的自定义弹出对话框的功能,这里以第二幅效果图为例,图片已经放在博客最下方,X号自己随便找一个东西代替吧.  ...

  7. LeetCode刷题系列——Add Two Numbers

    题目链接 这个题目很简单,归并而已,好久没练编程,居然忘了在使用自定义类型前,要进行初始化(new操作). class ListNode{ int val; ListNode next; ListNo ...

  8. PhoneGap API介绍:Events

    事件类型: backbutton deviceready menubutton pause resume searchbutton online offline backbutton 当用户在Andr ...

  9. Java—集合框架List

    集合的概念 现实生活中:很多的事物凑在一起 数学中的集合:具有共同属性的事物的总和 Java中的集合类:是一种工具类,就像是容器,存储任意数量的具有共同属性的对象 集合的作用 在类的内部,对数据进行组 ...

  10. Windows C/C++ 内存泄露检测

    #pragma once #define _CRTDBG_MAP_ALLOC #include<crtdbg.h> #include<stdlib.h> #include< ...