Database does not exist error when executing appication

My application is running fine from within the IDE. I’m using SQLite for the database. After ‘building’ the application, when I double click the exe, i’m getting the error:

Database does not exit. This application will now close - note: ‘exit’ is the exact spelling. i believe it should be ‘exist’.

After clicking Okay i get the following:

Database error: 0
Operation cannot be completed because the database is closed.

Then the application opens. But trying to get data results in the Database error: 0 again.

what have i done wrong?

It sounds like the database you’re trying to access is not being referenced correctly by your getfolderitem statement - if no path has been given then just make sure your database is in the same folder as the app build or wherever you’ve put it or put in an absolute path like getfolderitem("G:\myDb…) then the app know where to expect the db. Also you need to connect to open the db

if db.connect()=false then
      msgbox "Can't connect - to database "
    end if

after the getfolderitem statement will stop the app crashing…maybe use a dialogue to locate the db instead.

Just a thought…

Here’s my current code:
[code // Set Database File
#if DebugBuild then
rakDataDB.databaseFile = GetFolderItem("").Parent.Child(“rakTestData.db”)
#else
rakDataDB.databaseFile = GetFolderItem(“rakTestData.db”)
#endif

// Connect to the database
If rakDataDB.databaseFile.exists = True then
// The database file already exists, so we want to connect to it.
If rakDataDB.Connect() = False then
// there was an error connecting to the database
Beep
MsgBox "Database Error: " + Str(rakDataDB.ErrorCode) + EndOfLine + _
EndOfLine + rakDataDB.ErrorMessage
Quit
Return
End if
Else
// The database file does not exist so we want to create a new one.
// The process of creating a database will establish a connection to it
// so there isn’t a need to call Database.Connect after we create it.
MsgBox “Databse does not exit. The application will now close.”
WindowMain.Close()
End if][/code]

[quote=19726:@Robert Kamarowski]MsgBox “Databse does not exit. The application will now close.”
WindowMain.Close()[/quote]
This the part of you code that is displaying your message. Which indicates that the DB file does not exist. Perhaps you should also call “Quit” here rather than closing WindowMain like that?

Paul, the Quit worked, but now I can’t build the applicaiton:

I can run the application from the IDE. Analyze project is the same (no problems). But I’m getting errors when attempting to build.

I found the build problems and fixed them. But i still can’t run the exe file.

Why not? Same error? Different error? Something else entirely?

Your original error occurred because the DB file was not found. Have you verified that it is found when the build app runs?

Same error. The application runs fine under the IDE. I have no idea why it doesn’t run from the exe created by the ‘Build’.

And the database is right next to the exe ?

No. The database file is not being created. I thought an sqlite database would be included in the exe?

I’m curious what makes you expect that there would there be one ?

Your code to open it is looking for a file next to the application

#if DebugBuild then rakDataDB.databaseFile = GetFolderItem("").Parent.Child("rakTestData.db") #else rakDataDB.databaseFile = GetFolderItem("rakTestData.db") #endif
You either need to create one or have one already there when you run your application.

uhhh… I understand now. Thank you.

You can adjust your code to create the database if its missing

// Set Database File
#if DebugBuild then
   rakDataDB.databaseFile = GetFolderItem("").Parent.Child("rakTestData.db")
#else
   rakDataDB.databaseFile = GetFolderItem("rakTestData.db")
#endif

If rakDataDB.databaseFile is nil then
   // you have a big problem as this should not happen
   // deal with the error
   MsgBox "Not sure what's wrong with locating the database. The application will now close."
   Quit
end if

// Connect to the database
If rakDataDB.databaseFile.exists = True then
   // The database file already exists, so we want to connect to it.
   If rakDataDB.Connect() = False then
      // there was an error connecting to the database
      Beep
      MsgBox "Database Error: " + Str(rakDataDB.ErrorCode) + EndOfLine + _
      EndOfLine + rakDataDB.ErrorMessage
      Quit
      Return
   End if
Else
// The database file does not exist so we want to create a new one.
// The process of creating a database will establish a connection to it
// so there isn't a need to call Database.Connect after we create it.
   if not rakDataDB.CreateDatabase then
        // oh oh we cant even create it !
       MsgBox "Can't create database. The application will now close."
       Quit
    end if

   // ok we created the database file now create the tables etc    
   // THIS IS SOMETHING YOU HAVE TO WRITE !!!!!

End if

The database has been created. When I moved it to the directory with the executable it worked. I thought sqlite was an embedded database, meaning it was included in the exe.

99.9% of the time they need to be readable & writeable so we cannot put it in the exe as the OS and virus checkers would prevent that or flag it as virus-like behaviour

thank you.

SQLite is an embedded database. But embedded means the database engine is embedded in the app, not the database file.

In this day and age, apps should not be modified by themselves. OS permissions would likely prevent that, but even if they didn’t it would likely trigger virus scanners as Norman mentioned.

You should store the database file elsewhere. Your own folder within SpecialFolder.ApplicationData is usually a safe place.

Thank you Paul.