You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Main/Preferences with local Database
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Open()
pref.init
dim n as integer = pref.value("Counter")
MsgBox str(n)+"nth launch."
pref.Value("Counter") = n +1
End EventHandler
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Module Pref
Const PrefFilename = "TestPref.db"
Const PrefPassword = ""
Sub Init()
dic = new Dictionary
// define in PrefFilename the name of the preferences file
dim file as FolderItem = SpecialFolder.Preferences.Child(PrefFileName)
#if RBVersion<2013 then
// in Real Studio
dim d as new REALSQLDatabase
#else
// in Xojo
dim d as new SQLiteDatabase
#endif
if PrefPassword.len>0 then // you can put in PrefPassword a password for pref file
d.EncryptionKey = PrefPassword
end if
d.DatabaseFile = file
if file.Exists then
if d.Connect then
dbv = d
ReadTable
else
dim e as string = D.ErrorMessage
break
end if
else
if d.CreateDatabaseFile then
d.SQLExecute "CREATE TABLE Pref (Key VARCHAR UNIQUE, Value VARCHAR)"
if d.Error then
dim e as string = D.ErrorMessage
Break
end if
d.Commit
dbv = d
else
dim e as string = D.ErrorMessage
break
end if
end if
End Sub
Private Sub ReadTable()
// values are cached in dictionary in memory for quick read access
#if RBVersion<2013 then
dim db as REALSQLDatabase = dbv
#else
dim db as SQLiteDatabase = dbv
#endif
dim r as RecordSet = db.SQLSelect("SELECT Key,Value FROM Pref")
if db.Error then
dim e as string = db.ErrorMessage
Break
Return
end if
while not r.EOF
dim Key as string = r.IdxField(1).StringValue
dim Value as string = r.IdxField(2).StringValue
dic.Value(key) = value
r.MoveNext
wend
End Sub
Function Value(key as string) As Variant
// you can query values here
Return dic.Lookup(key, nil)
End Function
Sub Value(key as string, assigns value as variant)
// you can set value here
#if RBVersion<2013 then
dim db as REALSQLDatabase = dbv
#else
dim db as SQLiteDatabase = dbv
#endif
dim sql as string = "INSERT OR REPLACE INTO Pref (Key, Value) VALUES (?,?)"
dim p as PreparedSQLStatement = db.Prepare(sql)
#if RBVersion<2013 then
p.BindType(0, REALSQLPreparedStatement.SQLITE_TEXT)
p.BindType(1, REALSQLPreparedStatement.SQLITE_TEXT)
#else
p.BindType(0, SQLitePreparedStatement.SQLITE_TEXT)
p.BindType(1, SQLitePreparedStatement.SQLITE_TEXT)
#endif
p.Bind(0, key)
p.Bind(1, value)
p.SQLExecute
if db.Error then
dim e as string = db.ErrorMessage
Break
Return
end if
dic.Value(key) = value
End Sub
Property Private dbv As Variant
Property Private dic As Dictionary
End Module