On Jan 18, 2007, at 8:01 AM, [EMAIL PROTECTED] wrote:

I don't know what capability Applescript has. The easy way to do it with a script in the various Linux shell languages is with a "HERE" document, where the input to the command is redirected from the block of lines which follows
the command.  In your case, you'd do something like:

    sqlite3 filename.db <<'EOF'
    .headers on
    .mode column customers
    select * from customers ;
    EOF

If Applcscript doesn't support HERE documents (it's unlikely it does), you can accomplish something similar with redirecting from a separate file. I would hope it has that capability. You'd then do something like this to create a
"commands" file:

    echo '.headers on' > commands
    echo '.mode column customers' >> commands
    echo 'select * from customers;' >> commands

and then run sqlite using that command file for input:

    sqlite3 filename.db < commands

Hope that helps.

Derrell

Derrell,

Your solution solved my problem to a tee. Using a command file to issue multiple commands to sqlite when calling it from Applescript worked perfect. I knew that I probably could not mix and match "dot commands" with "sql commands", but I thought perhaps I could do so using "pragma & sql" commands.

Thanks for taking the time to help me, especially so quickly. I shut down my computer about 2:30AM last night, and this morning when I woke up around 8:30AM I checked the email, just in case, and I immediately saw the light when I read your message...

THANKS A MILLION FOR YOUR HELP...

Best regards,

Bill Hernandez
Plano, Texas


The term AppleScript comes in two flavors (both which are free and come with the operating system):

( 1 ) AppleScript which allows you to access (set/get) info from almost all applications, including the OS itself. It provides incredible flexibility, and power to an average user. Most all commercial software is scriptable. The interface is not a strong point, there are dialogs to display as well as get user input, it also provides choice lists that you can build on the fly, and have the user pick one or more items, it does a lot more, but there are GUI limitations.

( 2 ) Applescript Studio is full blown rapid application build environment that allows a user to build complete GUI applications using anywhere from a few lines to a few hundred lines of applescript code, instead of having to use thousands of lines of Objective C code. Not only that but but the xCode environment provides all the interface items one could ever hope for, along with all code that has already been in production to build the OS X operating system itself.

The OSX xCode development environment provides all the interface tools, and allows the user to use any number of languages to create an application. Once completed you cannot tell what language it was written in. So you could write a massive amount of Objective C code, or a few lines of AppleScript code, and the end result once compiled will be identical.

Anything you build using xCode is automatically scriptable. If I attach a data source containing the results of a query, to a GUI "Table View" object, anytime the user clicks on any of the column headers, the object handles the sorting of the records (up/down) without any code on my part whatsoever, there is no need to query/ fetch again from the database just to get the sorted records.

I have been doing structured programming on the Mac for about 19 years, and always hated the AppleScript syntax, but a few months ago I decided to really try to learn it in earnest, and I now realize what an incredible environment it truly is, it's like the iPod of development environments with built in GUI objects, debuggers, etc. Talk about minimal amount of code...

OSX comes with sqlite already installed. A guy named "Adam Bell" wrote a sample AppleScript that showed how to access sqlite, create, search, modify, etc from within AppleScript issuing the commands directly to any one of the Unix Shells. Basically I can have AppleScript open a shell, issue a numbe of commands, retrieve the results, massage them, etc, and close the shell when done, or I can run shell commands in the background so the user never sees the shell itself, which is what I will be doing.

One of the basic handlers (functions) within the script that "Adam Bell" wrote, and I used to create the DB and enter a few records :
-- +---------+---------+---------+---------+---------+---------+
on SQLite_CreateDatabase()
   set loc to space & "~/desktop/TestDB.db" & space
   set head to "sqlite3" & loc & quote
   set tail to quote
-- "head" tells SQLite where to put our db if it doesn't exist, identifies it if it does. -- "head" is the opening statement of every future command to our db.
   -- "tail" ends every query started with "head".

-- Next, we set up a table and give the columns labels (there can be several). -- Note the space between the semicolon (which ends every line) and the quote.
   set tblName to "customers"
set newTbl to "create table " & tblName & "(firstname, lastname, country); "

   -- Now we set up the data to be entered in the table
   set d1 to "insert into customers values('Ray', 'Barber', 'USA'); "
set d2 to "insert into customers values('Sancho', 'Panza', 'Spain'); "
   set d3 to "insert into customers values('Adam', 'Bell', 'Canada'); "
set d4 to "insert into customers values('Bruce', 'Phillips', 'USA'); "
   set d5 to "insert into customers values('Kim', 'Hunter', 'USA'); "
set d6 to "insert into customers values('Kevin', 'Bradley', 'USA'); "

   -- And finally, build the SQLite query and execute it
   do shell script head & newTbl & d1 & d2 & d3 & d4 & d5 & d6 & tail
   -- a new db called TestDB.db should appear on your desktop!
end SQLite_CreateDatabase
-- +---------+---------+---------+---------+---------+---------+

Here's the working test script I wrote to get me started.

on run
   -- Create a list to populate the popup menu
