acl/lib_acl_cpp/samples/db/query/main.cpp

86 lines
2.3 KiB
C++
Raw Normal View History

#include <stdio.h>
2014-11-19 00:25:21 +08:00
#include <stdlib.h>
#include <time.h>
#include "acl_cpp/db/query.hpp"
#ifdef WIN32
# ifndef snprintf
# define snprintf _snprintf
# endif
#endif
int main(void)
{
acl::query query;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѯ<EFBFBD><D1AF>ʽ<EFBFBD><CABD> SQL <20><>ѯ<EFBFBD><D1AF>
2014-11-19 00:25:21 +08:00
query.create_sql("select * from person where name = :name"
" and age >= :age and male = 1 and nick = :name");
query.set_parameter("name", "'zsxxsz'\\");
query.set_parameter("age", 30);
printf("sql: %s\r\n", query.to_string().c_str());
// <20>ظ<EFBFBD>ʹ<EFBFBD>øò<C3B8>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>Ҫ<EFBFBD><D2AA><EFBFBD>øò<C3B8>ѯ<EFBFBD><D1AF>״̬
2014-11-19 00:25:21 +08:00
query.reset();
int age = 20;
query.create_sql("update table set name=:name, age=%d"
", home=:home where nick=:name and price=:price", age)
2014-11-19 00:25:21 +08:00
.set_parameter("name", "zsx1&xsz1")
.set_parameter("home", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")
.set_parameter("price", 1.00212, 6);
2014-11-19 00:25:21 +08:00
printf("sql: %s\r\n", query.to_string().c_str());
query.reset();
query.create_sql("insert into table(name, age, home, nick, date, time)"
" values(:name, %d, :home, :nick, :date, :time)", age)
.set_parameter("name", "zsx'xsz")
.set_parameter("home", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>.<2E><>ƽ.<2E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")
.set_parameter("nick", "'<27><>ң<EFBFBD><D2A3>'")
2014-11-19 00:25:21 +08:00
.set_date("date", time(NULL), "%Y-%m-%d")
.set_date("time", time(NULL));
printf("sql: %s\r\n", query.to_string().c_str());
query.reset();
query.create_sql("select * from table where name=:name")
.set_format("name", "zsx\"%s-%d", "xsz", 1);
printf("sql: %s\r\n", query.to_string().c_str());
/////////////////////////////////////////////////////////////////////
time_t now = time(NULL);
acl::string buf(128);
if (acl::query::to_date(now, buf) == NULL)
printf("to_date_time error, now: %ld\r\n", (long) now);
else
printf("to_date_time ok, now: %ld, %s\r\n",
(long) now, buf.c_str());
const char* date_fmt = "%Y-%m-%d";
buf.clear();
if (acl::query::to_date(now, buf, date_fmt) == NULL)
printf("to_date_time error, now: %ld\r\n", (long) now);
else
printf("to_date_time ok, now: %ld, %s\r\n",
(long) now, buf.c_str());
date_fmt = "%Y-%m";
buf.clear();
if (acl::query::to_date(now, buf, date_fmt) == NULL)
printf("to_date_time error, now: %ld, fmt: %s\r\n",
(long) now, date_fmt);
else
printf("to_date_time ok, now: %ld, %s\r\n",
(long) now, buf.c_str());
#ifdef WIN32
printf("enter any key to exit\r\n");
getchar();
#endif
return 0;
}