Hi,
Newbe here.
I am trying out to use SQLite.
So the code works with a ‘connected’ database in development mode. But I can see that is not the path to the SQLite database. So when I run the code everything works during runtime. But nothing is really stored in the database. When I enter the connection in final mode then the whole program doesn’t function it refuses and gets stuck in debugmode. The Path is correct in final mode.
I am on Ubuntu.
Thanks for clearing out.
If SQLiteDatabase1.IsConnected Then
MessageBox(“You are connected to the database.”) //ok
Else
MessageBox(“Something went wrong. You’re not connected to the database.”)
Return
End If
Var row As New DatabaseRow
Var rows As RowSet
// Set up the row with data
row.Column(“Naam”) = “Bloes”
row.Column(“Voornaam”) = “Bob Roberts”
Try
SQLiteDatabase1.BeginTransaction
SQLiteDatabase1.AddRow(“tblKlanten”, row)
SQLiteDatabase1.CommitTransaction
MessageBox(“Toegevoegd”) // ok
Catch error As DatabaseException
SQLiteDatabase1.RollbackTransaction // rollback in case of error
MessageBox("DB Error: " + error.Message)
Return
End Try
Try
rows = SQLiteDatabase1.SelectSQL(“SELECT * FROM tblKlanten;”)
If rows = Nil Then
MessageBox(“No records found.”)
Return
End If
Catch error As DatabaseException
MessageBox("DB Error: " + error.Message)
Return
End Try
Var klanten() As String
Var aantal As Integer = 0
For Each lijn As DatabaseRow In rows
Dim teamName As String = lijn.Column(“Naam”).StringValue
klanten.AddRow(teamName)
aantal = aantal + 1
MessageBox(teamName) // last added
Next
what xojo version?
From where do you take this code ?
You do not share the code that write to disk the data base ( create the SQLiteBD to disk)
db.DatabaseFile = SQLite_FI // FI: FolderItem
Try
// Create the Data Base File…
db.CreateDatabase
db.Connect
Catch error As DatabaseException
MessageBox(error.Message)
Return
End Try
I am using Xojo 2025 version 1. I provided the whole and working code. Exept it doesn’t keep the records saved to the database after runtime. The code is taken parts from the documentation about databases and slightly adapted by me for my testcase. I did not have to write code to connect to the database. I could connect by chosing the menu ‘insert’ → ‘connect database’ then I used the name of the object that was created (SQLiteDatabase1) to work in the code. You get a choise of 4 different modes on that object ( development, alpha, beta, and final). So how does that work? Of course I could write the code to connect, but what is the use for the object database connection? Is this a new feature in Xojo?
Note: it can be stored in memory, and that can explain why you post.
Look there SQLiteDatabase you will find example code that works fine to create a db,write to it, etc.
Example shared under Create:
Var db As New SQLiteDatabase
db.DatabaseFile = SpecialFolder.Desktop.Child("MyDatabase.sqlite")
Try
db.CreateDatabase
Catch error As IOException
MessageBox("The database file could not be created: " + error.Message)
End Try
If you add other code (Add TABLE, read below this one in the Documentation), then, you can Add Records that will be stored in MyDatabase.sqlite.
You may also want to read a document named SQLite Basics…
Thanks Emile for a possible solution, I will check it out. I guess that is what is happening here, in memory storing. The database feature would save us writing code if it was working or rather if I knew how to set it up. Maybe there is something I need to change in the configuration? Using a tool for creating SQLite databases get’s things moving faster too. I used SQLitely on Ubuntu - Linux. Greetings from Belgium, Rudi
You don’t appear to have declared SQLiteDatabase1, neither do you appear to have initialised it.
I would have expected at least:
Var SQLiteDatabase1 as SQLiteDatabase
SQLiteDatabase1 = new SQLiteDatabase
with, more likely, SQLiteDatabase1 being made a property of the window, so that it persists.
Then you have to actually create the permanent database on disk, which also I don’t see. And connect to it, and setting the FolderItem so that the connection refers to that file.
Hi Tim
Thanks for replying, I started of with creating the permanent database. It exists on the harddrive. Then I added it as described here:https://documentation.xojo.com/topics/databases/connecting_to_a_database.html
Then the database is available as an object in the code. So it’s normal you can’t see this in the code, Xojo would take care of that and it does, the code itself has no problems. I could make all these connections manually with code as you and Emile are suggesting but what is the use for the object ‘connect to database’ it does the same and requires no code? Or will it only work when building the application? Did you use this feature?
In my case, databases (apart from a few fixed ones) are created as the user desires (I have at least one user with 1000 such databases). Thus I have to be able to create them from scratch in code, including the fixed ones.
However for learning/testing there’s no reason not to create one separately as you have done. Persoanlly I use the SQLite CLI program (written by the SQLIte devs) to inspect a database on disk, to see its schema, and thus to verify that it has the tables and columns as expected, and to view data within it.
Thus my app(s) have no database objects within them.
Ah, here we need more detail. Do you get an exception? If so what are its message and error code? BTW, for simple logging, I’d be inclined to use system.debuglog rather than MessageBox. Doing that gives you messages in the message area, without interrupting program flow.
As means getting stuck. Force exit or wait. Apperently It had problems with access to the folder. (It didn’t show that neighter, A screenshot would not been usefull.) I changed rights to it. Now it runs on final mode too, but still in memory storage. Nothing saves to the database this way. I guess it will save to disk after a build. Can’t test that yet. When I connect through code, it works and saves. Still wondering why we have this feature? It works and then not?
I’ve never used these “stages” so I don’t really understand how one differs from another. I can only suggest using the debugger to step through the code to ensure you are arriving at the lines which should be writing to the database, and when you do, carefully examine the properties of SQLiteDatabase1 to ensure that they make sense.
I think you are mixing two things up. There’s the code way and the way with the connection to the database from the menu. I have no problem with the code way.
Hi Tim,
That was my question. Why? What is the use for it? Only for temporary data? Could not find this in the documentation. It could be more straight forward. To many ways to Rome. I come from vb and vba MS ACCESS.