I have an array of objects I want to insert into a SQLite database. I'm using FMDB as my SQLite wrapper. This is the code I have:
do {
if !db.beginTransaction() {
throw db.lastError()
}
for location in locations {
let command = "INSERT INTO Locations ( LocationId, Name ) VALUES ( ?, ? )"
try db.executeUpdate(command, NSArray( [ location.locationId, location.name ] ) )
}
if !db.commit() {
throw db.lastError()
}
}//do
FMDB's executeUpdate
is overloaded with these Objective-C signatures:
- (BOOL)executeUpdate:(NSString*)sql withErrorAndBindings:(NSError**)outErr, ...;
- (BOOL)executeUpdate:(NSString*)sql, ...;
- (BOOL)executeUpdateWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
- (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments;
- (BOOL)executeUpdate:(NSString*)sql values:(NSArray *)values error:(NSError * __autoreleasing *)error;
- (BOOL)executeUpdate:(NSString*)sql withParameterDictionary:(NSDictionary *)arguments;
- (BOOL)executeUpdate:(NSString*)sql withVAList: (va_list)args;
My current code gives me this compiler error on the executeUpdate
line:
DatabaseService.swift:299:44: Type of expression is ambiguous without more context
I get the same error when I pass in a normal Swift array.
When I change it to the values:
overload I get this error:
try db.executeUpdate( command, values: [ location.locationId, location.name ] )
DatabaseService.swift:299:55: Value of type 'Int64' does not conform to expected element type 'AnyObject'
And when I try to use the VAARGS version:
try db.executeUpdate( command, location.locationId, location.name )
DatabaseService.swift:299:12: Cannot invoke 'executeUpdate' with an argument list of type '(String, Int64, String)'