I'm working on a project that requires access to a SQL database as well (as various actions that SQL Server cannot do on it's own). So I'm using C++ to connect using the following code:
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <sqlext.h>
#include <sqltypes.h>
#include <sql.h>
#include "database.h"
using namespace std;
int main(){
databaseConnect();
return 0;
}
void show_error(unsigned int handletype, const SQLHANDLE& handle){
SQLWCHAR sqlstate[1024];
SQLWCHAR message[1024];
if(SQL_SUCCESS == SQLGetDiagRec(handletype, handle, 1, sqlstate, NULL, message, 1024, NULL))
wcout<<L"Message: "<<message<<L"\nSQLSTATE: "<<sqlstate<<endl;
}
int databaseConnect(){
SQLHANDLE sqlstatementhandle;
SQLHANDLE sqlenvhandle;
SQLHANDLE sqlconnectionhandle;
if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlenvhandle))
goto FINISHED;
if(SQL_SUCCESS!=SQLSetEnvAttr(sqlenvhandle,SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0))
goto FINISHED;
if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_DBC, sqlenvhandle, &sqlconnectionhandle))
goto FINISHED;
SQLWCHAR retconstring[1024];
switch(SQLDriverConnect (sqlconnectionhandle,
NULL,
(SQLWCHAR*)TEXT("DRIVER={SQL Server};SERVER=serverName;DATABASE=dataName;UID=user;PWD=password;Trusted_Connection=no;"),
SQL_NTS,
retconstring,
1024,
NULL,
SQL_DRIVER_NOPROMPT)){
case SQL_SUCCESS_WITH_INFO:
show_error(SQL_HANDLE_DBC, sqlconnectionhandle);
break;
case SQL_INVALID_HANDLE:
case SQL_ERROR:
show_error(SQL_HANDLE_DBC, sqlconnectionhandle);
goto FINISHED;
default:
break;
}
if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_STMT, sqlconnectionhandle, &sqlstatementhandle))
goto FINISHED;
if(SQL_SUCCESS!=SQLExecDirect(sqlstatementhandle, (SQLWCHAR*)"select * from test", SQL_NTS)){
show_error(SQL_HANDLE_STMT, sqlstatementhandle);
goto FINISHED;
}
else{
char name[64];
char address[64];
int id;
while(SQLFetch(sqlstatementhandle)==SQL_SUCCESS){
SQLGetData(sqlstatementhandle, 1, SQL_C_ULONG, &id, 0, NULL);
SQLGetData(sqlstatementhandle, 2, SQL_C_CHAR, name, 64, NULL);
SQLGetData(sqlstatementhandle, 3, SQL_C_CHAR, address, 64, NULL);
cout<<id<<" "<<name<<" "<<address<<endl;
}
}
FINISHED:
SQLFreeHandle(SQL_HANDLE_STMT, sqlstatementhandle );
SQLDisconnect(sqlconnectionhandle);
SQLFreeHandle(SQL_HANDLE_DBC, sqlconnectionhandle);
SQLFreeHandle(SQL_HANDLE_ENV, sqlenvhandle);
return 0;
}
I finally managed to connect to the server and resolve the login issues, but now i'm getting this syntax error:
Message: [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '0x2a20'.
SQLSTATE: 42000
How can I correct this error?
I am very rusty on C++, not having had my classes in years and not having a job that I needed to use it in, and have all of a sudden had this project pushed on me at work since I'm the only one in my small company with any programming experience. Any help would be useful.
As a secondary question:
The final project will connect to a database, search for entries added or modified since the last time the application ran, copy the data into a .txt file, and then access a directory on a harddrive, scan the image files in the directory for files added or modified since the last time the application ran, generate a new directory, then copy the new/modified images and the .txt file into the new directory. And all this will be automated. Is there an easier way to do this than C++ going through SQL?