📄 passworddbms.cpp
字号:
return KErrNone;
}
// ---------------------------------------------------------------------------
// CBookDb::AddBookWithCppApi()
//
// Add a book to database using RDbTable API
// ---------------------------------------------------------------------------
TInt CPasswordDb::AddPasswordWithCppApi(const TDesC& aTitle,
const TDesC& aUsername,
const TDesC& aPassword,
const TDesC& aDescription)
{
if(aTitle.Length()==0)
{
return KErrGeneral;
}
// Create an updateable database table object
RDbTable table;
TInt err = table.Open(iPasswordDb, KPasswordTable, table.EUpdatable);
if(err!=KErrNone)
{
return err;
}
CDbColSet* passwordColSet = table.ColSetL();
CleanupStack::PushL(passwordColSet);
table.Reset();
//RDbColWriteStream writeStream;
TTime aDateTime;
aDateTime.UniversalTime();
// TBuf<100> aCreateDateStr;
// aCreateDateStr.Format(_L("%1%/1%2%/2%3 %-B%:0%J%:1%T%:3%:2%S%+B"),aDateTime.HomeTime());
// aDateTime.FormatL(aCreateDateStr,_L("%1%/1%2%/2%3 %-B%:0%J%:1%T%:3%:2%S%+B"));
// TBuf<100> aModifiedDataStr;
// aDateTime.FormatL(aModifiedDataStr,_L("%1%/1%2%/2%3 %-B%:0%J%:1%T%:3%:2%S%+B"));
TRAPD(error,
table.InsertL();
table.SetColL(passwordColSet->ColNo(KPasswordTitleCol), aTitle); // col = 1
table.SetColL(passwordColSet->ColNo(KPasswordUsernameCol), aUsername); // col = 2
table.SetColL(passwordColSet->ColNo(KPasswordPWDCol), aPassword); // col = 3
table.SetColL(passwordColSet->ColNo(KPasswordDescriptionCol), aDescription); // col = 3
table.SetColL(passwordColSet->ColNo(KPasswordCreateDateCol), aDateTime); // col = 3
table.SetColL(passwordColSet->ColNo(KPasswordModifiedDateCol), aDateTime); // col = 3
);
if(error!=KErrNone)
{
return error;
}
// writeStream.Close();
TRAP(err, table.PutL()); // Complete changes (the insertion)
if(err!=KErrNone)
{
return err;
}
CleanupStack::PopAndDestroy(passwordColSet);
table.Close();
return KErrNone;
}
CDesCArrayFlat* CPasswordDb::GetAllPasswordL(RArray<TInt32>& aPasswordID)
{
TPtrC aTitle;
RArray<TInt32> aID;
// TBuf<KDescriptionMaxLength> description;
TBuf<KPasswordItemMaxLength> rowText;
//TBuf<KPasswordItemMaxLength> rowID;
RDbTable table;
TInt err = table.Open(iPasswordDb, KPasswordTable, table.EReadOnly);
User::LeaveIfError(err);
CDesCArrayFlat* resultArray =
new (ELeave)CDesC16ArrayFlat(KArrayGranularity);
CleanupStack::PushL(resultArray);
// CDesCArrayFlat* resultArrayID =
// new (ELeave)CDesC16ArrayFlat(KArrayGranularity);
// CleanupStack::PushL(resultArrayID);
table.Reset();
CDbColSet* colSet = table.ColSetL();
CleanupStack::PushL(colSet);
for (table.FirstL(); table.AtRow(); table.NextL())
{
//description.Zero();
rowText.Zero();
// rowID.Zero();
table.GetL();
aTitle.Set(table.ColDes(colSet->ColNo(KPasswordTitleCol)));
aID.Append(table.ColInt32(colSet->ColNo(KPasswordIDCol)));
rowText.Append(aTitle);
resultArray->AppendL(rowText); // Copy rowText to resultArray
// resultArrayID->AppendL((rowID);
}
CleanupStack::PopAndDestroy(colSet);
// CleanupStack::Pop(resultArrayID);
CleanupStack::Pop(resultArray);
table.Close();
aPasswordID = aID;
return resultArray;
}
// ---------------------------------------------------------------------------
// CBookDb::GetBooksByKeyL()
//
// Get array of books from database according to column name and a search
// pattern. Format of each array item is:
// <Author>|<Title>|<Description>
// ---------------------------------------------------------------------------
CDesCArrayFlat* CPasswordDb::GetPasswordByKeyL(const TInt& aKeyID)
{
TPtrC aUsername, aTitle, aPWD;
// TTime aCreateDate, aModifiedData;
// TPtrC aDescription;
TBuf<255> description;
_LIT(KSelect, "SELECT ");
_LIT(KFrom, " FROM ");
_LIT(KWhere, " WHERE ");
_LIT(KLike, " LIKE '");
_LIT(KOrderBy, "' ORDER BY ");
_LIT(KDot, ", ");
_LIT(KEquel, " = ");
_LIT(KSeparator, "|");
// Sql: SELECT Author, Title, Description FROM Books
// WHERE "aColumnName LIKE aSearchString"
// ORDER BY Title, Author
TBuf<KCustomSqlMaxLength> sqlStr;
sqlStr.Append(KSelect);
sqlStr.Append(KPasswordTitleCol);
sqlStr.Append(KDot);
sqlStr.Append(KPasswordUsernameCol);
sqlStr.Append(KDot);
sqlStr.Append(KPasswordPWDCol);
sqlStr.Append(KDot);
sqlStr.Append(KPasswordDescriptionCol);
sqlStr.Append(KDot);
sqlStr.Append(KPasswordCreateDateCol);
sqlStr.Append(KDot);
sqlStr.Append(KPasswordModifiedDateCol);
sqlStr.Append(KFrom);
sqlStr.Append(KPasswordTable);
sqlStr.Append(KWhere);
sqlStr.Append(KPasswordIDCol);
sqlStr.Append(KEquel);
sqlStr.AppendNum(aKeyID);
// sqlStr.Append(KOrderBy);
// sqlStr.Append(KBooksTitleCol);
// sqlStr.Append(KDot);
// sqlStr.Append(KBooksAuthorCol);
CDesCArrayFlat* resultArray =
new (ELeave)CDesC16ArrayFlat(KArrayGranularity);
CleanupStack::PushL(resultArray);
// Create a view on the database
RDbView view;
User::LeaveIfError(
view.Prepare(iPasswordDb, TDbQuery(sqlStr), view.EReadOnly));
User::LeaveIfError(view.EvaluateAll());
CDbColSet* colSet = view.ColSetL();
CleanupStack::PushL(colSet);
// Append each result row to array
for (view.FirstL(); view.AtRow(); view.NextL())
{
view.GetL();
aTitle.Set(view.ColDes(colSet->ColNo(KPasswordTitleCol)));
aUsername.Set(view.ColDes(colSet->ColNo(KPasswordUsernameCol)));
aPWD.Set(view.ColDes(colSet->ColNo(KPasswordPWDCol)));
TDbColNo descrColNo = colSet->ColNo(KPasswordDescriptionCol);
RDbColReadStream readStream; // A stream object for long columns
readStream.OpenLC(view, descrColNo);
readStream.ReadL(description, view.ColLength(descrColNo));
readStream.Close();
CleanupStack::Pop(); //readStream
TTime aCreateDate;
TTime aModifiedDate;
aCreateDate = view.ColTime(colSet->ColNo(KPasswordCreateDateCol));
aModifiedDate = view.ColTime(colSet->ColNo(KPasswordModifiedDateCol));
aCreateDate.HomeTime();
aModifiedDate.HomeTime();
TBuf<100> aCreateDateStr;
aCreateDate.FormatL(aCreateDateStr,_L("%1%/1%2%/2%3 %-B%:0%J%:1%T%:3%:2%S%+B"));
TBuf<100> aModifiedDateStr;
aModifiedDate.FormatL(aModifiedDateStr,_L("%1%/1%2%/2%3 %-B%:0%J%:1%T%:3%:2%S%+B"));
resultArray->AppendL(aTitle);
resultArray->AppendL(aUsername);
resultArray->AppendL(aPWD);
resultArray->AppendL(description);
resultArray->AppendL(aCreateDateStr);
resultArray->AppendL(aModifiedDateStr);
}
CleanupStack::PopAndDestroy(colSet);
view.Close();
CleanupStack::Pop(resultArray);
return resultArray;
}
// ---------------------------------------------------------------------------
// CBookDb::RemoveBooks()
//
// Delete a book using title pattern and RDbUpdate (DML)
// ---------------------------------------------------------------------------
void CPasswordDb::RemovePassword(const TInt& aKeyID)
{
RDbUpdate updOp;
_LIT(KDeleteFrom, "DELETE FROM ");
_LIT(KWhere, " WHERE ");
_LIT(KLike, " LIKE '");
_LIT(KDot, "'");
_LIT(KEquel, " = ");
// Sql: DELETE FROM Books WHERE Title LIKE 'aTitle'
TBuf<KCustomSqlMaxLength> sqlStr;
sqlStr.Append(KDeleteFrom);
sqlStr.Append(KPasswordTable);
sqlStr.Append(KWhere);
sqlStr.Append(KPasswordIDCol);
sqlStr.Append(KEquel);
sqlStr.AppendNum(aKeyID);
// Initialize execution and perform the first step.
// Note: Execute() returns 0 (=KErrNone), but it does not affect database
// until Next() is called.
TInt incStat = updOp.Execute(iPasswordDb, sqlStr, EDbCompareFolded);
incStat = updOp.Next(); // This will leave, if Execute() failed.
while( incStat == 1 ) // Just in case, if the operation has more steps
{
incStat = updOp.Next();
}
//aResultCount = updOp.RowCount();
updOp.Close();
//return incStat; // KErrNone or system wide error code
}
TInt CPasswordDb::UpdatePasswordRec(const TInt aId,
const TDesC& aTitle,
const TDesC& aName,
const TDesC& aPassword,
const TDesC& aDescription)
{
TBuf<KCustomSqlMaxLength> sqlStr;
sqlStr.Format(_L("SELECT * FROM %S WHERE %S = %d"),&KPasswordTable,&KPasswordIDCol,aId);
// Create a view on the database
RDbView view;
User::LeaveIfError(
view.Prepare(iPasswordDb, TDbQuery(sqlStr), view.EUpdatable));
User::LeaveIfError(view.EvaluateAll());
// TTime hometime;
TTime aDateTime;
aDateTime.UniversalTime();
// TBuf<100> aModifiedDataStr;
// aDateTime.FormatL(aModifiedDataStr,_L("%1%/1%2%/2%3 %-B%:0%J%:1%T%:3%:2%S%+B"));
// TBuf<100> aModifiedDataStr;
// aModifiedDataStr.FormatL(hometime.HomeTime(),_L("%1%/1%2%/2%3 %-B%:0%J%:1%T%:3%:2%S%+B"));
CDbColSet* ColSet = view.ColSetL();
CleanupStack::PushL(ColSet);
User::LeaveIfError((&view)->Prepare(iPasswordDb, TDbQuery(sqlStr, EDbCompareNormal)));
User::LeaveIfError((&view)->EvaluateAll());
//view should only contain one record
if(view.CountL()!=1) return (-1);
view.FirstL();
view.GetL();
view.UpdateL(); //Update the record
view.SetColL(ColSet->ColNo(KPasswordTitleCol), aTitle); // col = 2
view.SetColL(ColSet->ColNo(KPasswordUsernameCol), aName); // col = 3
view.SetColL(ColSet->ColNo(KPasswordPWDCol), aPassword); // col = 4
view.SetColL(ColSet->ColNo(KPasswordDescriptionCol), aDescription); // col = 5
view.SetColL(ColSet->ColNo(KPasswordModifiedDateCol), aDateTime); // col = 7
view.PutL();
CleanupStack::PopAndDestroy(ColSet);
view.Close();
// CleanupStack::PopAndDestroy(ColSet);
return KErrNone;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -