SQLite.swift framework

This function not working !!! I can add some other functions if your want I got problem with saving, its not working

Output - "Connected Ok The operation couldn’t be completed. (SQLite.Result error 0.)"

let db = try Connection("\(path)/database.sqlite3")
print("Connected Ok")

let nameTemp1 = Expression<String>("namePoint")
let latTemp1 = Expression<Double>("lat")
let longTemp1 = Expression<Double>("long")
let addressTemp1 = Expression<String?>("address")
let id = Expression<Int64>("id")
try db.run(TempPointOriginal.create(ifNotExists: true) { t in
    t.column(id, primaryKey: true)
    t.column(nameTemp1)
    t.column(latTemp1)
    t.column(longTemp1)
    t.column(addressTemp1)
})

try db.run(TempPointOriginal.insert(/*or: .replace,*/ nameTemp1 <- name1))
try db.run(TempPointOriginal.insert(/*or: .replace,*/ latTemp1 <- lat1))
try db.run(TempPointOriginal.insert(/*or: .replace,*/ longTemp1 <- long1))
try db.run(TempPointOriginal.insert(/*or: .replace,*/ addressTemp1 <- address1))

Upvotes: 0

Views: 879

Answers (1)

Joakim Danielson
Joakim Danielson

Reputation: 51892

Two things you need to fix or at least consider with your code. When you create a column like this

let nameTemp1 = Expression<String>("namePoint")

you are saying it is mandatory (not null), to make it optional the type has to be optional, <String?>

let nameTemp1 = Expression<String?>("namePoint")

So this is something you might want to change in your table

Secondly your insert is wrong since you are actually performing 4 inserts but only one column at a time has been given a value and with all columns mandatory this generates an error. So unless you make your columns optional all values must be given for an insert statement

try db.run(TempPointOriginal.insert(nameTemp1 <- name1, 
                                    latTemp1 <- lat1, 
                                    longTemp1 <- long1, 
                                    addressTemp1 <- address1))

Upvotes: 1

Related Questions