Database 操作
更新时间:2026-09-01
创建数据库
功能介绍
新建一个库,用于进一步创建各类数据表。
请求示例
C++
1#include <iostream>
2#include <memory>
3#include <string>
4
5#include "mochow/Mochow.h"
6
7int main() {
8 const std::string account = "root";
9 const std::string api_key = "$您的账户API密钥";
10 const std::string endpoint = "$您的实例访问端点"; // 例如:http://127.0.0.1:5287
11
12 mochow::ClientOptions options;
13 options.endpoint = endpoint;
14 options.credentials.account = account;
15 options.credentials.api_key = api_key;
16
17 // create mochow client
18 auto client_result = mochow::MochowClient::Create(options);
19 if (!client_result.IsOk()) {
20 std::cerr << "Fail to init mochow client due to error: "
21 << client_result.GetStatus().Message() << std::endl;
22 return 1;
23 }
24 std::shared_ptr<mochow::MochowClient> client = client_result.MoveValue();
25
26 // create database
27 mochow::Status status = client->CreateDatabase("db_test");
28 if (!status.IsOk()) {
29 std::cerr << "Fail to create database due to error: " << status.Message()
30 << ", request_id=" << status.RequestId() << std::endl;
31 (void)client->Close();
32 return 1;
33 }
34
35 (void)client->Close();
36 return 0;
37}
请求参数
| 参数 | 参数类型 | 是否必选 | 参数配置 |
|---|---|---|---|
| database_name | const std::string& | 是 | 指定库的名称。库名称命名要求如下:InvalidArgument。 |
返回参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| status | mochow::Status | 本次调用的结果。IsOk() 为 true 表示创建成功;否则可通过 Code()、Message()、ServerCode() 和 RequestId() 定位失败原因。 |
删除数据库
功能介绍
删除指定的目标数据库,仅支持删除空库,不支持对尚有表存在的库进行递归删除,即删除之前需提前删除该数据库中的所有表,否则报错。
请求示例
C++
1#include <iostream>
2#include <memory>
3#include <string>
4
5#include "mochow/Mochow.h"
6
7int main() {
8 const std::string account = "root";
9 const std::string api_key = "$您的账户API密钥";
10 const std::string endpoint = "$您的实例访问端点"; // 例如:http://127.0.0.1:5287
11
12 mochow::ClientOptions options;
13 options.endpoint = endpoint;
14 options.credentials.account = account;
15 options.credentials.api_key = api_key;
16
17 // create mochow client
18 auto client_result = mochow::MochowClient::Create(options);
19 if (!client_result.IsOk()) {
20 std::cerr << "Fail to init mochow client due to error: "
21 << client_result.GetStatus().Message() << std::endl;
22 return 1;
23 }
24 std::shared_ptr<mochow::MochowClient> client = client_result.MoveValue();
25
26 // drop database
27 mochow::Status status = client->DropDatabase("db_test");
28 if (!status.IsOk()) {
29 std::cerr << "Fail to drop database due to error: " << status.Message()
30 << ", request_id=" << status.RequestId() << std::endl;
31 (void)client->Close();
32 return 1;
33 }
34
35 (void)client->Close();
36 return 0;
37}
请求参数
| 参数 | 参数类型 | 是否必选 | 参数配置 |
|---|---|---|---|
| database_name | const std::string& | 是 | 指定库的名称。SDK 侧会校验该参数非空,为空时直接返回 InvalidArgument。 |
返回参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| status | mochow::Status | 本次调用的结果。IsOk() 为 true 表示删除成功;库中仍有表存在时会返回服务端错误。 |
查询数据库列表
功能介绍
查询数据库列表。C++ SDK 通过出参指针写回结果,调用前需要准备一个 std::vector<mochow::DatabaseInfo> 变量。
请求示例
C++
1#include <iostream>
2#include <memory>
3#include <string>
4#include <vector>
5
6#include "mochow/Mochow.h"
7
8int main() {
9 const std::string account = "root";
10 const std::string api_key = "$您的账户API密钥";
11 const std::string endpoint = "$您的实例访问端点"; // 例如:http://127.0.0.1:5287
12
13 mochow::ClientOptions options;
14 options.endpoint = endpoint;
15 options.credentials.account = account;
16 options.credentials.api_key = api_key;
17
18 // create mochow client
19 auto client_result = mochow::MochowClient::Create(options);
20 if (!client_result.IsOk()) {
21 std::cerr << "Fail to init mochow client due to error: "
22 << client_result.GetStatus().Message() << std::endl;
23 return 1;
24 }
25 std::shared_ptr<mochow::MochowClient> client = client_result.MoveValue();
26
27 // list databases
28 std::vector<mochow::DatabaseInfo> databases;
29 mochow::Status status = client->ListDatabases(&databases);
30 if (!status.IsOk()) {
31 std::cerr << "Fail to list databases due to error: " << status.Message()
32 << ", request_id=" << status.RequestId() << std::endl;
33 (void)client->Close();
34 return 1;
35 }
36
37 std::cout << "database count: " << databases.size() << std::endl;
38 for (const mochow::DatabaseInfo& database : databases) {
39 std::cout << "database: " << database.name << std::endl;
40 }
41
42 (void)client->Close();
43 return 0;
44}
请求参数
| 参数 | 参数类型 | 是否必选 | 参数配置 |
|---|---|---|---|
| databases | std::vector<mochow::DatabaseInfo>* | 是 | 用于接收库列表的出参指针。传入空指针时直接返回 InvalidArgument;调用成功前 SDK 会先清空该容器再写入结果。 |
返回参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| status | mochow::Status | 本次调用的结果。仅在 IsOk() 为 true 时,出参 databases 的内容才有效。 |
| databases | std::vector<mochow::DatabaseInfo> | 库对象列表。 |
DatabaseInfo
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| name | std::string | 库名。 |
获取数据库对象
功能介绍
获取指定库的 mochow::Database 对象,后续的表操作、索引操作和别名操作都通过该对象发起。该接口只在本地构造句柄,不会发出网络请求,因此没有返回状态;库是否存在会在真正调用表级接口时由服务端校验。
请求示例
C++
1#include <iostream>
2#include <memory>
3#include <string>
4#include <vector>
5
6#include "mochow/Mochow.h"
7
8int main() {
9 const std::string account = "root";
10 const std::string api_key = "$您的账户API密钥";
11 const std::string endpoint = "$您的实例访问端点"; // 例如:http://127.0.0.1:5287
12
13 mochow::ClientOptions options;
14 options.endpoint = endpoint;
15 options.credentials.account = account;
16 options.credentials.api_key = api_key;
17
18 // create mochow client
19 auto client_result = mochow::MochowClient::Create(options);
20 if (!client_result.IsOk()) {
21 std::cerr << "Fail to init mochow client due to error: "
22 << client_result.GetStatus().Message() << std::endl;
23 return 1;
24 }
25 std::shared_ptr<mochow::MochowClient> client = client_result.MoveValue();
26
27 // get database handle
28 mochow::Database database = client->GetDatabase("db_test");
29 std::cout << "database name: " << database.Name() << std::endl;
30
31 // 通过 database 对象访问库内的表
32 std::vector<mochow::TableInfo> tables;
33 mochow::Status status = database.ListTables(&tables);
34 if (!status.IsOk()) {
35 std::cerr << "Fail to list tables due to error: " << status.Message()
36 << ", request_id=" << status.RequestId() << std::endl;
37 (void)client->Close();
38 return 1;
39 }
40 std::cout << "table count: " << tables.size() << std::endl;
41
42 (void)client->Close();
43 return 0;
44}
请求参数
| 参数 | 参数类型 | 是否必选 | 参数配置 |
|---|---|---|---|
| database_name | const std::string& | 是 | 指定库的名称。 |
返回参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| database | mochow::Database | 库对象。可通过 Name() 读取库名,并通过该对象调用 CreateTable、DropTable、DescribeTable、ListTables、ModifyTable、AliasTable、UnaliasTable 和 GetTable 等接口。 |
关闭客户端
功能介绍
关闭客户端。MochowClient 关闭后不再接受新的请求,继续调用接口会返回 NotConnected。可以通过 IsClosed() 判断当前客户端是否已关闭。
请求示例
C++
1#include <iostream>
2#include <memory>
3#include <string>
4
5#include "mochow/Mochow.h"
6
7int main() {
8 const std::string account = "root";
9 const std::string api_key = "$您的账户API密钥";
10 const std::string endpoint = "$您的实例访问端点"; // 例如:http://127.0.0.1:5287
11
12 mochow::ClientOptions options;
13 options.endpoint = endpoint;
14 options.credentials.account = account;
15 options.credentials.api_key = api_key;
16
17 // create mochow client
18 auto client_result = mochow::MochowClient::Create(options);
19 if (!client_result.IsOk()) {
20 std::cerr << "Fail to init mochow client due to error: "
21 << client_result.GetStatus().Message() << std::endl;
22 return 1;
23 }
24 std::shared_ptr<mochow::MochowClient> client = client_result.MoveValue();
25
26 // close client
27 mochow::Status status = client->Close();
28 if (!status.IsOk()) {
29 std::cerr << "Fail to close mochow client due to error: "
30 << status.Message() << std::endl;
31 return 1;
32 }
33 std::cout << "client closed: " << std::boolalpha << client->IsClosed()
34 << std::endl;
35 return 0;
36}
返回参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| status | mochow::Status | 本次调用的结果。 |
| is_closed | bool | IsClosed() 的返回值,表示客户端当前是否已关闭。 |
评价此篇文章
