3

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)'

Dai
  • 141,631
  • 28
  • 261
  • 374
  • `db.executeUpdate("INSERT ...", withArgumentsInArray: [...])` should work just fine. But did you consider using a Swift API around SQLite, such as http://github.com/groue/GRDB.swift or http://github.com/stephencelis/SQLite.swift ? – Gwendal Roué Mar 14 '16 at 04:46

0 Answers0