'nanodbc get SQL type FLOAT as C++ std::string truncates characters
I'm using the nanodbc ODBC C++ library
to retrieve a row of data from a table that has a SQL FLOAT type in a column, that for that record was saved to the database as "1.4" the following program returns that value as std::string = "1"
how can I get the string as the value that was saved , "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;
}
}
output
Microsoft SQL Server
a : 1
b : foo
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
