Page 1 of 3

How to close SQLite Connection?

Posted: Tue Jan 31, 2012 8:46 am
by Rathinagiri
Hi,

I am using HBSQLite for an application.

At the end of the application, I wish to encrypt the database and delete the original database file. I could not delete the file from inside the application because, the file is in use.

Actually when the application exits the database file is also closed automatically.

How to close this file either automatically or forcefully? Or, can I run a module just after the closure of the database and before closure of the application? (SQLite3_close() wrapper function is called via HB_Garbage function.)

Can anybody help me?

Re: How to close SQLite Connection?

Posted: Wed Feb 01, 2012 2:16 pm
by apais
An EXIT PROCEDURE would fullfill ???

Re: How to close SQLite Connection?

Posted: Wed Feb 01, 2012 4:05 pm
by Rathinagiri
Thanks for the reply. I tried very eagerly and got the following: :( Actually I have called the deleting porting at 'on release of main window' only.

Re: How to close SQLite Connection?

Posted: Wed Feb 01, 2012 8:12 pm
by esgici
Hi Rathi

Does this function not served to your purpose ?

http://www.php.net/manual/en/function.sqlite-close.php

Regards

--

Esgici

Re: How to close SQLite Connection?

Posted: Thu Feb 02, 2012 4:58 am
by Rathinagiri
Thanks for the reference Esgici. It is for php.

However, the respective wrapper function in harbour is sqlite3_close(). It can't be called directly. (On seeing the source code, I have found this). It is called automatically by Harbour in Garbage Collection procedure.

Francesco had thrown light on calling hb_gcall( .t. ), which simulates garbage collection (like cleaning up the unneeded things in the memory). Just now I have checked this and it works just as I expected.

hb_gcall( .t. )
frease( cDB )

This worked. Thanks to all. This helped me in creating an encrypted database with password protection.

This is my way of encryption/decryption of a SQLite database kept in secret. If there is any flaw in this please guide me.

1. We have an AES encrypted database.
2. When the password is given the encrypted database is decrypted inside a random directory which is created at the runtime.
3. All operations are done to this decrypted database.
4. Once the application is closed, the database is encrypted again to the original location.
5. Now, the decrypted database is deleted and the random directory is removed from the system.

Re: How to close SQLite Connection?

Posted: Thu Feb 02, 2012 6:26 am
by bpd2000
Dear Rathinagiri,

Working example, please

Re: How to close SQLite Connection?

Posted: Thu Feb 02, 2012 6:37 am
by mol
But... When you reset computer or close your program by task manager, your database will stay decrypted :(
You must detect if it's encrypted or decrypted.

Re: How to close SQLite Connection?

Posted: Thu Feb 02, 2012 8:53 am
by mrduck
Hi Rathinagiri,
I think your methods is really dangerous but I need more infos on the data in the database.
If the database is "readonly", I mean just SELECTs and no INSERT or UPDATE, you just need to keep track of where the temporary db is stored so if the program/computer crashed, at the next run you go and delete the temporary archive.

Mol already told you that when decripted, everyone can read it...

If it is updated the situation is worse: what about I add record for 3 hours and then the program crashes ? So you need to keep track of where the temporary db is stored and at next run recover from it (not delete it).

I don't remember why you need to encript the db, perhaps you can encript single fields...

Re: How to close SQLite Connection?

Posted: Thu Feb 02, 2012 9:39 am
by Rathinagiri
Hi Francesco,

Really interesting situations.

I update the random directory name initially inside the database. Before application closure, I update this field to empty. If the program is crashed, this field will not be empty. So, the next time when the application is started, if there is anything in that field in the database, I will remove that directory content immediately.

After every commit, I encrypt the whole db side by side (which is silly and time consuming. But I am doing it because the database is < 20 MB and it takes less than a second to encrypt the whole db!). So, even after working for 3 hours you will not miss anything other than the last transaction.
Mol already told you that when decripted, everyone can read it...
Yes, only if they know about the name and location of the random directory created elsewhere.
I don't remember why you need to encript the db, perhaps you can encript single fields...
I want to keep the whole db secretly and I wish to keep the index on some of these fields too. We can't make a primary key field to be encrypted. Isn't it?

Re: How to close SQLite Connection?

Posted: Thu Feb 02, 2012 4:23 pm
by apais
Did you know there is an encrypted version of Sqlite for under U$S 100 ?
You pay once deploy forever. The license is PER DEVELOPPER.
It is made by the same author than sqlite.
Later will try to find the link