当我尝试在 WM 6.5 上的 MFC 应用程序上使用 SQLite 时,请查看以下我的代码,请告诉我我做错了什么。任何内存泄漏等?
通常,程序运行良好。但是,只有两个 WM 设备在运行我的应用程序,其中一个我根本没有听到任何问题。但是当我运行 select 语句时,另一个有时会出现内存不足。有时,没有错误消息,但它会破坏一些数据。而且我还意识到,有时它会将 .dat 扩展名放在我的数据库名称上。
任何帮助都将受到高度评价。
SelectedID=-1;
iSelectAll=0;
SipShowIM(SIPF_OFF);
//////////////////////////////////////////////////////////////////////////////////////////////////////////
CString FindStr1,FindStr2;
m_Edit1.GetWindowText(FindStr1); FindStr1.TrimLeft(); FindStr1.TrimRight();
m_Edit2.GetWindowText(FindStr2); FindStr2.TrimLeft(); FindStr2.TrimRight();
if(FindStr1 == L"" && FindStr2 == L"")
{
MessageBox(L"Please enter a keyword to search!",L"Product Search",MB_OK|MB_ICONERROR); return -1;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
bIgnore = TRUE;
int Ret = 0;
sqlite3 *db; sqlite3_stmt *stmt = NULL;
m_lv1.DeleteAllItems();
const char *data = NULL;
if( !sqlite3_open(chDatabaseName, &db) )
{
CString Str;
CString SearchField = L"";
SearchField.Format(L" WHERE Description1 LIKE '%%%s%%' AND Description1 LIKE '%%%s%%' ",(LPCTSTR)FindStr1,(LPCTSTR)FindStr2);
Str.Format(L"SELECT Status,Description1,ProductID,Stock,Code,UnitRate1,Size,Unit1,Unit2,DefaultUnit,VAT,Notes FROM Products %s ORDER BY Status DESC, GroupID DESC, Description1 DESC",(LPCTSTR)SearchField);
char chSqlString[1024+1];
memset(chSqlString,0x00,sizeof(chSqlString)-1);wcstombs(chSqlString,Str,1024);chSqlString[Str.GetLength()]='\0';
if ( sqlite3_prepare_v2( db, chSqlString, -1, &stmt, NULL )== SQLITE_OK)
{
m_lv1.SendMessage(WM_SETREDRAW,0,0);
while( sqlite3_step( stmt ) == SQLITE_ROW )
{
CString StrDescription,StrSize;
data = (const char*)sqlite3_column_text( stmt, 0 ); m_lv1.InsertItem( 0 , L"",atol(data)-1);
m_lv1.SetItemText(0, 1,L"1");
data = (const char*)sqlite3_column_text( stmt, 1 ); if(data) StrDescription=CString(data); else StrDescription=L"";
data = (const char*)sqlite3_column_text( stmt, 2 ); if(data) m_lv1.SetItemText(0, 3,CString(data)); else m_lv1.SetItemText(0, 3,L"");
data = (const char*)sqlite3_column_text( stmt, 3 ); if(data) m_lv1.SetItemText(0, 4,CString(data)); else m_lv1.SetItemText(0, 4,L"");
data = (const char*)sqlite3_column_text( stmt, 4 ); if(data) m_lv1.SetItemText(0, 5,CString(data)); else m_lv1.SetItemText(0, 5,L"");
data = (const char*)sqlite3_column_text( stmt, 5 ); if(data) m_lv1.SetItemText(0, 6,CString(data)); else m_lv1.SetItemText(0, 6,L"");
data = (const char*)sqlite3_column_text( stmt, 6 ); if(data) StrSize=CString(data); else StrSize=L"";
//data = (const char*)sqlite3_column_text( stmt, 6 ); if(data) m_lv1.SetItemText(0, 7,CString(data)); else m_lv1.SetItemText(0, 7,L"");
data = (const char*)sqlite3_column_text( stmt, 7 ); if(data) m_lv1.SetItemText(0, 8,CString(data)); else m_lv1.SetItemText(0, 8,L"");
data = (const char*)sqlite3_column_text( stmt, 8 ); if(data) m_lv1.SetItemText(0, 9,CString(data)); else m_lv1.SetItemText(0, 9,L"");
data = (const char*)sqlite3_column_text( stmt, 9 ); if(data) m_lv1.SetItemText(0,10,CString(data)); else m_lv1.SetItemText(0,10,L"");
data = (const char*)sqlite3_column_text( stmt,10 ); if(data) m_lv1.SetItemText(0,11,CString(data)); else m_lv1.SetItemText(0,11,L"");
data = (const char*)sqlite3_column_text( stmt,11 ); if(data) m_lv1.SetItemText(0,12,CString(data)); else m_lv1.SetItemText(0,12,L"");
StrDescription = StrDescription.Trim()+L" "+StrSize.Trim();
m_lv1.SetItemText(0, 2,StrDescription);
m_lv1.SetItemText(0, 7,StrSize);
}
m_lv1.SendMessage(WM_SETREDRAW,1,0);
}
else
{
MessageBox(CString(sqlite3_errmsg(db)),L"SELECT Err",MB_OK|MB_ICONERROR); Ret = -1;// Out of memory error happens here !
}
}
else
{
MessageBox(CString(sqlite3_errmsg(db)),L"Open DB",MB_OK|MB_ICONERROR); Ret =-1;
}
sqlite3_finalize( stmt );
sqlite3_close(db);
//////////////////////////////////////////////////////////////////////////////////////////////////////////
return Ret;
还有一些更新或插入语句的代码
COleDateTime today(COleDateTime::GetCurrentTime());
EndTime.Format(L"%02d:%02d:%02d",today.GetHour(),today.GetMinute(),today.GetSecond());
CString Str,SQLStr,SQLStrA,StrTableName;
pTab1->m_Edit6.GetWindowText(Str); Str.TrimLeft(); Str.TrimRight();
if(Str==L"" && SaleType!=3 && SaleType!=33 && dbR31!=0 )
{
MessageBox(_T("Please select a payment method!"), _T("Missing Detail"), MB_OK|MB_ICONWARNING); return -1;
}
SQLStrA=L"";
switch(SaleType)
{
case 1: SQLStrA.Format(L"UPDATE Config SET OrderSN=%ld" ,(SaleID+1)); StrTableName=L"Orders" ; break;
case 2: SQLStrA.Format(L"UPDATE Config SET InvoiceSN=%ld",(SaleID+1)); StrTableName=L"Invoices" ; break;
case 3: SQLStrA.Format(L"UPDATE Config SET ReturnSN=%ld" ,(SaleID+1)); StrTableName=L"Returns" ; break;
case 11: SQLStrA=L""; StrTableName=L"Orders" ; break;
case 22: SQLStrA=L""; StrTableName=L"Invoices" ; break;
case 33: SQLStrA=L""; StrTableName=L"Returns" ; break;
}
if(SaleType==1 ||SaleType==2 || SaleType==3)
{
SQLStr=L"INSERT INTO "+StrTableName+L" (OrderID,CustomerID,OrderNo,OrderDate,DeliveryDate,Payment,Comments1,Comments2,Comments3,DscTotal,VATTotal,NetTotal,Status,StartTime,EndTime) VALUES (";
Str.Format(_T("%ld"),SaleID); SQLStr = SQLStr +L" " +Str+ L" ,";
Str.Format(_T("%ld"),SCID); SQLStr = SQLStr +L" " +Str+ L" ,";
pTab1->m_Edit1.GetWindowText(Str) ; Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" '"+Str+ L"',";
pTab1->m_DateTime1.GetWindowText(Str) ; Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" '"+Str+ L"',";
pTab1->m_DateTime2.GetWindowText(Str) ; Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" '"+Str+ L"',";
pTab1->m_Edit6.GetWindowText(Str) ; Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" '"+Str+ L"',";
pTab1->m_Comments1.GetWindowText(Str) ; Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" '"+Str+ L"',";
pTab1->m_Comments2.GetWindowText(Str) ; Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" '"+Str+ L"',";
pTab1->m_Comments3.GetWindowText(Str) ; Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" '"+Str+ L"',";
pTab2->m_DscTotal.GetWindowText(Str) ; Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" "+Str+ L" ,";
pTab2->m_VATTotal.GetWindowText(Str) ; Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" "+Str+ L" ,";
pTab2->m_NetTotal.GetWindowText(Str) ; Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" "+Str+ L" , 0 ,";
SQLStr = SQLStr +L" '"+StartTime+L"',";
SQLStr = SQLStr +L" '"+EndTime+ L"' )";
}
if(SaleType==11 || SaleType==22 || SaleType==33)
{
SQLStrA=L"";
SQLStr=L"UPDATE "+StrTableName+L" SET ";
pTab1->m_DateTime1.GetWindowText(Str) ; Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" [OrderDate]='"+Str+ L"',";
pTab1->m_DateTime2.GetWindowText(Str) ; Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" [DeliveryDate]='"+Str+ L"',";
pTab1->m_Edit6.GetWindowText(Str) ; Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" [Payment]='"+Str+ L"',";
pTab1->m_Comments1.GetWindowText(Str) ; Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" [Comments1]='"+Str+ L"',";
pTab1->m_Comments2.GetWindowText(Str) ; Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" [Comments2]='"+Str+ L"',";
pTab1->m_Comments3.GetWindowText(Str) ; Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" [Comments3]='"+Str+ L"',";
pTab2->m_DscTotal.GetWindowText(Str) ; Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" [DscTotal]='"+Str+ L"',";
pTab2->m_VATTotal.GetWindowText(Str) ; Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" [VATTotal]='"+Str+ L"',";
pTab2->m_NetTotal.GetWindowText(Str) ; Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" [NetTotal]='"+Str+ L"',";
SQLStr = SQLStr +L" [Status]=0,";
SQLStr = SQLStr +L" [EndTime]='"+EndTime+ L"' ";
Str.Format(_T("%ld"),SCID); SQLStr = SQLStr +L" WHERE [CustomerID]=" +Str+ L" AND ";
Str.Format(_T("%ld"),SaleID); SQLStr = SQLStr +L" [OrderID]=" +Str+ L" ";
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
int Ret = 0;
sqlite3 *db; sqlite3_stmt *stmt = NULL;
if(SQLStrA != L"")
{
if( !sqlite3_open(chDatabaseName, &db) )
{
char chSqlString[1024+1];
memset(chSqlString,0x00,sizeof(chSqlString)-1);wcstombs(chSqlString,SQLStrA,1024);chSqlString[SQLStrA.GetLength()]='\0';
if ( sqlite3_prepare_v2( db, chSqlString, -1, &stmt, NULL )== SQLITE_OK)
{
int UpdateRet = sqlite3_step( stmt );
if (( UpdateRet!= SQLITE_DONE )&&( UpdateRet!= SQLITE_ROW ))
{
MessageBox(CString(sqlite3_errmsg(db)),L"[SerialNo]",MB_OK|MB_ICONERROR); Ret = -1;
}
}
else
{
MessageBox(CString(sqlite3_errmsg(db)),L"[SerialNo]",MB_OK|MB_ICONERROR); Ret = -1;
}
}
else
{
MessageBox(CString(sqlite3_errmsg(db)),L"Open DB",MB_OK|MB_ICONERROR); Ret =-1;
}
sqlite3_finalize( stmt ); sqlite3_close(db);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
if(Ret!=-1)
{
if( !sqlite3_open(chDatabaseName, &db) )
{
char chSqlString[2048+1];
memset(chSqlString,0x00,sizeof(chSqlString)-1);wcstombs(chSqlString,SQLStr,2048);chSqlString[SQLStr.GetLength()]='\0';
if ( sqlite3_prepare_v2( db, chSqlString, -1, &stmt, NULL )== SQLITE_OK)
{
int UpdateRet = sqlite3_step( stmt );
if (( UpdateRet!= SQLITE_DONE )&&( UpdateRet!= SQLITE_ROW ))
{
MessageBox(CString(sqlite3_errmsg(db)),L"[Master]",MB_OK|MB_ICONERROR); Ret = -1;
}
}
else
{
MessageBox(CString(sqlite3_errmsg(db)),L"[Master]",MB_OK|MB_ICONERROR); Ret = -1;
}
}
else
{
MessageBox(CString(sqlite3_errmsg(db)),L"Open DB",MB_OK|MB_ICONERROR); Ret =-1;
}
sqlite3_finalize( stmt ); sqlite3_close(db);
}
return Ret;
还有我正在使用的另一个更新或插入语句。
CString Str,SQLStr,StrTableName;
if(SaleType==1 || SaleType==11) StrTableName=L"OrderDetails" ;
if(SaleType==2 || SaleType==22) StrTableName=L"InvoiceDetails";
if(SaleType==3 || SaleType==33) StrTableName=L"ReturnDetails" ;
int Ret = 0;
sqlite3 *db; sqlite3_stmt *stmt = NULL;
if( !sqlite3_open(chDatabaseName, &db) )
{
char chSqlString[2048+1];
for( int i=0 ; i < pTab2->m_lv2.GetItemCount() ; i++)
{
if(_wtol(pTab2->m_lv2.GetItemText(i,9)) < 0) //if RecordID == -1
{
if(pTab2->m_lv2.GetItemText(i,0).Trim()!=L"*")
{
SQLStr=L"INSERT INTO "+StrTableName+L" (OrderID,CustomerID,Qty,Unit,Price,Vat,Dsc,Notes,ProductID,DetailID,Reason ) VALUES (";
Str.Format(_T("%ld"),SaleID) ; SQLStr = SQLStr +L" " +Str+ L" ,";
Str.Format(_T("%ld"),SCID); SQLStr = SQLStr +L" " +Str+ L" ,";
Str = pTab2->m_lv2.GetItemText(i, 1); Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" " +Str+ L" ,";//Qty
Str = pTab2->m_lv2.GetItemText(i, 3); Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" '" +Str+ L"',";//Unit
Str = pTab2->m_lv2.GetItemText(i, 4); Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" " +Str+ L" ,";//Price
Str = pTab2->m_lv2.GetItemText(i, 5); Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" " +Str+ L" ,";//VAT Rate
Str = pTab2->m_lv2.GetItemText(i, 6); Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" " +Str+ L" ,";//DSC Rate
Str = pTab2->m_lv2.GetItemText(i, 7); Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" '" +Str+ L"',";//Notes
Str = pTab2->m_lv2.GetItemText(i, 8); Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" " +Str+ L" ,";//ProductID
//Str = pTab2->m_lv2.GetItemText(i, 9); Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" " +Str+ L" ,";//RecordID - OrderID
Str = pTab2->m_lv2.GetItemText(i,10); Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" " +Str+ L" ,";//DetailID
//Str = pTab2->m_lv2.GetItemText(i,11); Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" " +Str+ L" ,";//PromotionID
Str = pTab2->m_lv2.GetItemText(i,12); Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" '" +Str+ L"')";//Reason
}
}
else
{
if(pTab2->m_lv2.GetItemText(i,0).Trim()!=L"*")
{
SQLStr=L"UPDATE "+StrTableName+L" SET ";
Str = pTab2->m_lv2.GetItemText(i, 1); Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" Qty= " +Str+ L" ,";//Qty
Str = pTab2->m_lv2.GetItemText(i, 3); Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" Unit='" +Str+ L"',";//Unit
Str = pTab2->m_lv2.GetItemText(i, 4); Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" Price= " +Str+ L" ,";//Price
Str = pTab2->m_lv2.GetItemText(i, 5); Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" Vat= " +Str+ L" ,";//VAT Rate
Str = pTab2->m_lv2.GetItemText(i, 6); Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" Dsc= " +Str+ L" ,";//DSC Rate
Str = pTab2->m_lv2.GetItemText(i, 7); Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" Notes='" +Str+ L"',";//Notes
Str = pTab2->m_lv2.GetItemText(i,12); Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L"Reason='" +Str+ L"' ";//Notes
Str.Format(_T("%ld"),SaleID); SQLStr = SQLStr +L" WHERE OrderID=" +Str+ L" AND ";
Str = pTab2->m_lv2.GetItemText(i,10); Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" DetailID=" +Str+ L" ";
}
else
{
SQLStr=L"DELETE FROM "+StrTableName+L" ";
Str.Format(_T("%ld"),SaleID); SQLStr = SQLStr +L" WHERE OrderID=" +Str+ L" AND ";
Str = pTab2->m_lv2.GetItemText(i,10); Str.TrimLeft(); Str.TrimRight(); SQLStr = SQLStr +L" DetailID=" +Str+ L" ";
}
}
memset(chSqlString,0x00,sizeof(chSqlString)-1);wcstombs(chSqlString,SQLStr,2048);chSqlString[SQLStr.GetLength()]='\0';
if ( sqlite3_prepare_v2( db, chSqlString, -1, &stmt, NULL )== SQLITE_OK)
{
int UpdateRet = sqlite3_step( stmt );
if (( UpdateRet!= SQLITE_DONE )&&( UpdateRet!= SQLITE_ROW ))
{
MessageBox(CString(sqlite3_errmsg(db)),L"[Detail]",MB_OK|MB_ICONERROR); Ret = -1;
}
}
else
{
MessageBox(CString(sqlite3_errmsg(db)),L"[Detail]",MB_OK|MB_ICONERROR); Ret = -1;
}
}
///////////////////////////////////////////////////////////////////////////////////////////
}
else
{
MessageBox(CString(sqlite3_errmsg(db)),L"Open DB",MB_OK|MB_ICONERROR); Ret =-1;
}
sqlite3_finalize( stmt ); sqlite3_close( db );
return 0;