Hi I am a new user and am working with some of the example code. When attempting a simple Database insert using SQL I keep getting a Database Closed error message. I’ve also tried the Insert Database Record example and get the same error. The database is in the same folder as the project and opens and updates perfectly via SQlite Browser. I am following the example code pretty closely so don’t understand why I am getting this error. Any help would be great as I want to move onto coding in greater detail with Xojo, thanks.
Dim dbFile As FolderItem
Dim db As New SQLiteDatabase
dbFile = GetFolderItem(“C:\…\dbase.sqlite”) //exact path to sqlite file
db.DatabaseFile = dbFile
db.SQLExecute (“INSERT INTO Things (Number, Last, First) VALUES “+”(‘Dr.Strangelove’,‘Advisor’,1962)”)
If db.Error then
MsgBox("Error: " + db.ErrorMessage)
Am using the latest download of the IDE: 2014 R3.1 on Windows 8.1 Pro.
You never connected to it in the first place.
db.DatabaseFile = dbFile
if db.connect = false then
msgbox "Never connected to the db!"
db.SQLExecute ("INSERT INTO Things (Number, Last, First) VALUES "+"('Dr.Strangelove','Advisor',1962)")
BTW, this bit: dbFile = GetFolderItem(“C:\…\dbase.sqlite”) //exact path to sqlite file is not cross platform. You’ll want to look up more info on FolderItems and how to access.
If it’s in the same folder as the executable you can do this:
dbfile = GetFolderItem("dbase.sqlite")
If it’s in the resources directory you can do:
dbfile = GetFolderItem("Resources").child("dbase.sqlite")
If it’s on the desktop you could do this:
dbFile = SpecialFolder.Desktop("dbase.sqlite")
AND THEN you should probably do a check to make sure it exists first.
If dbFile.Exists = false then
mSgbox "DBFile could not be found."
The example docs don’t do a lot of error checking so they don’t confuse you but it is the next step in your journey.
If video training is your thing you can take a look at our Xojo training videos at http://xojo.bkeeney.com/XojoTraining/. Over 200 videos, 62+ hours, and most of them come with the source that you can use in your own projects.
Where did you get the example? Sounds like it needs to be updated.
#Bob, boy don’t I feel like an idiot. Thanks for pointing that out. Getting just bleary eyed looking at the code. I’ll definitely check out your training resources.
#Tim, the example was from the Language Reference Wiki on SQLite and the db.Connect check is in there…
Thanks I’ll be more careful next time.
No worries, it happens. Tim and I probably have a combined 30 years using Xojo so the issue jumped out at us.
Xojo is a big framework and it takes a while to figure it out. Doing things step by step for a while is the best way of going about learning it. If you run into issues take a step back and maybe create a small sample app to learn about a subject (which is how a lot of my early training videos came into being).
Ask questions on the forums. This is a (mostly) friendly group that’s willing to help but don’t forget to search for an answer first because a lot of issues have been asked multiple times.