Re: Persistent data storage
Re: Persistent data storage
- Subject: Re: Persistent data storage
- From: T&B <email@hidden>
- Date: Sat, 14 Jul 2007 00:12:20 +1000
Hi Jem,
One thing I need to do is to store some data so that I can use it
from different scripts (they are going to be run as separate
scripts) and later I want to add GUI for adding/deleting info.
It's a small amount of data, probably no more than 50 records where
each record contains 5-10 fields (all strings).
how would you recommend that I store the data?
As Bill mentioned, there is the Database Events app, which you can
script. It uses SQLite under the hood, but basically obscures SQL via
its own syntax and layer of structure. That sounds OK, but in my
opinion the actual implementation is very limiting.
SQLite itself is very robust and built into Mac OS X.4 (maybe even X.
3) onward. You can freely download and install it for earlier
versions. Apple uses it for data storage for a range of its own
software, such as Mail indexing, and some high end photo apps.
SQLite uses syntax that basically follows the SQL standard. It's
pretty easy to build and query basic databases. The syntax is actually
very similar to AppleScript. I personally think that you're better off
just learning the basic SQL syntax you need rather than learning the
syntax for some scripting addition or other app.
Rather than using the Database Events software, I suggest directly
using the engine underneath it. Here's one way to do that. You can
just copy and paste this whole script, and alter to your needs.
-- Sample SQLite manipuation, by T&B 2007
-- http://www.tandb.com.au/applescript/
-- Create an SQLExecute() AppleScript handler to do all the work
on SQLExecute(databasePath, sqlCommand)
set shellCommand to "echo \"" & sqlCommand & "\" | sqlite3 " & quoted
form of databasePath
set resultText to do shell script shellCommand
return resultText
end SQLExecute
-- Set a file path for your database.
-- The .sqlite suffix is just a convention, but .db or no extension
works fine too
set databasePath to "/Users/Shared/MyDatabase.sqlite"
-- Create a table
set sqlCommand to "
create table Contacts( Name text, House integer, Street text, Suburb
text, Post_Code text );
"
SQLExecute(databasePath, sqlCommand)
-- Insert new records into the table
set sqlCommand to "
insert into Contacts values ( 'P Sherman', 42, 'Wallaby Way',
'Sydney', '2002' );
insert into Contacts values ( 'Nemo', 13, 'Anemone', 'Great Barrier
Reef', '7055' );
insert into Contacts values ( 'Nigel', 32, 'Wharf Rd', 'Sydney',
'2003' );
"
SQLExecute(databasePath, sqlCommand)
-- Find records matching a criteria
set sqlCommand to "
select Name, Suburb from Contacts where Suburb like 'Sydney' and
Post_Code not in ('2010', '2040');
"
SQLExecute(databasePath, sqlCommand)
(*
The above script will yield:
P Sherman|Sydney
Nigel|Sydney
*)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden