เมื่อฉันใช้ห้องสมุดของฉันด้วยวิธีนี้ applescript จะไม่แยกวิเคราะห์ display dialog
เส้น, การรักษา dialog
ในฐานะตัวบ่งชี้!
ทุกคนสามารถอธิบายได้หรือไม่ว่าทำไมสิ่งนี้ถึงไม่ถูกเข้าใจในแบบที่ฉันต้องการ ขอบคุณมาก! ** อัพเดท ** สคริปต์ทั้งสองด้านล่างนี้จะรวบรวมและทำงานอย่างถูกต้อง
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
แต่ถ้าฉันเขียนแบบนี้ด้วยการซ้อนซ้อนระดับพิเศษมันใช้ได้ดี:
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
FYI ไลบรารีคือ:
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