SQLite Insert not working as expected in Swift

I am trying to insert data from an array into my table. If the insert is done, I think it is supposed to be printed in the console, but it is not. So I tried db.trace (println) and it shows me

SELECT * FROM "formatedData1"

My createDatabase function:

    func createDatabase(){
        var data = db["formatedData1"]

        db.create(table: data, ifNotExists: true){ d in
            d.column(type, primaryKey: true)
            d.column(ada)
            d.column(code)
            d.column(name)
            d.column(proof)
            d.column(size)
            d.column(case_size)
            d.column(price)
        }

        for var i = 0; i < self.dataArrayMut.count; ++i{
            data.insert(type <- (self.dataArrayMut[i].valueForKey("Type") as! String), ada <- (self.dataArrayMut[i].valueForKey("ADA") as! String), code <- (self.dataArrayMut[i].valueForKey("Code") as! String), name <- (self.dataArrayMut[i].valueForKey("Name") as! String), proof <- (self.dataArrayMut[i].valueForKey("Proof") as! String), size <- (self.dataArrayMut[i].valueForKey("Size") as! String), case_size <- (self.dataArrayMut[i].valueForKey("CaseSize") as! String), price <- (self.dataArrayMut[i].valueForKey("Price") as! String))
        }

        db.trace(println)
    }

      

Then I tried running queries on the table to see if the data exists.

1st request:

//Select * From data
let all = Array(data)
println(all.description)

      

And when I type it I get

[SQLite.Row, SQLite.Row, SQLite.Row, SQLite.Row, SQLite.Row, SQLite.Row, SQLite.Row, SQLite.Row]

      

Second request

    //Select name From data where type = rum
    let query = data.select(name)
                    .filter(type == "Rum")

    println(query)

      

And when I type it I get

"formatedData1"

      

Third query I wanted to print all rows from only two columns.

    for datas in data.select(type, name){
        println("type: \(data[type]), name: \(data[name])")
    }

      

And he prints

SELECT "Type", "Name" FROM "formatedData1"
type: SQLite.Expression<Swift.String>, name: SQLite.Expression<Swift.String>
type: SQLite.Expression<Swift.String>, name: SQLite.Expression<Swift.String>
type: SQLite.Expression<Swift.String>, name: SQLite.Expression<Swift.String>
type: SQLite.Expression<Swift.String>, name: SQLite.Expression<Swift.String>
type: SQLite.Expression<Swift.String>, name: SQLite.Expression<Swift.String>
type: SQLite.Expression<Swift.String>, name: SQLite.Expression<Swift.String>
type: SQLite.Expression<Swift.String>, name: SQLite.Expression<Swift.String>
type: SQLite.Expression<Swift.String>, name: SQLite.Expression<Swift.String>

      

Sorry for the long question, but any help would be much appreciated.

+3


source to share


1 answer


SQLite.swift does not output the results insert

to the console on its own, you should do something like this:

let (id, statement) = data.insert(type <- (self.dataArrayMut[i].valueForKey("Type") as! String))

      

Then you can check id

and statement

and print the results to the console:

if let dbRowID = id {
    println("New entry: \(dbRowID)")
} else if statement.failed {
    println("Row insertion failed because \(statement.reason!)")
}

      

To access the content of a string, you need to use the types defined in db, not literals:

let all = Array(data)
for row in all {
    let name = row[self.name]
    println(name)
}

      



Here self.name

is one of the types created by the containing class createDatabase()

and matches one of your columns.

EDIT:

You can access the content Row

either by signing it ( data[name]

where it name

has a type Expression

) or its get ( data.get(name)

) method .

To answer your comment: if you do data[42]

, it means you get 42nd Row

, not a column. With this, Row

you can get the content of the column using Expression

as I just explained.

Please note that in Xcode with ALT + CLICK you can see the type of any variable, it can help you when performing data flow.

+2


source







All Articles