tags:

views:

48

answers:

2

My application crashes on reading / writing data from the database. I have one database on c: and I copy-pasted and rename with different name. The following process is what I have used for copy...Please guide me if you have any suggestion or solution.

RFs fs;
fs.Connect();

CFileMan* fileMan=CFileMan::NewL(fs);
CleanupStack::PushL(fileMan);

TInt err=fileMan->Copy(anOld,aNew);

CleanupStack::PopAndDestroy(fileMan);
fs.Close();

if(err==KErrNone)
return ETrue;
else
return EFalse;

It crashes on following line when I am trying to insert or get any data from the database.

User::LeaveIfError( iDatabase.Execute( strSQL ) );

db creation:

TBool Open = OpenL();

if (!Open)
{

User::LeaveIfError(iDbSession.Connect());

CleanupClosePushL(iDbSession);
CleanupClosePushL(iDatabase);

User::LeaveIfError(iDatabase.Replace(iDbSession, iDBPath ));

// create table
_LIT(KSQLtest,"CREATE TABLE testtable(id INTEGER,test1 VARCHAR(50),test2 VARCHAR(50))"); User::LeaveIfError(iDatabase.Execute(KSQLtest));

iDatabase.Compact();
iDatabase.Close();
iDbSession.Close();
CleanupStack::PopAndDestroy();
CleanupStack::PopAndDestroy();

Open database:

User::LeaveIfError( iDbSession.Connect() );

CleanupClosePushL( iDbSession );

if ( KErrNone != iDatabase.Open(iDbSession, iDBPath))
{
iDbSession.Close();
CleanupStack::PopAndDestroy();
return EFalse;
}
else
{
CleanupClosePushL( iDatabase );
iIsDatabaseOpened = ETrue;
return ETrue;
}
+1  A: 

User:: LeaveIfError() throws an exception when iDatabase.Execute() returns an error code.

You can find the most common Symbian error codes at NewLC

If the crash happens before RDbDatabase::Execute() is actually run, we'll need to see more code to figure out why iDatabase is in a bad state.

QuickRecipesOnSymbianOS
A: 

You need to explain what "crashes" means - an exception/leave? a panic? If so, what leave code, or what panic category and number?

If it "crashes" here

User::LeaveIfError( iDatabase.Execute( strSQL ) );

you might want to check the return value, i.e.

TInt error = iDatabase.Execute( strSQL );
//Now log/display the error
User::LeaveIfError(error);

A few other points of note:

  • If you use CleanupClosePushL() on an object, you don't need to call both Close() and CleanupStack::PopAndDestroy(). The latter will call Close() for you.
  • Your OpenL() function uses a mix of leaving and return code which is considered bad style generally. In addition, functions which leave something on the cleanup stack are generally named xxxxLC(), the trailing 'C' denoting a cleanup item.
KevinD