GetSaveFolderItem not replacing database file?

Hi all,

I have the problem that I want to create a database file, however when the file already exists and I’m asked in the GetSaveFolderItem if I want to replace it then it is NOT being replaced when I choose replace.

Any idea why not?

The code is

[code] dim d as new Date

dim db as new REALSQLDatabase

db.DatabaseFile = GetSaveFolderItem( FileTypes_Database.Digest, "Digest " + d.SQLDate )

if db.DatabaseFile = Nil then // user cancelled

// do nothing 
Return False

else

// CreateDatabaseFile
// If the database already exists, this function works like Database.Connect. 
// CreateDatabaseFile returns True if the new database was created successfully or existing database was opened successful and False if otherwise.

If db.CreateDatabaseFile = false Then  // Error While Creating the Database
  
  MsgBox "Creating the Digest file failed." + EndOfLine + EndOfLine + "Database Error:  " + Str( db.ErrorCode ) + EndOfLine + EndOfLine + db.ErrorMessage
  
  Return False
  
else  // ok, it exists so lets connect to it 
  
  if db.Connect then[/code]

The code we need to see to help with this is the “CreateDatabaseFile” Function

Perhaps you misunderstood the LR entry. If the file exists, it is not removed, it is simply connected to. In other words, CreateDatabaseFile can be called against an existing database without harming it. What you want is the opposite of how it is designed. You want to remove the file if it exists. You have to code that yourself.

if db.DatabaseFile.Exists then db.DatabaseFile.Delete
if db.CreateDatabaseFile then ...

Also note that GetSaveFolderItem does not delete the file. It just gets permission for you to delete it. It’s up to you how you want do proceed - delete it, move it to the trash, archive it, whatever.

That is a build-in function, see http://documentation.xojo.com/index.php/REALSQLdatabase.CreateDataBaseFile

@Tim: the system is called with GetSaveFolderItem (which is creating a new file) and is asking if it should replace the file, not the db.CreateDatabaseFile funtion. It pops up a dialog and asks if I want to cancel or replace the file. I choose replace but the replace does not happen, instead when the code gets to db.CreateDatabaseFile it connects to the file.

As you see from the comments in the code I’m aware of how db.CreateDatabaseFile works.

That is correct as Tim pointed out:

Do you mean that the confirmation message pops up in the save dialog message ?

If that is the case, afterwards db.DatabaseFile is not nil. If the user cancels, it returns nil.

So right before // CreateDatabaseFile, verify db.Database exists, and if so delete it. Then proceed as before. The code will create the file, since the previous one is deleted.

So basically the system asks me if I want to replace the file (standard file dialog practise) but does nothing when I press replace? So I have to deal with the choice myself?

somewhat confusing.

Thanks for the help, guys.

Think of it as giving the user a second chance to cancel if the file exists before returning to the program. That was anytime a program uses the API the user gets a consistent experience because we do not have to remember to ask if the user is sure.

Usually, the system does replace the file. Seems in this particular case, you may have to help it a bit…