1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
#include <windows.h>
#include <stdio.h>
#include <odbcinst.h>
#include <sql.h>
#include <sqlext.h>
#include "MySql.h"
unsigned int iInstallerError()
{
DWORD pErr;
char szErrMsg[512];
WORD cbMsgBuffer=512;
WORD cbRet;
WORD wErrNum=1;
while(SQLInstallerError(wErrNum,&pErr,szErrMsg,cbMsgBuffer,&cbRet)!=SQL_NO_DATA)
{
wErrNum++;
};
return (unsigned int)pErr;
}
unsigned int iCreateDB(char const* szDBName) //To create a Microsoft Access Database from
{ //scratch, you use SQLConfigDataSource(). I
char szCreate[256]; //believe if the database already exists at the
//specific location SQLInstallerError() returns
strcpy(szCreate,"CREATE_DB="); //'11'.
strcat(szCreate,szDBName);
if(SQLConfigDataSource(0,ODBC_ADD_DSN,"Microsoft Access Driver (*.mdb)",szCreate))
return TRUE;
else
return iInstallerError();
}
TIMESTAMP_STRUCT ParseDate(char* szDate, char* szFormat, char* szDelimiter)
{
UINT i=0,j=0,k=0;
TIMESTAMP_STRUCT ts;
char buf[3][8]; //buf[0] for month, buf[1] for day, buf[2] for year
char* p;
memset(buf,0,sizeof(buf)); //zero out buf[]
p=szDate;
for(i=0;i<strlen((char*)szDate);i++)
{
if(*p!=*szDelimiter)
{
buf[j][k++]=*p;
buf[j][k+1]='\0';
}
else
{
j++;
k=0;
}
p++;
}
if(!stricmp((char*)szFormat,"MDY"))
{
ts.month=(short)atoi(buf[0]);
ts.day=(short)atoi(buf[1]);
ts.year=(short)atoi(buf[2]);
}
if(!stricmp((char*)szFormat,"DMY"))
{
ts.day=(short)atoi(buf[0]);
ts.month=(short)atoi(buf[1]);
ts.year=(short)atoi(buf[2]);
}
if(!stricmp((char*)szFormat,"YMD"))
{
ts.year=(short)atoi(buf[0]);
ts.month=(short)atoi(buf[1]);
ts.day=(short)atoi(buf[2]);
}
return ts;
}
void MkDate(TIMESTAMP_STRUCT& ts, char* szBuffer)
{
char szMonth[4],szDay[4],szYear[8];
sprintf(szMonth,"%u",ts.month);
sprintf(szDay,"%u",ts.day);
sprintf(szYear,"%u",ts.year);
strcpy(szBuffer,szMonth);
strcat(szBuffer,"/");
strcat(szBuffer,szDay);
strcat(szBuffer,"/");
strcat(szBuffer,szYear);
return;
}
UINT blnMakeTable(SQL& Sql) //Uses SQL Create Table statement to add table
{ //to database represented by sql->hConn
char szQuery[256];
SQLHSTMT hStmt;
strcpy(szQuery,"CREATE TABLE Table1 (Id LONG NOT NULL PRIMARY KEY, Float_Point DOUBLE, Date_Field DATETIME, Text_Field CHAR(30));");
SQLAllocHandle(SQL_HANDLE_STMT,Sql.hConn,&hStmt);
if(SQLExecDirect(hStmt,(SQLTCHAR*)szQuery,SQL_NTS)==0)
{
SQLFreeHandle(SQL_HANDLE_STMT,hStmt);
return(TRUE);
}
else
return(FALSE);
}
UINT blnInsert(SQL& sql)
{
char* szStr[]={(char*)"My Birthday",(char*)"Walk On Moon?",(char*)"Some String",(char*)"April Fools Day"};
char* szDate[]={(char*)"11/15/1952",(char*)"6/30/1969",(char*)"1/1/2006",(char*)"4/1/2006"};
double dblNum[]={3.14159,1.23456,15.1234,0.54321};
char szQuery[100],szString[32],szBuffer[128]; //Let me give you a hint about something. If you decide
SQLINTEGER iNts=SQL_NTS; //to use raw ODBC as your database access methodology, the
UINT i,id,iRet=FALSE; //hard part is SQLBindParameter() for inserting prepared
TIMESTAMP_STRUCT ts; //SQL statements, and SQLBindCol() for selecting data. These
SQLINTEGER iJnk; //will inevitably take you some time to learn. I chose an
SQLHSTMT hStmt; //integer, a double, a data, and a char string so as to get
double dbl; //you started on the most common data types.
if(SQLAllocHandle(SQL_HANDLE_STMT,sql.hConn,&hStmt)==SQL_SUCCESS)
{
strcpy((char*)szQuery,"INSERT INTO Table1(Id, Float_Point, Date_Field, Text_Field) VALUES(?,?,?,?);");
printf(" SQLExecute(hStmt)\n");
printf("iId Double Date String 0=SQL_SUCCESS\n");
printf("========================================================================\n");
if(SQLPrepare(hStmt,(SQLTCHAR*)szQuery,SQL_NTS)==SQL_SUCCESS)
{
SQLBindParameter(hStmt,1,SQL_PARAM_INPUT,SQL_C_LONG,SQL_INTEGER,0,0,&id,0,&iJnk);
SQLBindParameter(hStmt,2,SQL_PARAM_INPUT,SQL_C_DOUBLE,SQL_DOUBLE,0,0,&dbl,0,&iJnk);
SQLBindParameter(hStmt,3,SQL_PARAM_INPUT,SQL_C_TYPE_DATE,SQL_TYPE_TIMESTAMP,16,0,&ts,0,&iJnk);
SQLBindParameter(hStmt,4,SQL_PARAM_INPUT,SQL_C_TCHAR,SQL_CHAR,31,0,szString,32,&iNts);
for(i=0;i<4;i++)
{
id=i+1, dbl=dblNum[i];
ts=ParseDate(szDate[i],(char*)"mdy",(char*)"/");
strcpy(szString,szStr[i]);
if(SQLExecute(hStmt)==SQL_SUCCESS)
{
memset(szBuffer,0,128);
printf("%-6u%8.2f %-12.10s %-20s%6u\n",id,dbl,szDate[i],szString,SQL_SUCCESS);
}
else
{
SQLGetDiagRec(SQL_HANDLE_STMT,hStmt,1,sql.szErrCode,&sql.iNativeErrPtr,sql.szErrMsg,512,&sql.iTextLenPtr);
printf(" sql.dr.szErrCode = %s\n",sql.szErrCode);
printf(" sql.dr.szErrMsg = %s\n",sql.szErrMsg);
SQLFreeHandle(SQL_HANDLE_STMT,hStmt);
return FALSE;
}
}
iRet=TRUE;
printf("\n");
}
SQLFreeHandle(SQL_HANDLE_STMT,hStmt);
}
return iRet;
}
UINT blnDumpData(SQL& sql)
{
char szQuery[100],szBuffer[128],szDate[12];
SQLTCHAR szString[20];
TIMESTAMP_STRUCT ts;
SQLINTEGER iJnk;
SQLHSTMT hStmt;
double dblNum;
UINT iId;
if(SQLAllocHandle(SQL_HANDLE_STMT,sql.hConn,&hStmt)==SQL_SUCCESS)
{
strcpy(szQuery,"SELECT Table1.Id,Table1.Float_Point,Table1.Date_Field,Table1.Text_Field FROM Table1;");
SQLBindCol(hStmt,1,SQL_C_ULONG,&iId,0,&iJnk);
SQLBindCol(hStmt,2,SQL_C_DOUBLE,&dblNum,0,&iJnk);
SQLBindCol(hStmt,3,SQL_C_TYPE_DATE,&ts,0,&iJnk);
SQLBindCol(hStmt,4,SQL_C_TCHAR,szString,20,&iJnk);
if(SQLExecDirect(hStmt,(SQLTCHAR*)szQuery,SQL_NTS)==SQL_SUCCESS)
{
printf("iId Double Date String 0=SQL_SUCCESS\n");
printf("========================================================================\n");
do
{
if(SQLFetch(hStmt)==SQL_NO_DATA)
break;
memset(szBuffer,0,128);
MkDate(ts,szDate);
printf("%-6u%8.2f %-12.10s %-20s%6u\n",iId,dblNum,szDate,szString,SQL_SUCCESS);
} while(TRUE);
}
SQLCloseCursor(hStmt);
SQLFreeHandle(SQL_HANDLE_STMT,hStmt);
return TRUE;
}
return FALSE;
}
int main()
{
char szBuffer[512];
std::string s1;
SQL Sql;
Sql.strDriver="Microsoft Access Driver (*.mdb)";
GetCurrentDirectory(512,szBuffer);
s1=szBuffer;
Sql.strDBQ=s1+"\\"+"TestData.mdb";
printf("Sql.strDBQ = %s\n",Sql.strDBQ.c_str());
if(iCreateDB(Sql.strDBQ.c_str())==TRUE)
{
Sql.ODBCConnect();
if(Sql.blnConnected==TRUE)
{
printf("Sql.blnConnected = True\n");
if(blnMakeTable(Sql))
{
printf("blnMakeTable() Succeeded!\n");
if(blnInsert(Sql))
{
printf("blnInsert() Succeeded!\n\n\n");
blnDumpData(Sql);
}
}
Sql.ODBCDisconnect();
}
}
getchar();
return 0;
}
|