This short snippet creates a database and then a table:
property pDBase : missing value
set pDBase to FMDatabase's databaseWithPath_(pDBPath)
if pDBase's |open|() as boolean = true then
pDBase's setShouldCacheStatements_(true)
pDBase's executeUpdate_("create table transactions (date double, type text, description text, amount double, category text)")
end if
It looks like this will work and it seems better than using this arrangement:
do shell script tHead & tSQLStatement & tTail
I will play further but better yet if someone has already implemented this.
I think this FMDB Cocoa SQLite wrapper is a better solution than the do shell arrangement.
Please take a look at this script:
on addTransactionList_toTableName_inDB_(tTransactionList, tTableName, tDBase)
repeat with tRecord in tTransactionList
set tTransactionType to tRecord's objectAtIndex_(0)
set tDatePosted to tRecord's objectAtIndex_(1)
set tTransactionDate to dateFromString_(tDatePosted)
set tTransactionAmount to NSNumber's numberWithFloat_(tRecord's objectAtIndex_(2)'s floatValue)
set tTransactionUniqueID to tRecord's objectAtIndex_(3)
set tTransactionName to tRecord's objectAtIndex_(4)
tDBase's beginTransaction
tDBase's executeUpdate_("insert into transactions (date, type, description, amount) values (?, ?, ?, ?)", tDatePosted, tTransactionType, tTransactionName, tTransactionAmount)
tDBase's commit
end repeat
end addTransactionList_toTableName_inDB_
It takes a tTransactionList (NSArray)
a tTableName (NSString)
and a tDBase reference to a FMDatabase created or opened see earlier post.
tTransactionType is an NSString
tTransactionDate is an NSDate
tTransactionAmount is an NSNumber created from a NSString
tTransactionUniqueID is an NSString
tTransactionName is an NSString
I am just working my way through this Cocoa wrapper class but am enthusiastic about it’s use from within ASOC and would encourage others to review it.