set aSearchCriteria to {"begins with", "contains", "is equal to", "ends with", "-", "does not begin with", "does not contain", "is not equal to", "does not end with", "-", "is greater than", "is less than", "-", "is greater than or equal to", "is less than or equal to"}

-- Build a simple record to use when building commands, this will grow considerably later set aQuery to {db_select:"SELECT", db_from:"FROM", db_where:"WHERE", db_order:"ORDER BY", db_howMany:"LIMIT", db_offset:"OFFSET", db_ending:";"}

   -- FOR TESTING PURPOSES SIMULATE SOME USER INPUT.
-- THE USER WILL PROVIDE THESE VALUES VIA THE CHOICES THEY MAKE IN THE SEARCH DIALOG

   set thisTable to "customers"
   set searchField to "lastname"
   set searchValue to ""
   set sortField to "firstname"
   set sortDir to "asc"

   -- SIMULATE SEVERAL REQUESTS FROM THE USER VIA THEIR SEARCH DIALOG

   set searchValue to "Be"
   set whichChoice to "begins with"
my sqlite_DoTheSearch(whichChoice, thisTable, searchField, searchValue, sortField, sortDir)

   set searchValue to "ey"
   set whichChoice to "contains"
my sqlite_DoTheSearch(whichChoice, thisTable, searchField, searchValue, sortField, sortDir)

   set searchValue to "Hunter"
   set whichChoice to "is equal to"
my sqlite_DoTheSearch(whichChoice, thisTable, searchField, searchValue, sortField, sortDir)

   set searchValue to "za"
   set whichChoice to "ends with"
my sqlite_DoTheSearch(whichChoice, thisTable, searchField, searchValue, sortField, sortDir)

   set searchValue to "Be"
   set whichChoice to "does not begin with"
my sqlite_DoTheSearch(whichChoice, thisTable, searchField, searchValue, sortField, sortDir)

   set searchValue to "ey"
   set whichChoice to "does not contain"
my sqlite_DoTheSearch(whichChoice, thisTable, searchField, searchValue, sortField, sortDir)

   set searchValue to "Hunter"
   set whichChoice to "is not equal to"
my sqlite_DoTheSearch(whichChoice, thisTable, searchField, searchValue, sortField, sortDir)

   set searchValue to "za"
   set whichChoice to "does not end with"
my sqlite_DoTheSearch(whichChoice, thisTable, searchField, searchValue, sortField, sortDir)

   set searchValue to "Bell"
   set whichChoice to "is greater than"
my sqlite_DoTheSearch(whichChoice, thisTable, searchField, searchValue, sortField, sortDir)

   set searchValue to "Bell"
   set whichChoice to "is less than"
my sqlite_DoTheSearch(whichChoice, thisTable, searchField, searchValue, sortField, sortDir)

   set searchValue to "Bell"
   set whichChoice to "is greater than or equal to"
my sqlite_DoTheSearch(whichChoice, thisTable, searchField, searchValue, sortField, sortDir)

   set searchValue to "Bell"
   set whichChoice to "is less than or equal to"
my sqlite_DoTheSearch(whichChoice, thisTable, searchField, searchValue, sortField, sortDir)
end run

-- THIS IS THE FUNCTION THAT HANDLES THE SEARCH
on sqlite_DoTheSearch(whichChoice, thisTable, searchField, searchValue, sortField, sortDir)
   set validRequest to true
if (whichChoice = "--") then -- If they select a divider line in the popup menu, ignore the request
      set validRequest to false
   else if (whichChoice = "begins with") then
set thisQuery to "select * from " & thisTable & " where " & searchField & " LIKE '" & searchValue & "%' order by " & sortField & " " & sortDir & ";"
   else if (whichChoice = "contains") then
set thisQuery to "select * from " & thisTable & " where " & searchField & " LIKE '%" & searchValue & "%' order by " & sortField & " " & sortDir & ";"
   else if (whichChoice = "is equal to") then
set thisQuery to "select * from " & thisTable & " where " & searchField & " LIKE '" & searchValue & "' order by " & sortField & " " & sortDir & ";"
   else if (whichChoice = "ends with") then
set thisQuery to "select * from " & thisTable & " where " & searchField & " LIKE '%" & searchValue & "' order by " & sortField & " " & sortDir & ";"
   else if (whichChoice = "does not begin with") then
set thisQuery to "select * from " & thisTable & " where " & searchField & " NOT LIKE '" & searchValue & "%' order by " & sortField & " " & sortDir & ";"
   else if (whichChoice = "does not contain") then
set thisQuery to "select * from " & thisTable & " where " & searchField & " NOT LIKE '%" & searchValue & "%' order by " & sortField & " " & sortDir & ";"
   else if (whichChoice = "is not equal to") then
set thisQuery to "select * from " & thisTable & " where " & searchField & " NOT LIKE '" & searchValue & "' order by " & sortField & " " & sortDir & ";"
   else if (whichChoice = "does not end with") then
