Menu

[r1575]: / dbixx / trunk / test.cpp  Maximize  Restore  History

Download this file

78 lines (69 with data), 2.1 kB

 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
#include "dbixx.h"
#include <iostream>
using namespace dbixx;
using namespace std;
int main()
{
try {
session sql("sqlite3:dbname=test.db;sqlite3_dbdir=./");
//session sql("mysql:dbname='cppcms';username=root;password='root';host=127.0.0.1;port=3306");
//session sql("pgsql:dbname=cppcms;username=artik");
sql<<"drop table if exists test",
exec();
// try {
// sql<<"drop table test",
// exec();
// }
// catch(dbixx_error const &e) {
// }
//sql<<"create table test ( id integer primary key auto_increment not null,n integer, f real , t timestamp ,name text )",
/// exec();
sql<<"create table test ( id integer primary key autoincrement not null,n integer, f real , t timestamp ,name text )",
exec();
//sql<<"create table test ( id serial primary key not null ,n integer, f real , t timestamp ,name text )",
// exec();
std::tm t;
time_t tt;
tt=time(NULL);
t=*localtime(&tt);
cout<<asctime(&t);
int n;
sql<<"insert into test(n,f,t,name) values(?,?,?,?)",
10,3.1415926565,t,"Hello \'World\'",
exec();
//cout<<"ID:"<<sql.rowid()<<endl;
cout<<"ID:"<<sql.rowid("test_id_seq")<<", Affected rows"<<sql.affected()<<endl;
sql<<"insert into test(n,f,t,name) values(?,?,?,?)",
use(10,true),use(3.1415926565,true),use(t,true),use("Hello \'World\'",true),
exec();
//cout<<"ID:"<<sql.rowid()<<endl;
cout<<"ID:"<<sql.rowid("test_id_seq")<<", Affected rows"<<sql.affected()<<endl;
row r;
result res;
sql<<"select id,n,f,t,name from test limit 10",
res;
cout<<"Rows:"<<res.rows()<<endl;
cout<<"Cols:"<<res.cols()<<endl;
n=0;
while(res.next(r)){
double f=-1;
int id=-1,k=-1;
std::tm atime={0};
string name="nonset";
r >> id >> k >> f >> atime >> name;
cout <<r.get<int>(1) << ' '<<k <<' '<<f<<' '<<name<<' '<< asctime(&atime) << endl;
cout<<"has "<<r.cols()<<" columns\n";
n++;
}
sql<<"delete from test",
exec();
cout<<"Deleted "<<sql.affected()<<" rows\n";
return 0;
}
catch(dbixx_error const &e) {
std::cerr<<"Error:"<<e.what()<<" for query:" << e.query() << std::endl;
}
catch(exception const &e) {
std::cerr<<"Error:"<<e.what()<<std::endl;
}
}
MongoDB Logo MongoDB