Yes, Intersect is what I was looking for… but I can’t get it to work.
Below is a longish script (probably too long to display) that’s mostly based on the SQLite Lib2 sample script. I’ve tried adding a handler with an intersect command, and it doesn’t work reliably. Right now it’s just returning the first query. I’ve gotten it to return some of the intersected data, along with some non-intersected data, but never the exact intersect.
(* This is a simple example of how to create a database, write some data to it, and then query it *)
use AppleScript version "2.5" -- El Capitan (10.11) or later
use script "SQLite Lib2" version "1.0.0"
use scripting additions
property theSQLString : ""
set dbFile to "~/desktop/TestDB2.db"
set tableName to "standings"
set updateTableName to "UpdatedStandings"
set theData to {¬
{"Christopher", "Froome", "Britain", 1}, ¬
{"Fabio", "Aru", "Italy", 2}, ¬
{"Romain", "Bardet", "France", 3}, ¬
{"Rigoberto", "Uran", "Colombia", 4}, ¬
{"Jakob", "Fuglsang", "Denmark", 5} ¬
}
set moreData to {¬
{"Christopher", "Froome", "Britain", 1}, ¬
{"Fabio", "Aru", "Italy", 2}, ¬
{"Egan", "Bernal", "Colombia", 6}, ¬
{"Geraint", "Thomas", "Great Britain", 7}, ¬
{"Steven", "Kruijswijk", "New Zealand", 8}, ¬
{"Emanuel", "Buchmann", "Germany", 9}, ¬
{"Julian", "Alaphilippe", "France", 10} ¬
}
set theColumns to {"firstname", "lastname", "country", "position integer"}
set openDB to CreateOpenDB(dbFile, tableName, theColumns)
set entireTable to RetrieveEntireTable(dbFile, tableName)
ClearTable(dbFile, tableName)
set clearedTable to RetrieveEntireTable(dbFile, tableName)
set enteredData to EnterDataAsList(dbFile, tableName, theColumns, theData)
set entireTable to RetrieveEntireTable(dbFile, tableName)
--ClearTable(dbFile, tableName)
set result1 to FindEntryInColumn(dbFile, tableName, "country", {"Italy"})
set result2 to FindEntryInColumn(dbFile, tableName, "country", {"Italy", "France"})
set result3 to DeleteRecordsInColumn(dbFile, tableName, "country", {"Italy"})
set result4 to FindEntryInColumn(dbFile, tableName, "country", {"Italy"})
set enteredData to EnterDataAsList(dbFile, tableName, theColumns, theData)
set result5 to FindEntryInColumn(dbFile, tableName, "country", {"Italy", "France"})
set result6 to DeleteRecordsInColumn(dbFile, tableName, "country", {"Italy", "France"})
set result7 to FindEntryInColumn(dbFile, tableName, "country", {"Italy", "France"})
set result8 to EnterAnItem(dbFile, tableName, theColumns, item 1 of result1)
set result9 to FindEntryInColumn(dbFile, tableName, "country", {"Italy", "France"})
ClearTable(dbFile, tableName)
set enteredData to EnterDataAsList(dbFile, tableName, theColumns, theData)
AddTableToDB(dbFile, updateTableName, theColumns)
set enteredData to EnterDataAsList(dbFile, updateTableName, theColumns, moreData)
set updatedTable to RetrieveEntireTable(dbFile, updateTableName)
set commonData to TableInterSect(dbFile, tableName, updateTableName)
return {result1, result2, result3, result4}
on TableInterSect(dbFile, firstTable, secondTable)
--SELECT column1 [, column2 ]
--FROM table1 [, table2 ]
--[WHERE condition]
--
--INTERSECT
--
--SELECT column1 [, column2 ]
--FROM table1 [, table2 ]
--[WHERE condition]
set firstQuery to "select * from " & firstTable
set secondQuery to "select * from " & secondTable
set sqlString to firstQuery & "; intersect; " & secondQuery
set theDb to open db in file ¬
dbFile with can create
-- begin transaction db theDb
try
set commonData to query db theDb ¬
sql string sqlString
on error errMsg number errNum
close db theDb
display dialog ("Error Number: " & errNum as text) & return & return & "Error Message: " & return & return & "\"" & errMsg & "\""
return {errMsg, errNum}
end try
return commonData
end TableInterSect
on DeleteRecordsInColumn(dbFile, tableName, columnName, textToFind)
local theSQLString, argMarkers, columnName, textToFind
if class of textToFind is text then set textToFind to {textToFind}
set argMarkers to generatePlaceHolders(count of textToFind)
set theSQLString to "delete from " & tableName & " where " & columnName & " in (" & argMarkers & ")"
set theDb to open db in file ¬
dbFile with can create
try
update db theDb ¬
sql string theSQLString ¬
with arguments textToFind ¬
--result type list of lists
set foundRecords to FindEntryInColumn(dbFile, tableName, columnName, textToFind)
close db theDb
on error errMsg number errNum
close db theDb
display dialog ("Error Number: " & errNum as text) & return & return & "Error Message: " & return & return & "\"" & errMsg & "\""
return {errMsg, errNum}
end try
end DeleteRecordsInColumn
on FindEntryInColumn(dbFile, tableName, columnName, textToFind)
if class of textToFind is text then set textToFind to {textToFind}
set argMarkers to generatePlaceHolders(count of textToFind)
set theSQLString to "select * from " & tableName & " where " & columnName & " in (" & argMarkers & ")"
set theDb to open db in file ¬
dbFile with can create
try
set foundRecords to query db theDb ¬
sql string theSQLString ¬
with arguments textToFind ¬
result type list of lists
close db theDb
on error errMsg number errNum
close db theDb
display dialog ("Error Number: " & errNum as text) & return & return & "Error Message: " & return & return & "\"" & errMsg & "\""
return {errMsg, errNum}
end try
return foundRecords
end FindEntryInColumn
on CreateOpenDB(dbFile, tableName, theColumns)
-- create and open db
-- create table, clearing it if necessary set saveTID to AppleScript's text item delimiters
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {", "}
set theColumns to theColumns as text
set AppleScript's text item delimiters to saveTID
set theDb to ¬
open db in file dbFile ¬
with can create
try
update db theDb ¬
sql string ("drop table if exists " & tableName)
update db theDb ¬
sql string ("create table " & tableName & " (" & theColumns & ")")
on error errMsg number errNum
display dialog ("Error Message: \"" & errMsg & "\"" & return & return & " Number: " & errNum as text)
close db theDb
return {errMsg, errNum}
end try
return theDb
end CreateOpenDB
on AddTableToDB(dbFile, tableName, theColumns)
-- create and open db
-- create table, clearing it if necessary set saveTID to AppleScript's text item delimiters
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {", "}
set theColumns to theColumns as text
set AppleScript's text item delimiters to saveTID
set theDb to ¬
open db in file dbFile ¬
with can create
try
update db theDb ¬
sql string ("drop table if exists " & tableName)
update db theDb ¬
sql string ("create table " & tableName & " (" & theColumns & ")")
on error errMsg number errNum
display dialog ("Error Message: \"" & errMsg & "\"" & return & return & " Number: " & errNum as text)
close db theDb
return {errMsg, errNum}
end try
return theDb
end AddTableToDB
on EnterAnItem(dbFile, tableName, theColumns, anEntry)
local theSQLString
set argPlaceHolders to generatePlaceHolders(count of theColumns)
set theSQLString to "insert into " & tableName & " values (" & argPlaceHolders & ")"
set theDb to open db in file ¬
dbFile with can create
try
update db theDb ¬
sql string theSQLString ¬
with arguments anEntry
close db theDb
on error errMsg number errNum
close db theDb
display dialog ("Error Number: " & errNum as text) & return & return & "Error Message: " & return & return & "\"" & errMsg & "\""
return {errMsg, errNum}
end try
return theDb
end EnterAnItem
on EnterDataInDB(dbFile, tableName, theColumns, theData)
local theSQLString
set argPlaceHolders to generatePlaceHolders(count of theColumns)
set theSQLString to "insert into " & tableName & " values (" & argPlaceHolders & ")"
set theDb to open db in file ¬
dbFile with can create
try
repeat with anEntry in theData
update db theDb ¬
sql string theSQLString ¬
with arguments anEntry
end repeat
close db theDb
on error errMsg number errNum
close db theDb
display dialog ("Error Number: " & errNum as text) & return & return & "Error Message: " & return & return & "\"" & errMsg & "\""
return {errMsg, errNum}
end try
return theDb
end EnterDataInDB
on RetrieveEntireTable(dbFile, tableName)
try
set theDb to open db in file ¬
dbFile with can create
-- begin transaction db theDb
set entireTable to query db theDb ¬
sql string ("select * from " & tableName)
on error errMsg number errNum
close db theDb
display dialog ("Error Number: " & errNum as text) & return & return & "Error Message: " & return & return & "\"" & errMsg & "\""
return {errMsg, errNum}
end try
return entireTable
end RetrieveEntireTable
on ClearTable(dbFile, tableName)
set theDb to open db in file ¬
dbFile with can create
-- clear database and start again
try
update db theDb ¬
sql string ("delete from " & tableName)
on error errMsg number errNum
close db theDb
display dialog ("Error Number: " & errNum as text) & return & return & "Error Message: " & return & return & "\"" & errMsg & "\""
return {errMsg, errNum}
end try
end ClearTable
on EnterDataAsList(dbFile, tableName, theColumns, theData)
-- enter data as a batch (list of lists)
local argPlaceHolders, sqlString
set argPlaceHolders to generatePlaceHolders(count of theColumns)
set sqlString to "insert into " & tableName & " values (" & argPlaceHolders & ")"
set theDb to open db in file ¬
dbFile with can create
try
batch update db theDb ¬
sql string sqlString ¬
with arguments theData
on error errMsg number errNum
close db theDb
display dialog ("Error Number: " & errNum as text) & return & return & "Error Message: " & return & return & "\"" & errMsg & "\""
return {errMsg, errNum}
end try
return theDb
end EnterDataAsList
to generatePlaceHolders(placeHolderCount)
set argPlaceHolders to {}
repeat (placeHolderCount) times
set the end of argPlaceHolders to "?"
end repeat
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {", "}
set argPlaceHolders to argPlaceHolders as text
set AppleScript's text item delimiters to saveTID
return argPlaceHolders
end generatePlaceHolders