Ginso
Ginso

Reputation: 569

SQLite for Swift: INSERT INTO SELECT is not working

i have a table MainTable containing a column 'variantID' (String) and a empty table called Variants with primary key 'id' (String) and a few other columns with default values. Now i want to create an entry for every variantID. Here is my code

func fillVariants() {
    do {
        let stmtString = "INSERT INTO Variants(id) SELECT DISTINCT variantID FROM MainTable"
        try self.connection?.prepare("\(stmtString)")
    } catch {
        print("Could not insert Variants.")
    }
}

the function does not catch any error, but the Variants table is still empty. What am i doing wrong?

Upvotes: 0

Views: 166

Answers (1)

Sahil Manchanda
Sahil Manchanda

Reputation: 10012

you need to use execute for executing raw sql

try self.connection?.execute("\(stmtString)")

Upvotes: 1

Related Questions