3

I am trying to insert data from array into my table. If the insert is done I think, its suppose to print it 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 to run queries on the table to see if the data exists.

1st query:

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

And when I print it I get

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

2nd Query

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

    println(query)

And when I print it I get

"formatedData1"

3rd Query I wanted to print all rows only from two columns.

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

And It 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 long question but any help would be highly appreciated.

Dereck
  • 95
  • 3
  • 12

1 Answers1

2

SQLite.swift doesn't print the insert results to the console by itself, you have to 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 row contents you need to use the types defined in the 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 class containing createDatabase(), and corresponds to one of your columns.

EDIT:

You can access the content of a Row either by subscripting it (data[name], where name is of type Expression) or by its get method (data.get(name)).

To answer your comment: if you do data[42], it means to get the 42nd Row, not column. With this Row, you can then get your column content by using an Expression like I just explained.

Note that in Xcode, with ALT+CLICK you can see the type of any variable, it can help you while following your data flow.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • Thanks Eric! The insert results are showing that something is being added to the rows. I figured that out a different way last night! But I am trying to figure out how to see what is in my rows (actual data)! I tried your last code with all and myRow and it gives me out of bound if i enter anything higher than 7! I guess it is because I have 8 columns. I actually see 26 different rows items getting inserted into the table and I want to see the actually data in the table for each column and row or according to my query! – Dereck Jun 09 '15 at 20:33
  • 1
    You're close, see the last part of my answer. You can access the content of a `Row` either by subscripting it (`data[name]`, where `name` is of type Expression) or by its `get` method (`data.get(name)`). Note that in Xcode, with `ALT+CLICK` you can see the type of any variable, it can help you while following your data flow. – Eric Aya Jun 09 '15 at 20:52
  • To answer your comment: in my example, `data[42]` was to get the 42nd `Row`, not column. With this `Row`, you can then get your column content by using an `Expression` like I just explained. – Eric Aya Jun 09 '15 at 20:54
  • You are confusing the `Query` type with the `Row` type. See the edited part of my answer again, and check your variables types: Query? Row? Array of Rows? and debug your code accordingly now that you have working examples. You've got all you need between your code and my examples. – Eric Aya Jun 09 '15 at 21:57
  • Thanks Eric! I just saw your edited code, If i use your code to print a custom query like Select name where type is "Rum" i wrote the code and it doesn't print anything! let query = data.select(name) .filter(type == "Rum") for row in query{ let name = row[self.name] let type = row[self.type] println(name) println(type) } – Dereck Jun 09 '15 at 22:03
  • Break down your statements and debug: first, do `let query = data.select(name)` and check if it has contents. If not, debug. If yes, debug the filter that comes next. If ok, debug the rows in the loop before trying to use them. Etc. Only *you* have the power to debug your data, I can't do that... What I could do is explain and give examples, and that's what I tried to do. – Eric Aya Jun 09 '15 at 22:13
  • Your example was very helpful. I just having hard time writing a custom query which says that Select * From myTable Where Type = "Something" – Dereck Jun 09 '15 at 22:40