Okay, I’m sure I’m missing something really, really basic here, but here goes…
I made a SQLiteDatabase for my Web app but for some reason (again, probably something obvious) I cannot get it to connect. Right now I’m just using the example code given in the Xojo docs to try to attempt to connect, but when I run the Web program I get “Database file doesn’t exist in the specified location.”
The database is in the same folder as the app. I tried making a test.sqlite database from within Xojo just to see if that was the issue, and it throws up the same error.
This is the code I’m using - It’s straight from the Xojo docs. It’s currently in the Session Open Event Handler.
Var db As New SQLiteDatabase
db.DatabaseFile = New FolderItem("test.sqlite")
Try
db.Connect
// proceed with database operations here..
Catch error As DatabaseException
MessageBox("The database couldn't be opened. Error: " + error.Message)
Return
End Try
Thank you for the help (and yes, debug mode). That throws up a Syntax error on:
db.DatabaseFile = New Folderitem("").Parent.Child("test.sqlite")
I’m sure I’m doing something incredibly stupid, but this is happening even in an empty app with just the database added in, but in desktop and web app.
I have the app and the database both in the same folder.
Maybe I need to go back to the example projects and figure out where I went wrong.
if DebugBuild then
db.DatabaseFile = New Folderitem("")
db.DatabaseFile = db.DatabaseFile.Parent.Child("test.sqlite")
else
db.DatabaseFile = New FolderItem("test.sqlite")
end
Again, untested, but syntax should be correct.
Alternately, you could run the program via Project -> Run Paused and copy the database file into the debug directory before resuming.
If db.DatabaseFile.Exists Then ’ Connect to the existing database
If db.Connect = False Then
// Couldn’t connect to the database file
Break
End If
Else ’ Create a new database
If db.CreateDatabase = False Then
// Couldn’t create the database file
Break
End If
// Use DDL (Data Definition Language) to create your tables & views here.
Golden Rule:
NEVER keep a file you want to update ‘next to’ the app.
1/ Thats not the same location for debug/release
2/ Somebody might move your app but not the data
3/ Most OS won’t let you write to a file in the program directory without very elevated permissions.
Create a folder for your app in a ‘good’ writable location
Specialfolder.ApplicationData is best, but Specialfolder.documents is also a possibility.
If you do that, your app will always be able to find the data, and it will be able to amend it.
Thank you, everyone. The solution ended up being to move the database to the Application Support folder and calling it through SpecialFolder.ApplicationData.Child
I also had a couple methods that were in the wrong place.
This is my first crack at a web app, and some of the ways it works differently are throwing me. I learned a few things here, but mostly I learned not to code when I’m also grading a 9 weeks worth of late assignments.