Database Events file could not be overwritten

Hi,
I created a database with Applescript’s Database Events (DBEV) in which I put all the title and artist names of a iTunes’ playlist. Then I save the DB as a *.dbev file on hard disk with the ‘save’ command. So far, it works fine.
But, on every run of the script, with the same playlist, it adds new records to the end of the existing database file instead of overwriting it.
As far as I know, the ‘save’ command has no parameter which explicitly tells to overwrite the file.

Here my code:


set DBpath to "...:Desktop:"
set DBname to "myDB"

tell application "iTunes"
   set theTracks to every track of playlist "xy"
end tell

tell application "Database Events"
   launch
   make new database with properties {name:DBname, location:DBpath}
   tell database DBname
      set c1 to count of records
      repeat with oneTrack in theTracks
         tell application "iTunes"
            set theName to name of oneTrack
            set theArtist to artist of oneTrack
         end tell
         set theRec to make new record with properties {name:theName}
         tell theRec
            make new field with properties {name:artist,value:theArtist}
         end tell
      end repeat
      set c2 to count of records
   end tell
   try
      save database DBname
      close database DBname
   on error
      quit
   end try
   quit
end tell

Counter c1 is always Zero, which I expected to be.
Counter c2 adds the number of new records to its former value. Let’s say, I got 4 tracks in the playlist, on first run c2 displays ‘4’, second run it’s ‘8’, and so on.
I don’t use a property or something like that. So it means, the DB HAS GOT 4,8,12,… record entries. That’s not what I want. There should be only 4 entries, everytime. And how can it be that counter c1 always displays zero records.

What is happening here? Why doesn’t the ‘save’ command overwrite the DB file?

I work with Mac Tiger and Scripteditor and XCode 2 respectively.

Regards,
Clancy Wiggum

Hi,

I guess, creating a new database with an existing name opens the existing one and appends the records.
Check (log) the variable c1 while running the script the second time

I’m 99% positive you are seeing the expected behavior. You are working with a database and overwriting one could be problematic. If thats what you are trying to do though you could physically first delete the database file or delete all records in the db.

[Edit] Good [insert time of day] Stefan, I see you are quick to the draw as usual =)

Hi Stefan, hi James,

thanks for your replies.

You are both right. Of course, the new records are attached at the end of the DB. In order to work as I had expected, the DB has to have a “startpoint” which says: “hey, make a new record! Insert it at the beginning and overwrite the existing records!” But DB-Events can’t do it.
So, I decided to delete the *.dbev file before I create the new DB. All is fine!

But I still don’t understand, why the count of records displays Zero at the beginning of the procedure everytime. During runtime, the counter shows the right number of records. But the first display of it is always zero.

Example (I placed the counter c1 into the repeat loop before the record making):
4 tracks in playlist, two runs done → in Database: 2 * 4 = 8 records
3rd run: track1 → c1=0 (!!!), track2 → c1 = 9, track3 → c1=10, track4 → c1=11
4th run: track1 → c1=0 (!!!), track2 → c1 = 13, track3 → c1=14, track4 → c1=15
and so on

Seems that the record count after a “make new database” command is always zero, no matter if the file already exists or not.
Ok. Nevermind.
It works by deleting the file. And so I don’t complain about anything :wink:

bye,
Clancy

I haven’t tried it, but you can probably delete all records before creating new ones.
This might be more efficient than deleting the file and creating a new one


.
tell application "Database Events"
	launch
	make new database with properties {name:DBname, location:DBpath}
	tell database DBname
		try
			delete records
		end try
.

Hi Stefan,

unfortunately it didn’t work for my script.
Since the count of records is always zero, there are no records to delete!

I also tried:

set c to count of records
   repeat with i from 1 to c
       delete record i
   end repeat

But as I said, count of records is zero…

For now I use the method of deleting the whole file.

Bye,
Wiggum