Cannot open SQLiteDatabase

Funny, I thought I suggested that you not do that.

Well, I tried all three versions. No success with any.
In the Open event, I had db = new SQLiteDB. Would you write the same in the Properties window, or can this be left open?
Also there was no other “Var db as SQLDB” in my code.

I now have db = new SQLiteDB in the App.open.
In the first lines of the module, which is called from the first line in window1 by Module1.SelectDbase I have:

Var dbFile As FolderItem
Var sqliteType As New FileType
sqliteType.Name = “sqlite”
sqliteType.Extensions = “sqlite”
db.Connect
dbFile = Folderitem.ShowOpenFileDialog(sqliteType)

and it stops here unable to process db…

  1. You add db (or whatever name) to the App as a property. Right-mouse on App in the left-most pane of the IDE, choose Add Property.

  2. You initialise db in app.open by:

    db = new SqliteDatabase

  3. elsewhere, you refer to it as app.db.

  4. Do NOT ‘var’ it anywhere.

Have you looked at the sample applications? Especially, Eddies Electronics for desktop should show you how to work with SQLite databases.

Exchanged all db by app.db.

In the Module I could select db-file, but
columns = app.db.Tablecolumns(“table1”) and columns.rowCount
could not be found with the same code that works in window1, when complete code is left there and either when complete code is in the module.

you db.connect before getting a file reference to a sqlite file.

Either:
a. go back to the LR and take the examples to do what you need,

b. share the simple project, so people can check it instead of “guessing” an answer.

In a message above I noted something strange; you have many times:

Property:DB As SQLDB
Global

This is not good.

Example from the LR (note where the db file is set to th sqlite reference):
Var db As New SQLiteDatabase
db.DatabaseFile = New FolderItem(“db.sqlite”)
db.EncryptionKey = “howdy+doody”
Try
db.Connect
Catch error As IOException
// handle error here
MessageBox(error.Message)
End Try

That LR page is here:
http://documentation.xojo.com/api/databases/sqlitedatabase.html

Oh yes, all the Database related code. Is there one I missed about using modules?

Thank you!
Here is the extract of my code, which is meant to fetch entries from a dictionary database (strTranslation in the end)
This code is entered into a module and would be called by “SelectOpenDB” from within window1. Property DB is global, Type SQLiteDatabase (just noticed that the code is contained in the Property window, too).

'Module SelectOpenDB
'in App.open: db = new SqliteDatabase

Var dbFile As FolderItem
Var sqliteType As New FileType
sqliteType.Name = “sqlite”
sqliteType.Extensions = “sqlite”

dbFile = Folderitem.ShowOpenFileDialog(sqliteType)

If dbFile <> Nil Then
db.DatabaseFile = dbFile
Try
db.Connect
MessageBox("Connected to " + dbFile.Name)
Catch error As DatabaseException
MessageBox("Error: " + error.Message)
End Try
End If

'All Columns of table
var colNo as Integer
Var columns As RowSet
var erg As Variant
var cn(10) as variant
var i as Integer
columns = db.TableColumns(“Lexeme”)
colNo = columns.RowCount 'Number of Rows

columns.Close

Var tables As RowSet
tables = db.Tables
var c as Integer

Try 'Names of Tables s
For Each row As DatabaseRow In tables
MessageBox(row.ColumnAt(0).StringValue + " " + str(c))
c = c + 1
Next
tables.Close
Catch error As NilObjectException
MessageBox(“This database has no tables.”)
End Try

MessageBox("Connected to " + dbFile.Name)

'3 different SQL patterns
var sql1, sql2, sql3, sql as String
'var theresult As String

sql1 = “SELECT (lexem || ’ # ’ || deutsch) as theresult FROM Lexeme WHERE ID < 10”
sql2 = “SELECT MAX(ID) FROM Lexeme”
sql3 = “SELECT * FROM Lexeme WHERE ID = 10”

sql = sql1

Var rowsFound As RowSet
var maxid As integer

db.Connect
db.ExecuteSQL(“BEGIN TRANSACTION”)

rowsFound = db.SelectSQL(sql)
maxid = rowsFound.Columnat(0).StringValue.ToInteger 'Maximal rows/ID
maxid = maxid - 2

var strTranslation(3) As String
Try
rowsfound = DB.SelectSQL(sql)
strTranslation(0) = rowsfound.Columnat(0).StringValue
strTranslation(1) = rowsfound.Columnat(1).StringValue
strTranslation(2) = rowsfound.Columnat(2).StringValue
Catch error As DatabaseException
MsgBox("DB Error: " + error.Message)
End Try

Sorry again for taking your time!!

DON’T USE THIS CODE!

You need to use a property and not a variable. Don’t do a var with your SQLiteDatabase anywhere.

Sorry, was a leftover in another copy. Was replaced by
db = new SqliteDatabase in App.open
However, changing this did not help.

Beatrix, I will tdo my best to avoid. But when reading the web-documentation exactly this “Var db As New SQLiteDatabase” quite often occurs in “Sample Code” even with existing databases (!).

Please make us an example.

For example:
Some of the samples in SQLiteDatabase.DatabaseFile, also Database.connect Sample SQLite.
I got most of my database code from this documentation.

And also, when I removed the VAR db as NEW from the code I was discussing today, an error resulted at the first occurrende of db.connect (see #29).

No, you do not do that, exactly.

db.DatabaseFile

must appear before

db.Connect

for example.

?? :roll_eyes:

Sorry, I add an explanation above.

Do you mean in #22? That was wrong indeed. :pensive:
I’ll check the other places, too.

Isn’t

that the case with my code?
I’m sorry, but I used the sample for my code (Selecting and opening the db-file), including the Var db as new ……for the existing database :blush:

Sorry, I didn’t see you’re new here. I came home, and am calm now (no nise herre, I was at McDonald’s before).

Are you okay now?