How to close SQLite Connection?

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

User avatar
Rathinagiri
Posts: 5385
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 235 times
Been thanked: 285 times
Contact:

How to close SQLite Connection?

Post 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?
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

User avatar
apais
Posts: 419
Joined: Fri Aug 01, 2008 6:03 pm
Location: uruguay
Has thanked: 90 times
Been thanked: 85 times
Contact:

Post by apais »

An EXIT PROCEDURE would fullfill ???
Angel Pais
Web Apps consultant/architect/developper.
HW_apache (webserver modules) co-developer.
HbTron (Html GUI for harbour desktop hybrid apps) co-developer.
https://www.hbtron.com

User avatar
Rathinagiri
Posts: 5385
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 235 times
Been thanked: 285 times
Contact:

Post 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.
Attachments
Clipboard01.jpg
Clipboard01.jpg (11.94 KiB) Viewed 5377 times
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Has thanked: 411 times
Been thanked: 120 times
Contact:

Post by esgici »

Hi Rathi

Does this function not served to your purpose ?

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

Regards

--

Esgici
Viva INTERNATIONAL HMG :D

User avatar
Rathinagiri
Posts: 5385
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 235 times
Been thanked: 285 times
Contact:

Post 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.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

User avatar
bpd2000
Posts: 1207
Joined: Sat Sep 10, 2011 4:07 am
Location: India
Has thanked: 291 times
Been thanked: 167 times

Post by bpd2000 »

Dear Rathinagiri,

Working example, please
BPD
Convert Dream into Reality through HMG

User avatar
mol
Posts: 3533
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Has thanked: 292 times
Been thanked: 212 times
Contact:

Post 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.

mrduck
Posts: 497
Joined: Fri Sep 10, 2010 5:22 pm

Post 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...

User avatar
Rathinagiri
Posts: 5385
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 235 times
Been thanked: 285 times
Contact:

Post 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?
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

User avatar
apais
Posts: 419
Joined: Fri Aug 01, 2008 6:03 pm
Location: uruguay
Has thanked: 90 times
Been thanked: 85 times
Contact:

Post 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
Angel Pais
Web Apps consultant/architect/developper.
HW_apache (webserver modules) co-developer.
HbTron (Html GUI for harbour desktop hybrid apps) co-developer.
https://www.hbtron.com

Post Reply