Dim dbFile As FolderItem
Dim db As New SQLiteDatabase
dbFile = GetFolderItem(“data.sqlite”)
db.DatabaseFile = dbFile
If db.Connect Then
//proceed with database operations here…
Else
MsgBox("The database couldn’t be opened. Error: " + db.ErrorMessage)
End If
But when M running it is showing error database Not found
please help
dbFile = GetFolderItem("data.sqlite") expects the file data.sqlite in your working directory. You checked if it’s there ? It can be tricky in debug-mode. Think it would be better to specify the folder based on the root.
yes its ther[quote=264961:@Joost Rongen]dbFile = GetFolderItem("data.sqlite") expects the file data.sqlite in your working directory. You checked if it’s there ? It can be tricky in debug-mode. Think it would be better to specify the folder based on the root.[/quote]
yes sir my file is there but when i copy my data base file to debug folder then it works ,so please tell how to specify the folder
This function gives you a folderitem to a file named as you project-name, but with the given file-extension. In debug mode it expects the file to exist in your working folder, for the build application you can simply specify if it should exist in the same folder as you exe / app or in the resource-folder. (Resourcefolder = True)
[code]Function ResourceFile(FileExt as string, ResourceFolder as Boolean) As FolderItem
// ------------------------------------------------------------------------------------------------
// Function: ResourceFile
// ------------------------------------------------------------------------------------------------
// Parameters:
// FileExt - the resourcefile wil have a name equal to the main app, but with
// the file-extension as given here, for example ‘*.isn_qry’
// ResourceFolder - false ==> file is supposed to be found in the same folder as
// the app or exe exists
// - true ==> file is supposed to be found in the resource-folder of the
// build applicaiton.
// - in debugmode the file should exist in the same folder as the
// xojo - projectfile
//
// Return: - folderitem represents the file, having the same name as the app or exe,
// but with the given extension
// ------------------------------------------------------------------------------------------------
dim f1 as FolderItem = App.ExecutableFile
dim strAppName as string
// ----------------------------------------------------------------------------
// OSX
// ----------------------------------------------------------------------------
#if TargetMacOS then
strAppName = ReplaceAll(f1.Name, "Debug", "")
strAppName = ReplaceAll(strAppName, ".app", "")
if right(strAppName, 1) = "." then strAppName = left(strAppName, len(strAppName) - 1)
#if DebugBuild then
f1 = f1.Parent.Parent.Parent.Parent
f1 = f1.child(strAppName + "." + FileExt)
#else
if ResourceFolder then
f1 = f1.Parent.Parent
f1 = f1.child("Resources")
f1 = f1.child(strAppName + "." + FileExt)
else
f1 = f1.Parent.Parent.Parent.Parent
f1 = f1.child(strAppName + "." + FileExt)
end if
#endif
// ----------------------------------------------------------------------------
// Windows
// ----------------------------------------------------------------------------
#elseif TargetWindows then
strAppName = ReplaceAll(f1.Name, "Debug", "")
strAppName = ReplaceAll(strAppName, ".exe", "")
#if DebugBuild then
f1 = f1.Parent.Parent
f1 = f1.child(strAppName + "." + FileExt)
#else
f1 = f1.Parent
if ResourceFolder then
f1 = f1.child(strAppName + " Resources\" + strAppName + "." + FileExt)
else
f1 = f1.child(strAppName + "." + FileExt)
end if
#endif
// ----------------------------------------------------------------------------
// Linux
// ----------------------------------------------------------------------------
#elseif TargetLinux then
strAppName = ReplaceAll(f1.Name, "Debug", "")
strAppName = ReplaceAll(strAppName, ".exe", "")
#if DebugBuild then
f1 = f1.Parent.Parent
f1 = f1.child(strAppName + "." + FileExt)
#else
f1 = f1.Parent
if ResourceFolder then
f1 = f1.child(strAppName + " Resources/" + strAppName + "." + FileExt)
else
f1 = f1.child(strAppName + "." + FileExt)
end if
#endif
// ----------------------------------------------------------------------------
#Endif
Return f1
End Function[/code]
Create a CopyFile step to copy the db file to the debug/build folder when run/build.
Right click in the navigator. Build step > Copy Files.
Add the db file to the list.
Move the build step down to “Build Settings” and put it below the step called “Build”.
I get the db like this in windows. I create the db as an app property called myDB. Then I create a global property called strAppPath.
I get the database path from a method like this:
[code]
’ Set application path
if DebugBuild Then
strAppPath = App.ExecutableFile.Parent.Parent.AbsolutePath
else
strAppPath = App.ExecutableFile.Parent.AbsolutePath
end
// Create database object
// myDB is a property of the App class
app.myDB = New SQLiteDatabase
// sets its databasefile property to the file…
app.myDB.databaseFile = GetFolderItem(strAppPath +“dbName.rsd”)
return app.myDB.databaseFile[/code]
I use it like this in a query method:
[code]
’ Get the settings from the db
Dim dbFile as FolderItem
Dim db as SQLiteDatabase