File: connect.cpp

package info (click to toggle)
soci 4.1.2-2~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 17,968 kB
  • sloc: ansic: 169,887; cpp: 54,198; javascript: 12,258; ada: 1,973; sh: 36; makefile: 12; xml: 2
file content (52 lines) | stat: -rw-r--r-- 1,302 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// Copyright (C) 2021 Vadim Zeitlin
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
//
//

// This is the simplest possible example of using SOCI: just connect to the
// database using the provided command line argument.

#include <soci/soci.h>

#include <cstdlib>
#include <iostream>

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        std::cerr << "usage: " << argv[0] << " <connection-string>\n";
        return EXIT_FAILURE;
    }

    char const* const connectString = argv[1];

    try
    {
        soci::session sql(connectString);

        std::cout << "Successfully connected to \"" << connectString << "\", "
                  << "using \"" << sql.get_backend_name() << "\" backend.\n";

        return EXIT_SUCCESS;
    }
    catch (soci::soci_error const& e)
    {
        std::cerr << "Connection to \"" << connectString << "\" failed: "
                  << e.what() << "\n";
    }
    catch (std::runtime_error const& e)
    {
        std::cerr << "Unexpected standard exception occurred: "
                  << e.what() << "\n";
    }
    catch (...)
    {
        std::cerr << "Unexpected unknown exception occurred.\n";
    }

    return EXIT_FAILURE;
}