• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
AppleScript access to databases (was: A bit of philosophy about Database events)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

AppleScript access to databases (was: A bit of philosophy about Database events)


  • Subject: AppleScript access to databases (was: A bit of philosophy about Database events)
  • From: BareFeet <email@hidden>
  • Date: Mon, 22 Mar 2010 11:01:23 +1100

On 22/03/2010, at 8:05 AM, Rob Lewis wrote:

I too was puzzled when I first read about database events. I kept trying to make it fit into the usual database concepts of tables etc. I gave up on actually trying to use it. 

Yes, the Database Events OSAX is advertised as giving AppleScript access to SQLite databases, but it adds its own layer and doesn't give you access to true SQL tables, views etc.

There used to be an OSAX for driving MySQL but I have no idea where to find it or if it's been maintained. It may not have been finished software. I wish there was a good solution. 

You can, however, create and access true SQLite database files from AppleScript. The simplest way is to use "do shell script" to use the command line tool "sqlite3".

Here's the code I was using to execute arbitrary SQLite commands and selects.

property sqlite3Path : "/usr/bin/sqlite3"
property columnSeparator : character id 3
property rowSeparator : character id 2

on SQLExecute(databasePath, sqlCommand)
set shellCommand to "echo " & (quoted form of sqlCommand) & " | " & sqlite3Path & space & quoted form of databasePath
--display dialog "shellCommand = " default answer shellCommand
set resultText to do shell script shellCommand
return resultText
end SQLExecute

on SQLExecuteSelect(databasePath, sqlCommand)
-- 2008 BareFeet http://www.tandb.com.au
set sqlCommandWithSeparators to "select *, '" & rowSeparator & "' from (" & sqlCommand & ");"
set shellCommand to "echo " & (quoted form of sqlCommandWithSeparators) & " | " & sqlite3Path & space & "-separator " & (quoted form of columnSeparator) & space & quoted form of databasePath
set recordsText to do shell script shellCommand
script speedyObject
property recordList : {}
end script
if recordsText is not "" then
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to (columnSeparator & rowSeparator & return)
set speedyObject's recordList to text items in recordsText
set last item in speedyObject's recordList to text 1 thru -(1 + (length of (columnSeparator & rowSeparator))) in last item in speedyObject's recordList
set AppleScript's text item delimiters to columnSeparator
set recordCount to (count speedyObject's recordList)
repeat with recordN from 1 to recordCount
set item recordN in speedyObject's recordList to text items in item recordN in speedyObject's recordList
end repeat
set AppleScript's text item delimiters to oldDelimiters
end if
return speedyObject's recordList
end SQLExecuteSelect


You can use it to, say:

on run
set databasePath to "/Users/Shared/Test.sqlite"
SQLExecute(databasePath, "
create table if not exists People
( ID integer primary key
, \"Name First\" text
, \"Name Last\" text
, Company text
, Email text
, DOB date
)
;
insert or ignore into People values (1, 'Tom', 'Brodhurst-Hill', 'BareFeetWare', 'email@hidden', julianday('1968-08-02'))
;
insert or ignore into People values (2, 'Fred', 'Flintstone', 'Bedrock Quarry', 'email@hidden', julianday('1920-06-01'))
;")
set resultList to SQLExecuteSelect(databasePath, "
select \"Name First\" || ' ' || \"Name Last\", Company, DOB from People where DOB > julianday('1925-01-01')")
set firstPerson to item 1 in resultList
set company to item 2 in firstPerson
end run

For more info, see:
http://macscripter.net/articles/436_0_10_0_C/
http://lists.apple.com/archives/applescript-studio/2006/Oct/msg00018.html
http://lists.apple.com/archives/applescript-studio/2006/Oct/msg00066.html
http://lists.apple.com/archives/AppleScript-Studio/2009/Feb/msg00016.html

You can also find info on various GUI tools for creating and accessing SQLite databases here:
http://www.tandb.com.au/sqlite/compare/?ml

Hope this helps,
Tom
BareFeet

 _______________________________________________
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

  • Follow-Ups:
    • Re: AppleScript access to databases (was: A bit of philosophy about Database events)
      • From: Vittorio <email@hidden>
    • Re: AppleScript access to databases (was: A bit of philosophy about Database events)
      • From: Axel Luttgens <email@hidden>
References: 
 >Re: A bit of philosophy about Database events (From: Rob Lewis <email@hidden>)

  • Prev by Date: Re: A bit of philosophy about Database events
  • Next by Date: Re: AppleScript access to databases (was: A bit of philosophy about Database events)
  • Previous by thread: Re: A bit of philosophy about Database events
  • Next by thread: Re: AppleScript access to databases (was: A bit of philosophy about Database events)
  • Index(es):
    • Date
    • Thread