I am trying to connect MS access database in C++ using C-Free 5.0 compiler. I found the following code from internet but it is giving me an error as:
[Error] C:\Users\rubeea\Documents\C-Free\Temp\db.cpp:25: error: invalid conversion from const char*' tounsigned char*'
Here's the code:
#include<windows.h>
#include<sqlext.h>
#include<stdio.h>
#include <sql.h>
int main(void){
char szDSN[256] = "Driver=std;DSN='';DBQ=E:\\student.mdb;";
/* Data Access Method used in this sample */
const char* DAM = "Direct ODBC";
HENV hEnv;
HDBC hDbc;
/* ODBC API return status */
RETCODE rc;
int iConnStrLength2Ptr;
char szConnStrOut[256];
unsigned char* query ="SELECT faculty.[name], faculty.[remarks] FROM faculty;";
SQLCHAR chval1[128], chval2[128], colName[128];
int ret1;
int ret2;
/* Number of rows and columns in result set */
SQLINTEGER rowCount = 0;
SQLSMALLINT fieldCount = 0, currentField = 0;
HSTMT hStmt;
/* Allocate an environment handle */
rc = SQLAllocEnv(&hEnv);
/* Allocate a connection handle */
rc = SQLAllocConnect(hEnv, &hDbc);
/* Connect to the 'student.accdb' database */
rc = SQLDriverConnect(hDbc, NULL, (unsigned char*)szDSN,
SQL_NTS, (unsigned char*)szConnStrOut,
255, (SQLSMALLINT*)&iConnStrLength2Ptr, SQL_DRIVER_NOPROMPT);
if (SQL_SUCCEEDED(rc))
{
printf("%s: Successfully connected to database. Data source name: \n %s\n",
DAM, szConnStrOut);
/* Prepare SQL query */
printf("%s: SQL query:\n %s\n", DAM, query);
rc = SQLAllocStmt(hDbc,&hStmt); rc = SQLPrepare(hStmt, query, SQL_NTS);
/* Bind result set columns to the local buffers */
rc = SQLBindCol(hStmt, 1, SQL_C_CHAR, chval1, 128, (SQLINTEGER*)&ret1);
rc = SQLBindCol(hStmt, 2, SQL_C_CHAR, chval2, 128, (SQLINTEGER*)&ret2);
/* Execute the query and create a record set */
rc = SQLExecute(hStmt);
if (SQL_SUCCEEDED(rc))
{
printf("%s: Retrieve schema info for the given result set:\n", DAM);
SQLNumResultCols(hStmt, &fieldCount);
if (fieldCount > 0)
{
for (currentField=1; currentField <= fieldCount; currentField++)
{
SQLDescribeCol(hStmt, currentField,
colName, sizeof(colName), 0, 0, 0, 0, 0);
printf(" | %s", colName);
}
printf("\n");
}
else
{
printf("%s: Error: Number of fields in the result set is 0.\n", DAM);
}
printf("%s: Fetch the actual data:\n", DAM);
/* Loop through the rows in the result set */
rc = SQLFetch(hStmt);
while (SQL_SUCCEEDED(rc))
{
printf(" | %s | %s\n", chval1, chval2);
rc = SQLFetch(hStmt);
rowCount++;
};
printf("%s: Total Row Count: %d\n", DAM, rowCount);
rc = SQLFreeStmt(hStmt, SQL_DROP);
}
}
else
{`enter code here`
printf("%s: Couldn't connect to %s.\n", DAM, szDSN);
}
/* Disconnect and free up allocated handles */
SQLDisconnect(hDbc);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
printf("%s: Cleanup. Done.\n", DAM);
return 0;
}
Related
connection to access is normal, the SQL query is executed, I get a SQL Success response. and I can't do it anymore. how to get data from sql query
int bdacess(int argc, char* argv[])
{
/* 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))
{
wprintf(L"Successfully connected to database. Data source name: \n %s\n", szConnStrOut);
/* Prepare SQL query */
wprintf(L"SQL query:\n %s\n", query);
rc = SQLAllocStmt(hDbc, &hStmt);
rc = SQLPrepare(hStmt, query, SQL_NTS);
/* Excecute the query */
rc = SQLExecute(hStmt);
if (SQL_SUCCEEDED(rc))
{
wprintf(L"SQL Success\n");
}
else {
wprintf(L"SQL Failed\n");
}
}
else
{
wprintf(L"Couldn't connect to %s.\n", szDSN);
}
/* Disconnect and free up allocated handles */
SQLDisconnect(hDbc);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
getchar();
return 0;
}
I can't get the result of sql execution itself
I'm trying to update column of type uniqueidentifier with null. My query looks like:
UPDATE table_name SET column_name = ?
The column is bound with:
SQLLEN _nullLen(SQL_NULL_DATA);
_rc = SQLBindParameter(_hstmt,
static_cast<SQLUSMALLINT>(1),
SQL_PARAM_INPUT,
SQL_C_CHAR,
SQL_VARCHAR,
37,
NULL,
NULL,
0,
&_nullLen);
Executing the query results in a ODBC error 'String data, right truncation'.
Using the exact same SQLBindParameter I'm able to successfuly insert a new row with null data. Why does this not work for updating the row?
Please read
https://learn.microsoft.com/en-us/sql/odbc/reference/syntax/sqlbindparameter-function?view=sql-server-ver15 thoroughly.
According to it, the 6th parameter to SQLBindParameter is ColumnSize, which you set to 37. Why this value?
The 8th parameter is ParameterValuePtr, but you set it to NULL. Is NULL the value you are trying to set?
The 10th parameter is StrLen_or_IndPtr which you set to &_nullLen where SQLLEN _nullLen(SQL_NULL_DATA), but that's not the kind of thing it should point to.
Please make sure you understand each parameter passed to SQLBindParameter().
I suspect you update more than one column and that the truncation is not on the UNIQUEIDENTIFIER column but rather on something else.
I fired up my old VS and coded following sample program, and it updated to NULL just fine. You might wanna put a trace to see what was actually executed.
#include "stdafx.h"
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <stdlib.h>
#include <sal.h>
#define TRYODBC(h, ht, x) { RETCODE rc = x;\
if (rc != SQL_SUCCESS) \
{ \
HandleDiagnosticRecord(h, ht, rc); \
} \
if (rc == SQL_ERROR) \
{ \
fwprintf(stderr, L"Error in " L#x L"\n"); \
goto Exit; \
} \
}
void HandleDiagnosticRecord(SQLHANDLE hHandle,
SQLSMALLINT hType,
RETCODE RetCode);
int __cdecl wmain(int argc, _In_reads_(argc) WCHAR **argv)
{
SQLHENV hEnv = NULL;
SQLHDBC hDbc = NULL;
SQLHSTMT hStmt = NULL;
WCHAR* pwszConnStr;
// Allocate an environment
if (SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv) == SQL_ERROR)
{
fwprintf(stderr, L"Unable to allocate an environment handle\n");
exit(-1);
}
TRYODBC(hEnv,
SQL_HANDLE_ENV,
SQLSetEnvAttr(hEnv,
SQL_ATTR_ODBC_VERSION,
(SQLPOINTER)SQL_OV_ODBC3,
0));
// Allocate a connection
TRYODBC(hEnv,
SQL_HANDLE_ENV,
SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc));
pwszConnStr = L"";
TRYODBC(hDbc,
SQL_HANDLE_DBC,
SQLDriverConnect(hDbc,
GetDesktopWindow(),
pwszConnStr,
SQL_NTS,
NULL,
0,
NULL,
SQL_DRIVER_COMPLETE));
fwprintf(stderr, L"Connected!\n");
TRYODBC(hDbc,
SQL_HANDLE_DBC,
SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStmt));
RETCODE RetCode = NULL;
SQLSMALLINT sNumResults;
//Here be dragons
SQLLEN _nullLen(SQL_NULL_DATA);
SQLRETURN _retcode = SQLBindParameter(hStmt, 1,
SQL_PARAM_INPUT,
SQL_C_CHAR,
SQL_VARCHAR,
37,
NULL,
NULL,
0,
&_nullLen);
if (_retcode == -1)
{
HandleDiagnosticRecord(hStmt, SQL_HANDLE_STMT, _retcode);
return 1;
}
_retcode = SQLPrepare(hStmt, L"UPDATE zz SET v = ? ", SQL_NTS);
if (_retcode == -1)
{
HandleDiagnosticRecord(hStmt, SQL_HANDLE_STMT, _retcode);
return 1;
}
RetCode= SQLExecute(hStmt);
switch (RetCode)
{
case SQL_SUCCESS_WITH_INFO:
{
HandleDiagnosticRecord(hStmt, SQL_HANDLE_STMT, RetCode);
// fall through
}
case SQL_SUCCESS:
{
// If this is a row-returning query, display
// results
TRYODBC(hStmt,
SQL_HANDLE_STMT,
SQLNumResultCols(hStmt, &sNumResults));
{
SQLLEN cRowCount;
TRYODBC(hStmt,
SQL_HANDLE_STMT,
SQLRowCount(hStmt, &cRowCount));
if (cRowCount >= 0)
{
wprintf(L"%Id %s affected\n",
cRowCount,
cRowCount == 1 ? L"row" : L"rows");
}
}
break;
}
case SQL_ERROR:
{
HandleDiagnosticRecord(hStmt, SQL_HANDLE_STMT, RetCode);
break;
}
default:
fwprintf(stderr, L"Unexpected return code %hd!\n", RetCode);
}
TRYODBC(hStmt,
SQL_HANDLE_STMT,
SQLFreeStmt(hStmt, SQL_CLOSE));
wprintf(L"Thanks for playing, type Enter to exit");
getchar();
Exit:
// Free ODBC handles and exit
if (hStmt)
{
SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
}
if (hDbc)
{
SQLDisconnect(hDbc);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
}
if (hEnv)
{
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
}
wprintf(L"\nDisconnected.");
return 0;
}
void HandleDiagnosticRecord(SQLHANDLE hHandle,
SQLSMALLINT hType,
RETCODE RetCode)
{
SQLSMALLINT iRec = 0;
SQLINTEGER iError;
WCHAR wszMessage[1000];
WCHAR wszState[SQL_SQLSTATE_SIZE + 1];
if (RetCode == SQL_INVALID_HANDLE)
{
fwprintf(stderr, L"Invalid handle!\n");
return;
}
while (SQLGetDiagRec(hType,
hHandle,
++iRec,
wszState,
&iError,
wszMessage,
(SQLSMALLINT)(sizeof(wszMessage) / sizeof(WCHAR)),
(SQLSMALLINT *)NULL) == SQL_SUCCESS)
{
// Hide data truncated..
if (wcsncmp(wszState, L"01004", 5))
{
fwprintf(stderr, L"[%5.5s] %s (%d)\n", wszState, wszMessage, iError);
}
}
}
I have a school project where I need to develop an application that queries and writes into Access database files, but using C++.
After some research I found about ODBC, and that it could help me, but I had no luck. I've tried differents connection strings, but nothing seems to work.
What I've done so far:
#include "pch.h"
#include <windows.h>
#include <sqlext.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char szDSN[256] = "Driver={Microsoft Access Driver (*.mdb,
*.accdb)};DBQ=C:\\EntrySystem.mdb";
/* Data Access Method used in this sample */
const char* DAM = "Direct ODBC";
HENV hEnv;
HDBC hDbc;
/* ODBC API return status */
RETCODE rc;
int iConnStrLength2Ptr;
char szConnStrOut[256];
unsigned char query[] = "SELECT * from Condomino;";
SQLCHAR chval1[128], chval2[128], colName[128];
int ret1;
int ret2;
/* Number of rows and columns in result set */
SQLINTEGER rowCount = 0;
SQLSMALLINT fieldCount = 0, currentField = 0;
HSTMT hStmt;
/* Allocate an environment handle */
rc = SQLAllocEnv(&hEnv);
/* Allocate a connection handle */
rc = SQLAllocConnect(hEnv, &hDbc);
/* Connect to the 'Northwind 2007.accdb' database */
rc = SQLDriverConnect(hDbc, NULL, (SQLWCHAR*)szDSN,
SQL_NTS, (SQLWCHAR*)szConnStrOut,
255, (SQLSMALLINT*)&iConnStrLength2Ptr, SQL_DRIVER_NOPROMPT);
if (SQL_SUCCEEDED(rc))
{
printf("%s: Successfully connected to database. Data source name: \n %s\n",
DAM, szConnStrOut);
/* Prepare SQL query */
printf("%s: SQL query:\n %s\n", DAM, query);
rc = SQLAllocStmt(hDbc, &hStmt);
rc = SQLPrepare(hStmt, (SQLWCHAR*)query, SQL_NTS);
/* Bind result set columns to the local buffers */
rc = SQLBindCol(hStmt, 1, SQL_C_CHAR, chval1, 128, (SQLINTEGER*)&ret1);
rc = SQLBindCol(hStmt, 2, SQL_C_CHAR, chval2, 128, (SQLINTEGER*)&ret2);
/* Excecute the query and create a record set */
rc = SQLExecute(hStmt);
if (SQL_SUCCEEDED(rc))
{
printf("%s: Retrieve schema info for the given result set:\n", DAM);
SQLNumResultCols(hStmt, &fieldCount);
if (fieldCount > 0)
{
for (currentField = 1; currentField <= fieldCount; currentField++)
{
SQLDescribeCol(hStmt, currentField,
(SQLWCHAR*)colName, sizeof(colName), 0, 0, 0, 0, 0);
printf(" | %s", colName);
}
printf("\n");
}
else
{
printf("%s: Error: Number of fields in the result set is 0.\n", DAM);
}
printf("%s: Fetch the actual data:\n", DAM);
/* Loop through the rows in the result set */
rc = SQLFetch(hStmt);
while (SQL_SUCCEEDED(rc))
{
printf(" | %s | %s\n", chval1, chval2);
rc = SQLFetch(hStmt);
rowCount++;
};
printf("%s: Total Row Count: %d\n", DAM, rowCount);
rc = SQLFreeStmt(hStmt, SQL_DROP);
}
}
else
{
printf("%s: Couldn't connect to %s.\nLastError: %d\n", DAM, szDSN, GetLastError());
}
/* Disconnect and free up allocated handles */
SQLDisconnect(hDbc);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
printf("%s: Cleanup. Done.\n", DAM);
return 0;
}
I expect it to query all rows from my table "condomino", but the library (sqlext) keep giving me the error "0". Any help would be welcome, if you any other solutions, let me know.
Yes.
Seems like you are having issue with Access connection strings. An alternative is to create User DNS ; go to control panel / administrative tools / ODBC Data Source / choose Microsoft Access Database - configure and set your path to your *.mdb ( use of *.accdb recommended)
Now your connection string will be simplified as ( driver name is case sensitive ):
SQLWCHAR outstr[1024];
SQLSMALLINT outstrlen;
SQLReturnCode = SQLDriverConnect(hDatabase, NULL, L"DSN=Microsoft Access Driver;", SQL_NTS,
outstr, sizeof(outstr), &outstrlen, SQL_DRIVER_COMPLETE);
How to make this to read from custom string the information
I would like to read Host,User,Password,Database from the custom string, how can i do that?
Also another problem here in code is that it show that is connected with ODBC , but it doesnt read anything from database
GetPrivateProfileStringA("SQL","Host","127.0.0.1",szServer2,sizeof(szServer2),SQL_PATH);
GetPrivateProfileStringA("SQL","User","sa",szUser,sizeof(szUser),SQL_PATH);
GetPrivateProfileStringA("SQL","Password","12345",szPassword,sizeof(szPassword),SQL_PATH);
GetPrivateProfileStringA("SQL","Database","DbName",szDatabase,sizeof(szDatabase),SQL_PATH);
BOOL SQLCONNECT::Connect()
{
SQLHENV env;
SQLHDBC dbc;
SQLHSTMT stmt;
SQLRETURN ret;
SQLSMALLINT columns;
int row = 0;
/* Allocate an environment handle */
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
/* We want ODBC 3 support */
SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
/* Allocate a connection handle */
SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
/* Connect to the DSN */
SQLDriverConnectW(dbc, NULL, L"DRIVER={SQL Server};SERVER=(local);DATABASE=DbName;UID=sa;PWD=password;", SQL_NTS, NULL, 0, NULL, SQL_DRIVER_COMPLETE);
/* Check for success */
if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt))
{
MessageBoxA(0, "Failed to connect to Database!", "Error", MB_OK);
}
std::cin.get();
return FALSE;
}
I am trying to insert an blob into a database .
For this I am using c++ and microsoft ODBC driver .
The app works fine with files that have less than 4000 bits .
When i am trying to insert a file that has more than 4000 bytes , meaning 3,9 KB it returns : ORA-01460: unimplemented or unreasonable conversion requested .
The database is oracle 11.2.0 .
I have searched the internat for a solution but find none .
How could i solve this problem ?
What can i do ?
The code for my insert function is :
try{
CString strSqlStat(szSqlStat);
if(IsConnectionDead())
{
if(!Reconnect())
return ERR_RECONNECT_FAILED;
}
CFileException exFile;
CFile sourceFile;
if(!sourceFile.Open(szFilePath, CFile::modeRead | CFile::shareDenyNone, &exFile))
return ERR_BLOB_READFILE;
int nrBytesToRead = (int)sourceFile.GetLength();
char* pData = new char[nrBytesToRead+1];
DWORD nrBytesRead;
if(!ReadFile((HANDLE)sourceFile.m_hFile,pData,nrBytesToRead, &nrBytesRead, NULL))
{
delete pData;
return ERR_BLOB_READFILE;
}
sourceFile.Close();
if(nrBytesRead == 0)//file empty
{
delete pData;
return 0;
}
//variables
SQLRETURN retCode;
SDWORD cbTextSize, lbytes;
lbytes = (SDWORD)nrBytesRead;
cbTextSize = SQL_LEN_DATA_AT_EXEC(lbytes);
PTR pParmID;
SDWORD cbBatch = nrBytesRead;
int rgbValue = 1;
// Bind the parameter marker.
retCode = retcode = SQLBindParameter(hstmt, // hstmt
1, // ipar
SQL_PARAM_INPUT, // fParamType
SQL_C_BINARY, // fCType
SQL_LONGVARBINARY, // FSqlType
lbytes, // cbColDef
0, // ibScale
&rgbValue, // rgbValue
0, // cbValueMax
&cbTextSize); // pcbValue
SqlError(hstmt,SQL_HANDLE_STMT,_T("WriteBlob"), _T("CTLSqlConnection"), _T("SQLBindParameter"));
if(retCode != SQL_SUCCESS)
{
delete pData;
if(!EndTransaction(FALSE))
return ERR_ENDTRANSACTION_FAILED;
else
return -3;
}
//SQLExec
retcode = retCode = SQLExecDirect(hstmt,(SQLTCHAR*)szSqlStat, SQL_NTS);
retcode = retCode = SQLParamData(hstmt, &pParmID);
SQLRETURN ret; //ADI fix all warnings - including this var that is unreferenced - delete it if you don't use it
SQLCHAR* SQLState; //ADI same here
SQLINTEGER NativeError;
SQLSMALLINT errmsglen;
SQLWCHAR errmsg[255];
SQLWCHAR errstate[50];
SQLGetDiagRec(SQL_HANDLE_STMT, hstmt, 1, (SQLWCHAR*)errstate, &NativeError, (SQLWCHAR*)errmsg, sizeof(errmsg), &errmsglen);
if(retCode == SQL_NEED_DATA)
{
// Put final batch.
SQLPutData(hstmt, pData, lbytes);
}
else
{
delete pData;
SqlError(hstmt,SQL_HANDLE_STMT,_T("WriteBlob"), _T("CTLSqlConnection"), _T("SQLExecDirect or SQLParamData"));
if(!EndTransaction(FALSE))
return ERR_ENDTRANSACTION_FAILED;
else
return -4;
}
delete pData;
// Make final SQLParamData call.
retcode = retCode = SQLParamData(hstmt, &pParmID);
/*SQLRETURN ret; //ADI fix all warnings - including this var that is unreferenced - delete it if you don't use it
SQLCHAR* SQLState; //ADI same here
SQLINTEGER NativeError;
SQLSMALLINT errmsglen;
SQLWCHAR errmsg[255];
SQLWCHAR errstate[50];
*/
SQLGetDiagRec(SQL_HANDLE_STMT, hstmt, 1, (SQLWCHAR*)errstate, &NativeError, (SQLWCHAR*)errmsg, sizeof(errmsg), &errmsglen);
if(SqlError(hstmt,SQL_HANDLE_STMT,_T("WriteBlob"), _T("CTLSqlConnection"), _T("The last SQLParamData")))
if(!EndTransaction(FALSE))
return ERR_ENDTRANSACTION_FAILED;
else
return -5;
retcode = SQLCloseCursor(hstmt);
if(!EndTransaction(TRUE))
return ERR_ENDTRANSACTION_FAILED;
}catch(...){
WRITEERRORINLOGFILE(_T("Error: An exception has been caught in WriteBlob."));
return -10;
}
return 0;
}
Waitin for your answers .
Thanks .