File: example1.cpp

package info (click to toggle)
cppdb 0.3.1+dfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 668 kB
  • sloc: cpp: 7,373; sh: 133; ansic: 72; makefile: 7
file content (85 lines) | stat: -rw-r--r-- 2,337 bytes parent folder | download | duplicates (3)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
///////////////////////////////////////////////////////////////////////////////
//                                                                             
//  Copyright (C) 2010-2011  Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>     
//                                                                             
//  Distributed under:
//
//                   the Boost Software License, Version 1.0.
//              (See accompanying file LICENSE_1_0.txt or copy at 
//                     http://www.boost.org/LICENSE_1_0.txt)
//
//  or (at your opinion) under:
//
//                               The MIT License
//                 (See accompanying file MIT.txt or a copy at
//              http://www.opensource.org/licenses/mit-license.php)
//
///////////////////////////////////////////////////////////////////////////////
#include <cppdb/frontend.h>
#include <iostream>
#include <ctime>


int main()
{
	try {
		cppdb::session sql("sqlite3:db=db.db");
		
		sql << "DROP TABLE IF EXISTS test" << cppdb::exec;

		sql<<	"CREATE TABLE test ( "
			"   id   INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
			"   n    INTEGER,"
			"   f    REAL, "
			"   t    TIMESTAMP,"
			"   name TEXT "
			")  " << cppdb::exec;
		
		std::time_t now_time = std::time(0);
		
		std::tm now = *std::localtime(&now_time);

		cppdb::statement stat;
		
		stat = sql << 
			"INSERT INTO test(n,f,t,name) "
			"VALUES(?,?,?,?)"
			<< 10 << 3.1415926565 << now <<"Hello 'World'";

		stat.exec();
		std::cout<<"ID: "<<stat.last_insert_id() << std::endl;
		std::cout<<"Affected rows "<<stat.affected()<<std::endl;
		
		stat.reset();

		stat.bind(20);
		stat.bind_null();
		stat.bind(now);
		stat.bind("Hello 'World'");
		stat.exec();


		cppdb::result res = sql << "SELECT id,n,f,t,name FROM test";

		while(res.next()) {
			double f=-1;
			int id,k;
			std::tm atime;
			std::string name;
			res >> id >> k >> f >> atime >> name;
			std::cout <<id << ' '<<k <<' '<<f<<' '<<name<<' '<<asctime(&atime)<< std::endl;
		}

		res = sql << "SELECT n,f FROM test WHERE id=?" << 1 << cppdb::row;
		if(!res.empty()) {
			int n = res.get<int>("n");
			double f=res.get<double>(1);
			std::cout << "The values are " << n <<" " << f << std::endl;
		}
	}
	catch(std::exception const &e) {
		std::cerr << "ERROR: " << e.what() << std::endl;
		return 1;
	}
	return 0;
}