Google Cloud Spanner C++ Client  1.32.1
A C++ Client Library for Google Cloud Spanner
quickstart.cc
Go to the documentation of this file.
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "google/cloud/spanner/client.h"
16 #include <iostream>
17 #include <stdexcept>
18 
19 int main(int argc, char* argv[]) try {
20  if (argc != 4) {
21  std::cerr << "Usage: " << argv[0]
22  << " project-id instance-id database-id\n";
23  return 1;
24  }
25 
26  namespace spanner = ::google::cloud::spanner;
27  spanner::Client client(
28  spanner::MakeConnection(spanner::Database(argv[1], argv[2], argv[3])));
29 
30  auto rows =
31  client.ExecuteQuery(spanner::SqlStatement("SELECT 'Hello World'"));
32 
33  for (auto const& row : spanner::StreamOf<std::tuple<std::string>>(rows)) {
34  if (!row) throw std::runtime_error(row.status().message());
35  std::cout << std::get<0>(*row) << "\n";
36  }
37 
38  return 0;
39 } catch (std::exception const& ex) {
40  std::cerr << "Standard exception raised: " << ex.what() << "\n";
41  return 1;
42 }