MSDN.WhiteKnight - Stack Overflow answers
Ответ на "C++ Database get array of rows from a table using SQL ODBC"
Answer 66810732
You need to use SQLGetData, passing SQL_C_WCHAR type, for example, if your column is text. Here's example how to get values of the single text column and put them to vector:
#include <iostream> #include <string> #include <vector> #include <Windows.h> #include <sqlext.h> WCHAR szDSN[] = L"Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=C:\\Test\\users.accdb"; void DisplayError(SQLSMALLINT t, SQLHSTMT h) { SQLWCHAR SqlState[6], Msg[SQL_MAX_MESSAGE_LENGTH]; SQLINTEGER NativeError; SQLSMALLINT i, MsgLen; SQLRETURN rc; SQLLEN numRecs = 0; SQLGetDiagField(t, h, 0, SQL_DIAG_NUMBER, &numRecs, 0, 0); // Get the status records. i = 1; while (i <= numRecs && (rc = SQLGetDiagRec(t, h, i, SqlState, &NativeError, Msg, sizeof(Msg), &MsgLen)) != SQL_NO_DATA) { wprintf(L"Error %d: %s\n", NativeError, Msg); i++; } } std::vector<std::wstring> ExecuteSql(const WCHAR* sql) { std::vector<std::wstring> results = {}; HENV hEnv = NULL; HDBC hDbc = NULL; HSTMT hStmt = NULL; int iConnStrLength2Ptr; WCHAR szConnStrOut[256]; SQLINTEGER rowCount = 0; SQLSMALLINT fieldCount = 0; SQLWCHAR buf[128]; SQLINTEGER ret; /* ODBC API return status */ RETCODE rc; /* Allocate an environment handle */ rc = SQLAllocEnv(&hEnv); /* Allocate a connection handle */ rc = SQLAllocConnect(hEnv, &hDbc); /* Connect to the database */ rc = SQLDriverConnect(hDbc, NULL, (WCHAR*)szDSN, SQL_NTS, (WCHAR*)szConnStrOut, 255, (SQLSMALLINT*)&iConnStrLength2Ptr, SQL_DRIVER_NOPROMPT); if (SQL_SUCCEEDED(rc)) { /* Prepare SQL query */ rc = SQLAllocStmt(hDbc, &hStmt); rc = SQLPrepare(hStmt, (SQLWCHAR*)sql, SQL_NTS); /* Excecute the query */ rc = SQLExecute(hStmt); if (SQL_SUCCEEDED(rc)) { SQLNumResultCols(hStmt, &fieldCount); if (fieldCount > 0) { /* Loop through the rows in the result set */ rc = SQLFetch(hStmt); while (SQL_SUCCEEDED(rc)) { //get data rc = SQLGetData(hStmt, 1, SQL_C_WCHAR, buf, sizeof(buf), &ret); if (SQL_SUCCEEDED(rc) == FALSE) { wprintf(L"SQLGetData failed\n"); continue; } //convert data to string std::wstring str; if (ret <= 0) { str = std::wstring(L"(null"); } else { str = std::wstring(buf); } results.push_back(str); rc = SQLFetch(hStmt); rowCount++; }; rc = SQLFreeStmt(hStmt, SQL_DROP); } else { wprintf(L"Error: Number of fields in the result set is 0.\n"); } } else { wprintf(L"SQL Failed\n"); DisplayError(SQL_HANDLE_STMT, hStmt); } } else { wprintf(L"Couldn't connect to %s\n", szDSN); DisplayError(SQL_HANDLE_DBC, hDbc); } /* Disconnect and free up allocated handles */ SQLDisconnect(hDbc); SQLFreeHandle(SQL_HANDLE_DBC, hDbc); SQLFreeHandle(SQL_HANDLE_ENV, hEnv); return results; } int main() { auto results = ExecuteSql(L"SELECT name FROM Users"); for (auto x : results) { std::wcout << x << std::endl; } getchar(); return 0; }
Content is retrieved from StackExchange API.
Auto-generated by ruso-archive tools.