Kiedy w ten sposób korzystam z mojej biblioteki, AppleScript nie analizuje display dialog
linii, traktując ją dialog
jako identyfikator!
Czy ktoś może wyjaśnić, dlaczego nie jest to rozumiane tak, jak tego chcę? Wielkie dzięki! ** AKTUALIZACJA ** Oba poniższe skrypty będą się teraz kompilować i będą działać poprawnie
use myLib : script "IHDatabase" -- the name of the library
-- UPDATE -- See also my answer below
use scripting additions
-- UPDATE -- without the above statement this script will not compile
set db to IHDatabase of myLib at "~/Desktop/TestDB.db"
-- IHDatabase handler returns an IHDB script object
tell db
add_sql("drop table if exists testtable;")
add_sql("create table testtable (firstname, lastname, country);")
add_sql("insert into testtable values('Ray', 'Barber', 'USA');")
add_sql("insert into testtable values('jj', 'Sancho', 'Spain');")
add_sql("insert into testtable values('Adam', 'Bell', 'Canada');")
add_sql("insert into testtable values('Bruce', 'Phillips', 'USA');")
add_sql("insert into testtable values('Kim', 'Hunter', 'USA');")
add_sql("insert into testtable values('Kevin', 'Bradley', 'USA');")
add_sql("select * from testtable;") -- columns are separated by pipes ("|") in the result.
set testtable_result to run_sql()
log testtable_result & return & return
set title to "So far, the table contents are: " & return & return
display dialog title & testtable_result buttons {"Done"} default button 1 with icon 1
end tell
Ale jeśli napiszę to w ten sposób, z dodatkowym poziomem informowania o zagnieżdżeniu, będzie działało OK:
tell script "IHDatabase" -- the name of the library
set db to IHDatabase at "~/Desktop/TestDB.db"
-- IHDatabase handler returns an IHDB script object
tell db
add_sql("drop table if exists testtable;")
add_sql("create table testtable (firstname, lastname, country);")
add_sql("insert into testtable values('Ray', 'Barber', 'USA');")
add_sql("insert into testtable values('jj', 'Sancho', 'Spain');")
add_sql("insert into testtable values('Adam', 'Bell', 'Canada');")
add_sql("insert into testtable values('Bruce', 'Phillips', 'USA');")
add_sql("insert into testtable values('Kim', 'Hunter', 'USA');")
add_sql("insert into testtable values('Kevin', 'Bradley', 'USA');")
add_sql("select * from testtable;") -- columns are separated by pipes ("|") in the result.
set testtable_result to run_sql()
log testtable_result & return & return
set title to "So far, the table contents are: " & return & return
display dialog title & testtable_result buttons {"Done"} default button 1 with icon 1
end tell
end tell
Do Twojej dyspozycji jest biblioteka:
property name : "IHDatabase"
property version : "1.0"
--IHDatabase class
on IHDatabase at dbname
script IHDB
property loc : missing value -- tells SQLite where to put our db if it doesn't exist, identifies it if it does.
property head : missing value -- the opening statement of every future command to our db.
property tail : missing value --ends every query started with "head".
property sql_stmt_list : missing value -- we build up a SQL program here
on init(dbname)
set loc to space & dbname & space
set head to "sqlite3" & loc & quote
set tail to quote
set sql_stmt_list to {}
return me
end init
on add_sql(stmt)
set stmt_space to stmt & space
set sql_stmt_list to sql_stmt_list & stmt_space
end add_sql
on run_sql()
set rows to do shell script head & sql_stmt_list & tail
set sql_stmt_list to {}
return rows
end run_sql
end script
return IHDB's init(dbname)
end IHDatabase
źródło