The functions are declared in the "Mysql.h" header file, and the definitions are in the mysql.cpp file
wherever your mysql folder is there's a folder in there called 'Include'. all the files in there
should be included in your project.
#include the mysql.h file in your [yourapp.cpp] file. then here's a snippet: well a whole function
Sorry it's a little sloppy; It's time to leave work now, and I'm doing this at the last minute
int Export(BSTR szServer, BSTR szDB, BSTR szUser, BSTR szPassword, BSTR szSQL, int nExportType)
{
SQLRETURN sr;
int numOfRows=0;
int numOfFields=0;
int connected=0;
MYSQL *dbHandle;
MYSQL_RES *res;
MYSQL_FIELD *field;
MYSQL_ROW row;
char errmsg[512];
hDbConn =0;
hOdbcEnv = 0;
dbHandle=mysql_init((MYSQL*)0);
CString szDBFName;
CString strPath,strTblName;
try
{
CString sFileFilter;
if(nExportType)
sFileFilter = "Excel Spreadsheet (*.xls)|*.xls||";
else
sFileFilter = ".dbf files (*.dbf)|*.dbf||";
CFileDialog dlg(FALSE,".DBF",NULL,OFN_ENABLESIZING | OFN_HIDEREADONLY,sFileFilter);
if(dlg.DoModal() == IDOK)
{
szDBFName = dlg.GetPathName();
strTblName = dlg.GetFileName();
if((szDBFName.GetLength() || strTblName.GetLength() ) == 0)
{
AfxMessageBox("Export Canceled",MB_OK | MB_ICONINFORMATION);
return 0;
}
}
else
{
AfxMessageBox("Export Canceled",MB_OK | MB_ICONINFORMATION);
return 0;
}
int nPos = szDBFName.Find(strTblName,0);
strPath = szDBFName.Left(nPos);
if(mysql_real_connect(dbHandle,
(char*)szServer,
(char*)szUser,
(char*)szPassword,
(char*)szDB,
MYSQL_PORT,
NULL,
0))
{
connected=1;
}
else
{
sprintf(errmsg, "\nConnection Error: %s\n", mysql_error(dbHandle));
AfxMessageBox(errmsg);
connected = 0;
}
if(mysql_select_db(dbHandle,(char*)szDB))
{
sprintf(errmsg, "\nSelect DB Error: %s\n", mysql_error(dbHandle));
AfxMessageBox(errmsg);
return 0;
}
if(mysql_query(dbHandle,(char*)szSQL))
{
sprintf(errmsg, "\nError: %s\n", mysql_error(dbHandle));
AfxMessageBox(errmsg);
return 0;
}
res=mysql_store_result(dbHandle);
CString strConnect;
if(nExportType)
strConnect.Format("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;DefaultDir=%s;",strPath);
else
strConnect.Format("Driver={Microsoft dBase Driver (*.dbf)};DriverID=533;Dbq=%s;",strPath);
if(hDbConn != SQL_NULL_HANDLE)
SQLFreeHandle(SQL_HANDLE_DBC, hDbConn);
if(hOdbcEnv != SQL_NULL_HANDLE)
SQLFreeHandle(SQL_HANDLE_ENV,hOdbcEnv);
sr = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hOdbcEnv);
SR_CHECK
sr = SQLSetEnvAttr(hOdbcEnv, SQL_ATTR_ODBC_VERSION ,(SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER);
SR_CHECK
sr = SQLAllocHandle(SQL_HANDLE_DBC, hOdbcEnv, & hDbConn);
SR_CHECK
sr = SQLSetConnectAttr(hDbConn,SQL_ATTR_LOGIN_TIMEOUT,(void*)5,0);
SR_CHECK
char szConnectString[512];
sprintf(szConnectString,"%s",strConnect);
char szDescription[1024];
SQLSMALLINT length;
sr = SQLDriverConnect(hDbConn,NULL , (UCHAR*)szConnectString, 512,
(UCHAR*)szDescription,1023,&length,SQL_DRIVER_COMPLETE_REQUIRED);
SR_CHECK
CString sColumns;
numOfRows = (int) mysql_num_rows(res);
numOfFields= (int) mysql_num_fields(res);
if(numOfRows > 255)
{
AfxMessageBox("Too many fields for export.");
return 0;
}
char * bNeedTics = new char[numOfFields] ;
int * nFieldLen = new int[numOfFields];
CString sColumnsToInsert;
sColumnNames = new CString[numOfFields];
for(int i = 0; i < numOfFields; i++)
{
bNeedTics[i] = 'N';
}
CString* sStatements = new CString[numOfRows];
for (i =0;i < numOfFields;i++)
{
field=mysql_fetch_field(res);
CString sTemp = field->name;
sColumnsToInsert += sTemp + ",";
int nType = field->type;
int nLen = (int)field->length;
CString sCol;
nFieldLen[i] = nLen;
sTemp = CheckFieldname(sTemp,i);
sColumnNames[i] = sTemp;
switch(nType)
{
case FIELD_TYPE_ENUM:
case FIELD_TYPE_TINY:
case FIELD_TYPE_SHORT:
sCol.Format("%s %s,",sTemp,"INTEGER ");
break;
case FIELD_TYPE_LONG:
sCol.Format("%s %s,",sTemp,"LONG ");
break;
case FIELD_TYPE_FLOAT:
case FIELD_TYPE_DECIMAL:
sCol.Format("%s %s,",sTemp,"FLOAT ");
break;
case FIELD_TYPE_DOUBLE:
sCol.Format("%s %s,",sTemp,"DOUBLE ");
break;
case FIELD_TYPE_NULL:
break;
case FIELD_TYPE_LONGLONG:
case FIELD_TYPE_INT24:
sCol.Format("%s %s,",sTemp,"LONG ");
break;
case FIELD_TYPE_YEAR:
case FIELD_TYPE_DATE:
case FIELD_TYPE_NEWDATE:
bNeedTics[i] = 'D';
sCol.Format("%s %s,",sTemp,"DATE ");
break;
case FIELD_TYPE_TIME:
case FIELD_TYPE_DATETIME:
case FIELD_TYPE_TIMESTAMP:
bNeedTics[i] = 'D';
sCol.Format("%s %s,",sTemp,"DATETIME ");
break;
case FIELD_TYPE_SET:
sCol.Format("%s %s,",sTemp,"int ");
break;
case FIELD_TYPE_TINY_BLOB:
case FIELD_TYPE_MEDIUM_BLOB:
case FIELD_TYPE_LONG_BLOB:
case FIELD_TYPE_BLOB:
bNeedTics[i] = 'T';
sCol.Format("%s %s,",sTemp,"MEMO ");
break;
case FIELD_TYPE_VAR_STRING:
case FIELD_TYPE_STRING:
bNeedTics[i] = 'T';
sCol.Format("%s TEXT(%d),",sTemp,nLen);
break;
}
sColumns += sCol;
}
sColumnsToInsert = sColumnsToInsert.Left(sColumnsToInsert.GetLength() - 1);
sColumns = sColumns.Left(sColumns.GetLength() - 1);
sColumns = "CREATE TABLE " + strTblName + " (" + sColumns + ")";
char tmp1[10000];
sprintf(tmp1,"%s",sColumns);
SQLCHAR* SQL1 = (SQLCHAR*)tmp1;
if(!nExportType)
executeSQL(SQL1);
SQLSMALLINT cols = 0;
CString sTic;
CString sTemp;
CString sValues,sInsertString;
SQLDisconnect(hDbConn);
CDBFRecordset* m_Set = 0;
CString str;
if(!nExportType)
{
m_Set = new CDBFRecordset;
m_Set->Open(szDBFName);
for(i=0;i<numOfRows;i++)
{
row=mysql_fetch_row( res );
m_Set->AddNew();
sValues = "";
for (int col=0;col<numOfFields;col++)
{
field=mysql_fetch_field(res);
str = row[col];
CString sYear,sMonth,sDay,sHour,sMinute,sSecond;
COleDateTime * t;
BOOL bDate = FALSE;
if(bNeedTics[col] == 'D')
{
str.Remove('-');
int len = str.GetLength();
sYear = str.Mid(0,4);
sMonth = str.Mid(4,2);
sDay = str.Mid(6,2);
if(len > 8)
{
sHour = str.Mid(8,2);
sMinute = str.Mid(10,2);
sSecond = str.Mid(12,2);
}
else
{
sHour = sMinute = sSecond = "0";
}
bDate = TRUE;
t = new COleDateTime(atoi(sYear),
atoi(sMonth),atoi(sDay),atoi(sHour),atoi(sMinute),atoi(sSecond));
}
COleVariant fVar;
if(bDate)
fVar = *t;
else
fVar = str;
m_Set->SetFieldValue(col,fVar);
}
m_Set->Update();
}
m_Set->Close();
delete m_Set;
}
mysql_free_result( res ) ;
dbHandle->free_me;
If it's broken, I probably did it
bdiamond
|