|Full tutorial of precompiled SQLite database standalone on android device|
|Débuté par ELIJAH ( Andersons P, 06 mai 2012 11:56 - Aucune réponse|
Posté le 06 mai 2012 - 11:56
|Ok, some of you might be "well that is common sense", well not for this guy, and because i couldn't find a tutorial like this, i decided to write one so no one else has to learn the hard way. In my last tutorial we converted HFSQL connection to SQLite BUT we had to pragmatically enter the data in the Pglobal file. NOT ANY MORE!!!!!! here EXACTLY what i did to get it to work(without all my embarrassing errors)|
First off, the program i use to create my SQLite databases, as well as graphically load the fields with data is SQLite Database Browser 2.1(this is freeware)
So i have created my database, i have named it "database"
1. Create your android project
2. Go through all the settings and click NO for everything. If you must. do what ever you want EXCEPT LOAD A DATABASE
3. create a blank window
4. Go to your analysis and create a new one. Make sure it is set up to your database we just created.
5. Create a looper in your window, link it to what ever table you want in the database
6. Create a new set of global procedures. Save it as Pglobal
7. in your project explorer highlight pglobal, then click create/new procedure
8. NAME THIS PROCEDURE fileconnection()
9. Place the following code in the fileconnection() mobule
IF InAndroidMode() THEN
IF HDescribeConnection(Myconnection,"","",CompleteDir(fDataDir()) + "database",""hAccessSQLiteAndroid,h0ReadWrite) = False THEN
Error("unable to connect", HErrorInfo(hErrMessage))
IF HChangeConnection("", myconnection) = False THEN
Error("unable to change connection", HErrorInfo(hErrMessage))
In the module for your app, this will be a module with the exact name as your app. Once you are their enter the following code
fExtractResource("database",CompleteDir(fDataDir()) + "database")
The last process is to compile your project, just remember to add the database file into the project through the "integrated files" window while compiling.
ok, now that you have gotten all of that in your modules i will hit on the details that snagged me up in the process.
-Windev says that you do not have to specify a path in the connection options when you extract your database. For me, i have found that untrue. That is why i used the exact same path i used for the extraction that i used in the Hdescribe method.
-All of the windev tutorials have the databases with a .db extension. This really messed me up because i am new to alot of things. YOUR DATABASE WILL NOT ALWAYS HAVE AN .DB EXTENSION FOR SQLITE!!!!!!!!
-Digging deeper into the above statement, you will see that i did not place an extension on any of the database identifiers. i just put "database". this is because using the SQLite database browser, the software saves both a .db and a file without the extension. Turns out that the file with no extension IS the database. that held me up for a while.
So hot points to remember here, use exact file names including extensions for the database. Note that your database will not always be .sqlite, .db..... it might not even have an extension. Also, just to be same, ALWAYS use paths, even when declaring the file on a connection.
it is best practice to use
this is because it will work on all android devices, remember if you specify the path /sdcard/database it will not always work because some devices use /mnt/sdcard