acl/lib_acl_cpp/samples/mysql2/mysql.cpp

101 lines
2.2 KiB
C++
Raw Normal View History

// mysql.cpp : <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̨Ӧ<CCA8>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ㡣
2014-11-19 00:25:21 +08:00
//
#include "stdafx.h"
// <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
static int tbl_select(acl::db_handle& db)
{
const char* sql = "select value, category, type from black_white_list";
if (db.sql_select(sql) == false)
{
printf("select sql error\r\n");
return (-1);
}
printf("\r\n---------------------------------------------------\r\n");
const acl::db_rows* result = db.get_result();
if (result)
{
const std::vector<acl::db_row*>& rows = result->get_rows();
for (size_t i = 0; i < rows.size(); i++)
{
const acl::db_row* row = rows[i];
for (size_t j = 0; j < row->length(); j++)
printf("%s, ", (*row)[j]);
printf("\r\n");
}
}
int ret = (int) db.length();
// <20>ͷŲ<CDB7>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
db.free_result();
return (ret);
}
int main(void)
{
// WIN32 <20><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>ô˺<C3B4><CBBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD> SOCKET <20>ij<EFBFBD>ʼ<EFBFBD><CABC>
acl::acl_cpp_init();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ
acl::log::stdout_open(true);
acl::string line;
acl::stdin_stream in;
acl::stdout_stream out;
#if defined(_WIN32) || defined(_WIN64)
const char* libname = "libmysql.dll";
#else
const char* libname = "libmysqlclient_r.so";
#endif
acl::string path;
// <20><>Ϊ<EFBFBD><CEAA><EFBFBD>ö<EFBFBD>̬<EFBFBD><CCAC><EFBFBD>صķ<D8B5>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҪӦ<D2AA>ø<EFBFBD><C3B8><EFBFBD> mysql <20>ͻ<EFBFBD><CDBB>˿<EFBFBD><CBBF><EFBFBD><EFBFBD>ڵ<EFBFBD>·<EFBFBD><C2B7>
out.format("Enter %s load path: ", libname);
if (in.gets(line) && !line.empty())
#if defined(_WIN32) || defined(_WIN64)
path.format("%s\\%s", line.c_str(), libname);
#else
path.format("%s/%s", line.c_str(), libname);
#endif
else
path = libname;
out.format("%s path: %s\r\n", libname, path.c_str());
// <20><><EFBFBD>ö<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>ȫ·<C8AB><C2B7>
acl::db_handle::set_loadpath(path);
2014-11-19 00:25:21 +08:00
const char* dbaddr = "127.0.0.1:16811";
const char* dbname = "inc365_antispam_db";
const char* dbuser = "root", *dbpass = "";
acl::db_mysql db(dbaddr, dbname, dbuser, dbpass);
if (db.dbopen(NULL) == false)
2014-11-19 00:25:21 +08:00
{
printf("open db(%s) error\r\n", dbname);
getchar();
return 1;
}
printf("open db %s ok\r\n", dbname);
int ret = tbl_select(db);
if (ret >= 0)
{
printf(">>select ok\r\n");
}
else
printf(">>select error\r\n");
printf("mysqlclient lib's version: %ld, info: %s\r\n",
db.mysql_libversion(), db.mysql_client_info());
return 0;
}