Skip to content

Commit

Permalink
修复MySQLStmt bindTime问题
Browse files Browse the repository at this point in the history
Signed-off-by: sylar-yin <564628276@qq.com>
  • Loading branch information
sylar-yin committed Aug 20, 2019
1 parent be8e331 commit 79afa8b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 40 deletions.
15 changes: 8 additions & 7 deletions sylar/db/mysql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -474,13 +474,14 @@ int MySQLStmt::bindBlob(int idx, const std::string& value) {
//}

int MySQLStmt::bindTime(int idx, const time_t& value) {
idx -= 1;
m_binds[idx].buffer_type = MYSQL_TYPE_TIMESTAMP;
MYSQL_TIME* mt = (MYSQL_TIME*)malloc(sizeof(MYSQL_TIME));
time_t_to_mysql_time(value, *mt);
m_binds[idx].buffer = mt;
m_binds[idx].buffer_length = sizeof(MYSQL_TIME);
return 0;
//idx -= 1;
//m_binds[idx].buffer_type = MYSQL_TYPE_TIMESTAMP;
//MYSQL_TIME* mt = (MYSQL_TIME*)malloc(sizeof(MYSQL_TIME));
//time_t_to_mysql_time(value, *mt);
//m_binds[idx].buffer = mt;
//m_binds[idx].buffer_length = sizeof(MYSQL_TIME);
//return 0;
return bindString(idx, sylar::Time2Str(value));
}

int MySQLStmt::execute() {
Expand Down
2 changes: 1 addition & 1 deletion sylar/http/http_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ int HttpConnection::sendRequest(HttpRequest::ptr rsp) {
std::stringstream ss;
ss << *rsp;
std::string data = ss.str();
std::cout << ss.str() << std::endl;
//std::cout << ss.str() << std::endl;
return writeFixSize(data.c_str(), data.size());
}

Expand Down
69 changes: 37 additions & 32 deletions tests/test_mysql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ void run() {
std::map<std::string, std::string> params;
params["host"] = "127.0.0.1";
params["user"] = "sylar";
params["passwd"] = "123456";
params["dbname"] = "sylar";
params["passwd"] = "blog123";
params["dbname"] = "blog";

sylar::MySQL::ptr mysql(new sylar::MySQL(params));
if(!mysql->connect()) {
Expand All @@ -25,39 +25,44 @@ void run() {
//b.buffer = &a;
//mysql_stmt_bind_param(m_

MYSQL_TIME mt;
sylar::time_t_to_mysql_time(time(0), mt);
sylar::MySQLStmt::ptr stmt = sylar::MySQLStmt::Create(mysql, "update user set update_time = ? where id = 1");
stmt->bindString(1, "2018-01-01 10:10:10");
int rt = stmt->execute();
std::cout << "rt=" << rt << std::endl;

int a = 0;
//auto stmt = mysql->prepare("select * from sylar where status >= ?");
//stmt->bind(0, a);
//auto res = std::dynamic_pointer_cast<sylar::MySQLStmtRes>(stmt->query());
//MYSQL_TIME mt;
//sylar::time_t_to_mysql_time(time(0), mt);

auto res = std::dynamic_pointer_cast<sylar::MySQLStmtRes>
//(mysql->queryStmt("select * from sylar"));
(mysql->queryStmt("select *, 'hello' as xx from user where status >= ? and status <= ?"
, a, a));
//(mysql->queryStmt("select id,name, keyword, creator as aa, last_update_time from sylar "
// " where last_update_time > ?", (time_t)0));
//auto res = std::dynamic_pointer_cast<sylar::MySQLRes>
// (mysql->query("select * from search_brand"));
if(!res) {
std::cout << "invalid" << std::endl;
return;
}
if(res->getErrno()) {
std::cout << "errno=" << res->getErrno()
<< " errstr=" << res->getErrStr() << std::endl;
return;
}
//int a = 0;
////auto stmt = mysql->prepare("select * from sylar where status >= ?");
////stmt->bind(0, a);
////auto res = std::dynamic_pointer_cast<sylar::MySQLStmtRes>(stmt->query());

int i = 0;
while(res->next()) {
++i;
std::cout << res->getInt64(0)
<< " - " << res->getInt64(1) << std::endl;
}
std::cout << "===" << i << std::endl;
//auto res = std::dynamic_pointer_cast<sylar::MySQLStmtRes>
// //(mysql->queryStmt("select * from sylar"));
// (mysql->queryStmt("select *, 'hello' as xx from user where status >= ? and status <= ?"
// , a, a));
// //(mysql->queryStmt("select id,name, keyword, creator as aa, last_update_time from sylar "
// // " where last_update_time > ?", (time_t)0));
////auto res = std::dynamic_pointer_cast<sylar::MySQLRes>
//// (mysql->query("select * from search_brand"));
//if(!res) {
// std::cout << "invalid" << std::endl;
// return;
//}
//if(res->getErrno()) {
// std::cout << "errno=" << res->getErrno()
// << " errstr=" << res->getErrStr() << std::endl;
// return;
//}

//int i = 0;
//while(res->next()) {
// ++i;
// std::cout << res->getInt64(0)
// << " - " << res->getInt64(1) << std::endl;
//}
//std::cout << "===" << i << std::endl;
} while(false);
std::cout << "over" << std::endl;
}
Expand Down

0 comments on commit 79afa8b

Please sign in to comment.