// "main.c" #include #include #include #define DSN "test" #define USERNAME "user" #define PASSWORD "pass" #define TABLENAME "myTable" #define COLUMNNAME "myColumn" #define lengthOfAllStrings 255 SQLHENV gEnvironmentHandle; SQLHDBC gConnectionHandle; Boolean SqlWorked (SQLRETURN sqlResult); //-------------------------------------------------------------------------------------------- int main(int argc, char* argv[]) { SQLRETURN sqlResult; SQLHENV env = NULL; SQLHDBC dbc = NULL; SQLHSTMT stmt = NULL; char theStmt[lengthOfAllStrings]; Boolean connected = false; unsigned long valueIntoServer = 4255080020; unsigned long valueFromServer; // before here make sure to: // -create database on MySQL server named "test" (define for DSN) // -add user named "user" with password "pass" (defines) and give them full privileges to the database // -have the database name the same as DSN // -set up MySQL driver to connect to server via ODBC with the info you just entered // Allocate environment handle sqlResult = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env); if ( SqlWorked(sqlResult) ) { // Set the ODBC version environment attribute. sqlResult = SQLSetEnvAttr (env, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0/*SQL_IS_UINTEGER*/); if ( SqlWorked(sqlResult) ) { gEnvironmentHandle = env; // Allocate connection handle sqlResult = SQLAllocHandle (SQL_HANDLE_DBC, gEnvironmentHandle, &dbc); if ( SqlWorked(sqlResult) ) { // Set connection attribute - Login Timeout sqlResult = SQLSetConnectAttr (dbc, SQL_ATTR_LOGIN_TIMEOUT, (SQLPOINTER)5, 0); if ( SqlWorked(sqlResult) ) { // Set connection attribute - Connection Timeout sqlResult = SQLSetConnectAttr (dbc, SQL_ATTR_CONNECTION_TIMEOUT, (SQLPOINTER)5, 0); if ( SqlWorked(sqlResult) ) { // Connect to the database sqlResult = SQLConnect (dbc, (SQLCHAR *)DSN, SQL_NTS, (SQLCHAR *)USERNAME, SQL_NTS, (SQLCHAR *)PASSWORD, SQL_NTS); if ( SqlWorked(sqlResult) ) { gConnectionHandle = dbc; connected = true; } } } } } } if (connected) { sqlResult = SQLAllocHandle (SQL_HANDLE_STMT, gConnectionHandle, &stmt); if ( SqlWorked(sqlResult) ) { if ( SqlWorked(sqlResult) ) { sprintf (theStmt, "USE %s", DSN); sqlResult = SQLExecDirect (stmt, (SQLCHAR *)theStmt, lengthOfAllStrings); SQLFreeStmt (stmt, SQL_CLOSE); } if ( SqlWorked(sqlResult) ) { sprintf (theStmt, "DROP TABLE IF EXISTS %s", TABLENAME); sqlResult = SQLExecDirect (stmt, (SQLCHAR *)theStmt, lengthOfAllStrings); SQLFreeStmt (stmt, SQL_CLOSE); } if ( SqlWorked(sqlResult) ) { sprintf (theStmt, "CREATE TABLE %s (%s INTEGER UNSIGNED)", TABLENAME, COLUMNNAME); sqlResult = SQLExecDirect (stmt, (SQLCHAR *)theStmt, lengthOfAllStrings); SQLFreeStmt (stmt, SQL_CLOSE); } if ( SqlWorked(sqlResult) ) { sprintf (theStmt, "INSERT INTO %s (%s) VALUES (%lu)", TABLENAME, COLUMNNAME, valueIntoServer); sqlResult = SQLExecDirect (stmt, (SQLCHAR *)theStmt, lengthOfAllStrings); SQLFreeStmt (stmt, SQL_CLOSE); } if ( SqlWorked(sqlResult) ) { sprintf (theStmt, "SELECT %s FROM %s", COLUMNNAME, TABLENAME); sqlResult = SQLExecDirect (stmt, (SQLCHAR *)theStmt, lengthOfAllStrings); if ( SqlWorked(sqlResult) ) { sqlResult = SQLBindCol (stmt, 1, SQL_C_ULONG, &valueFromServer, sizeof(valueFromServer), NULL); if ( SqlWorked(sqlResult) ) { sqlResult = SQLFetch (stmt); } } } SQLFreeHandle (SQL_HANDLE_STMT, stmt); } } if ( SqlWorked(sqlResult) ) { if (valueIntoServer == valueFromServer) { printf("BINGO, value out equals value in!"); } else { printf("ERROR, value out does NOT equal value in!"); } } else { printf("Something failed, check the code."); } return 0; } /* \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ Converts an SQLRETURN value into a Boolean. */ //////////////////////////////////////////////////////////// // -------------------- Boolean SqlWorked (SQLRETURN sqlResult) { if (sqlResult == SQL_SUCCESS || sqlResult == SQL_SUCCESS_WITH_INFO) { return true; } else { return false; } }