set thisQuery to "select * from " & thisTable & " where " & searchField & " NOT LIKE '%" & searchValue & "' order by " & sortField & " " & sortDir & ";"
   else if (whichChoice = "is greater than") then
set thisQuery to "select * from " & thisTable & " where " & searchField & " > '" & searchValue & "' order by " & sortField & " " & sortDir & ";"
   else if (whichChoice = "is less than") then
set thisQuery to "select * from " & thisTable & " where " & searchField & " < '" & searchValue & "' order by " & sortField & " " & sortDir & ";"
   else if (whichChoice = "is greater than or equal to") then
set thisQuery to "select * from " & thisTable & " where " & searchField & " >= '" & searchValue & "' order by " & sortField & " " & sortDir & ";"
   else if (whichChoice = "is less than or equal to") then
set thisQuery to "select * from " & thisTable & " where " & searchField & " <= '" & searchValue & "' order by " & sortField & " " & sortDir & ";"
   end if
   if (validRequest) then
      set show_query_with_results to true
      set dbFilePath to "~/desktop/TestDB.db"
      set dbCommands to dbFilePath & "_commands"
      set dbCommandLog to dbFilePath & "_command.log"

      do shell script ("echo '.headers on' > " & dbCommands)
do shell script ("echo '.mode column customers' >> " & dbCommands) -- do shell script ("echo 'select * from customers;' >> " & dbCommands) do shell script ("echo " & quote & thisQuery & quote & " >> " & dbCommands)

set timeStamp to (date string of (current date) & space & "(" & time string of (current date) & ")") as string do shell script ("echo " & return & quote & timeStamp & quote & " >> " & dbCommandLog)
      do shell script ("cat " & dbCommands & " >>  " & dbCommandLog)
      if (show_query_with_results) then
set str to ("QUERY : " & quote & thisQuery & quote & return & return & (do shell script ("sqlite3 " & dbFilePath & " < " & dbCommands)))
      else
set str to (do shell script ("sqlite3 " & dbFilePath & " < " & dbCommands))
      end if
      -- Show the user the search results
      display dialog str default answer str
   end if
end sqlite_DoTheSearch


THESE ARE THE RESULTS OF THE DISPLAY DIALOG

This is a simple sample of what the dialog shows for testing purposes (the actual query and the results) When I get done with this the data will actually be displayed, edited, etc. in a full GUI application.

This query is searching for records whose lastname do not contain "ey", such as "Bradley", or "Harvey"
----------------------------
QUERY : "select * from customers where lastname NOT LIKE '%ey%' order by firstname asc;"

firstname   lastname    country
----------  ----------  ----------
Adam        Bell        Canada
Bruce       Phillips    USA
Kai         Edwards     England
Kim         Hunter      USA
Ray         Barber      USA
Sancho      Panza       Spain
----------------------------

The 'commands' file contains the last sqlite set of commands
----------------------------
.headers on
.mode column customers
select * from customers where lastname <= 'Bell' order by firstname asc;
----------------------------


The log should prove useful during testing, and when finished the log will track errors.

THESE ARE THE RESULTS TO THE LOG

----------------------------
Thursday, January 18, 2007 (10:22:53 AM)
.headers on
.mode column customers
select * from customers where lastname LIKE 'Be%' order by firstname asc;

Thursday, January 18, 2007 (10:22:55 AM)
.headers on
.mode column customers
select * from customers where lastname LIKE '%ey%' order by firstname asc;

Thursday, January 18, 2007 (10:22:55 AM)
.headers on
.mode column customers
select * from customers where lastname LIKE 'Hunter' order by firstname asc;

Thursday, January 18, 2007 (10:22:56 AM)
.headers on
.mode column customers
select * from customers where lastname LIKE '%za' order by firstname asc;

Thursday, January 18, 2007 (10:22:57 AM)
.headers on
.mode column customers
select * from customers where lastname NOT LIKE 'Be%' order by firstname asc;

Thursday, January 18, 2007 (10:22:58 AM)
.headers on
.mode column customers
select * from customers where lastname NOT LIKE '%ey%' order by firstname asc;

Thursday, January 18, 2007 (10:22:59 AM)
.headers on
.mode column customers
select * from customers where lastname NOT LIKE 'Hunter' order by firstname asc;

Thursday, January 18, 2007 (10:23:00 AM)
.headers on
.mode column customers
select * from customers where lastname NOT LIKE '%za' order by firstname asc;

Thursday, January 18, 2007 (10:23:01 AM)
.headers on
.mode column customers
select * from customers where lastname > 'Bell' order by firstname asc;

Thursday, January 18, 2007 (10:23:02 AM)
.headers on
.mode column customers
select * from customers where lastname < 'Bell' order by firstname asc;

Thursday, January 18, 2007 (10:23:03 AM)
.headers on
.mode column customers
select * from customers where lastname >= 'Bell' order by firstname asc;

Thursday, January 18, 2007 (10:23:04 AM)
.headers on
.mode column customers
select * from customers where lastname <= 'Bell' order by firstname asc;
----------------------------

Thanks Again...


-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------

Reply via email to