我正在使用 nanodbc ODBC C++ 库
要从列中具有 SQL FLOAT 类型的表中检索一行数据,该记录作为“1.4”保存到数据库中,以下程序将该值返回为 std::string = “1”
如何将字符串作为保存的值“1.4”?
#include <iostream>
#include <nanodbc/nanodbc.h>
using namespace std;
using namespace nanodbc;
void show(nanodbc::result& results);
int main(int, char*[])
{
try
{
auto const connection_string =
NANODBC_TEXT("Driver={SQL Server};Server=localhost;Database=test_db;");
connection conn(connection_string);
std::cout << conn.dbms_name() << std::endl;
execute(conn, "drop table if exists test_table;");
execute(conn, "create table test_table (a float, b varchar(10));");
execute(conn, "insert into test_table VALUES (1.4, 'foo');");
result result = execute(conn, NANODBC_TEXT("SELECT TOP 1 * FROM [test_table];"));
show(result);
}
catch (std::runtime_error const& e)
{
std::cerr << e.what() << std::endl;
}
}
void show(nanodbc::result& results)
{
const short columns = results.columns();
nanodbc::string const null_value = NANODBC_TEXT("NULL");
while (results.next())
{
for (short col = 0; col < columns; ++col)
{
cout << results.column_name(col) << " : ";
std::string value = results.get<nanodbc::string>(col, null_value);
cout << value << "\n";
}
cout << endl;
}
}
输出
Microsoft SQL Server
a : 1
b : foo