Mysql和c ++连接器返回奇怪的字符

时间:2012-03-29 23:30:25

标签: c++ mysql mysql-connector

我在尝试获取列的值时遇到问题,因为总是返回奇怪的字符。 数据是普通字符(a-z和A-Z),列的大小为15个字符。但是当我在Resultset类中使用getString函数时,它总是返回奇怪的字符(�^ Yt �^Yt�^)。 以下是我的数据库的基本连接示例:

#include <stdlib.h>
#include <iostream>

#include <mysql/driver.h>
#include <mysql/exception.h>
#include <mysql/resultset.h>
#include <mysql/statement.h>
#include <mysql/prepared_statement.h>

int main(){

    sql::Driver *driver;
    sql::Connection *con;
    sql::ResultSet *res;
    sql::PreparedStatement *pstmt;

    try{
        driver = get_driver_instance();
        con = driver->connect("tcp://10.2.3.100:3306", "user", "password");
        con->setSchema("user");        

        pstmt = con->prepareStatement("select * from tbluser where id=100000");
        res = pstmt->executeQuery();
        while (res->next())
            std::cout<<res->getString("password")<<"  "<<res->getString("username")   <<std::endl;
       delete res;
       delete pstmt;

       delete con;    
    }catch(sql::SQLException &e){
        std::cout<<e.what();
    }
}

以下是该查询的结果集:

+--------+----------+------------+
| id     | password | username   |
+--------+----------+------------+
| 100000 |   test   |   martin   | 
+--------+----------+------------+

0 个答案:

没有答案