Create db File in Application Folder

Looking through all the examples in the Files folder…
I want to create an SQLite database file in the same folder as the app,
(which is deal for a portable app.)

I found this example which gives me the application folder on my Z: drive

Dim f As New FolderItem MsgBox(f.NativePath)

Then using the create SQLite db code I found in the example SQLiteExample app.

Dim dbFile As FolderItem = SpecialFolder.Desktop.Child("example.sqlite")

I tried to combine the two but seem to be missing something.

Dim app_folder As New FolderItem Dim dbFile As FolderItem = (app_folder,nativePath) + "\\test.db"

As this isn’t working.

not a reccommended scenario… unless this is for your own personal use…

  1. this line: app_folder,nativePath. Has a comma instead of the required dot
  2. I don’t see where in your example you get the Application folder. The desktop, but not App
  3. just getting the path to your proposed DB is not going to create your DB. ( see he examples)
  4. as Dave said, this is not a good idea. You’re going to have trouble writing to the App Folder on some systems

He said it is for a portable app. So, a database file in the same folder as the app it is a perfectly reccommended scenario.

Dim dbFile As FolderItem
dbFile = GetFolderItem(“BaDa.db”)

That makes no sense. It is really easy, just use:

Dim dbFile As FolderItem
dbFile = GetFolderItem("test.db")

GetFolderItem takes as parameter the name of a document if it is in the current directory.

[quote=419770:@Pedro Ivan Tellez Corella]He said it is for a portable app. So, a database file in the same folder as the app it is a perfectly reccommended scenario.
[/quote]

Only if the user is going to run everything with admin privileges and does not give a dam about his data.

besides all apps are “portable”… as portable as the computer they are installed on.

unless it is stand-alone on a thumb-drive, in which case the app is not in APPLICATIONS FOLDER anyways

it DOES NOT says APPLICATIONS FOLDER. Also he is talking about a Z: Drive

if you can’t create something reliable in this scenario, it is just about your level of expertice, not something that cant be done. Any solution is about the proyect requirements.

First… let me thank you guys for chipping in here with your replies.

Yes. This is an app that I alone would be using.
My routine for collecting JSON data works well.
I am now trying to collect this data in a SQLite database.
I was hoping to have the source, exe, and db file in the same folder, for easy backup.
(Using Windows: ALL my work and critical data is stored on Z: drive.)

This code works nicely, (without error checking), but puts the db file on my desktop.
For now, I can just make sure my backup routine includes this db file from desktop.

Dim dbFile As FolderItem = SpecialFolder.Desktop.Child("test2.db") App.DB = New SQLiteDatabase App.DB.DatabaseFile = dbFile If App.DB.CreateDatabaseFile Then MsgBox("db Created")

PS: example code (below) from the Language Reference retrieves the full path to the app folder but as a string.
If the NativePath function can be used in the (above) Dim dbFile FolderItem definition, I’m still interested.

Dim f As New FolderItem MsgBox(f.NativePath)

[quote=419761:@paul townsend]

I tried to combine the two but seem to be missing something.

Dim app_folder As New FolderItem Dim dbFile As FolderItem = (app_folder,nativePath) + "\\test.db"

As this isn’t working.[/quote]

Have you considered following?

Dim app_folder As New FolderItem Dim dbFile As FolderItem = app_folder.Child("test.db")

If you insist on putting in in the same folder as the application you could try this

Dim dbFile As FolderItem = app.ExecutableFile.parent.Child("test.db")

HOWEVER, be aware the this (and most other solutions) will give a different location depending on if it is running in the IDE (debug) or running as a compiled installed app. Since the location of the executable changes between those two modes.

[quote=419780:@Pedro Ivan Tellez Corella]
if you can’t create something reliable in this scenario, it is just about your level of expertice, not something that cant be done. Any solution is about the proyect requirements.[/quote]

Now I have no idea of what your experience is nor do I care. But just because I can do something, does not mean that I will do it unless it is in line with best practices. And for me a solution is not something that requires users to have special privileges in order to run it. Now it is definitely possible to design portable solutions that meet normal security requirements, I have done so for secure banking environments over the past 30 years. And that is where I’m going to leave it.

You guys are great! Paul, Dave & Pedro! Good suggestions. Syntax compiled fine.
But… the db file was not created in the application folder.

Though… experimenting with your code, I did find the perfect solution.

Dim dbFile As FolderItem dbFile = GetSaveFolderItem("", "test7.db") App.DB = New SQLiteDatabase App.DB.DatabaseFile = dbFile If App.DB.CreateDatabaseFile Then MsgBox("db Created")
This way, I or a potential user, can place the db file anywhere they choose.

Also… important points from Dave, Roger & James, about best practices. Thanks!! I’ve spent a lot of time with Visual Basic and LiveCode. My biggest challenge will be getting my head around all these new OOP concepts. It may take a while, but I’ll get there. Got a hold of an old Realbasic book AND found a wealth of video tutorials. http://developer.xojo.com/videos

PS: Happy New Year! Looking forward to a fun year with Xojo…