Here’s an example of how to create an SQLite database using the contents of a basic Numbers table:
NOTE: The example script assumes that the source table has a single row header whose cell values will be used as the names of the created record fields in the database. Before running the script, make sure that the value of each cell of the row header is unique.
NOTE: By default, the Database Events application creates database file in a folder titled “Databases” located in the Documents folder. The provided script will create this folder if it doesn’t exist.
DO THIS ► DOWNLOAD a Numbers document containing the example table.
Build Database from Selected Table
01
propertyextractDataWithFormatting : false
02
propertycoerceNumberToInteger : true
03
propertydocumentsFolder : missing value
04
05
setdocumentsFolderto (path to documents folder)
06
tellapplication "Numbers"
07
activate
08
try
09
if not (existsdocument 1) then errornumber 1000
10
telldocument 1
11
try
12
tellactive sheet
13
set theselectedTableto ¬
14
(the firsttablewhoseclassofselection rangeisrange)
15
end tell
16
on error
17
errornumber 1001
18
end try
19
display dialog "This script will create an SQLite database with the data contained in the selected table." buttons {"Cancel", "Begin"} default button 2 with icon 1
20
tellselectedTable
21
setdatabaseNameto the name of it
22
23
tellapplication "Finder"
24
if not (existsfolder "Databases" ofdocumentsFolder) then
The result of a successful execution of the script will be a new SQLite database file with a file extension of “.dbev” and two supporting files with extended versions of the database file extension (⬇ see below )
Querying the Database
SQLite database queries using the Database Events application are written using standard AppleScript statements incorporating “whose” and “where” clauses to locate records by the value of their fields. The example script below demonstrates this technique using the created example database.
When referencing the database in query scripts, use a POSIX path reference to the main database file (the one with the file extension of “.dbev”)
Query Examples
01
setdatabaseFileto (POSIX pathof (path to documents folder)) & "Databases/Presidents of the United States.dbev"
02
03
tellapplication "Database Events"
04
tell database databaseFile
05
getvalueoffield "Last Name" of (everyrecordwhosevalueoffield "Start Year" is greater than or equal to 1800 andvalueoffield "Start Year" is less than or equal to 1900)
Mention of third-party websites and products is for informational purposes only and constitutes neither an endorsement nor a recommendation. MACOSXAUTOMATION.COM assumes no responsibility with regard to the selection, performance or use of information or products found at third-party websites. MACOSXAUTOMATION.COM provides this only as a convenience to our users. MACOSXAUTOMATION.COM has not tested the information found on these sites and makes no representations regarding its accuracy or reliability. There are risks inherent in the use of any information or products found on the Internet, and MACOSXAUTOMATION.COM assumes no responsibility in this regard. Please understand that a third-party site is independent from MACOSXAUTOMATION.COM and that MACOSXAUTOMATION.COM has no control over the content on that website. Please contact the vendor for additional information.