4
if sqlite3_prepare_v2(db, "select a from aaa", -1, &q,nil) == SQLITE_OK {
    while sqlite3_step(q) == SQLITE_ROW {
        let res = sqlite3_column_text(q, 1)
        print(res)
    }
} else {
    print("NOT WORKING")
    NSLog("%s", sqlite3_errmsg(db))
}

In my table aaa I have a field named 'a' and values 1,2,3 as shown here :

enter image description here

I'm trying to put the values in an array or just print them, but when I print (print(res)), the values are just "nil".

How can I get the real values?

EDIT: the values (1,2,3) are strings and not intagers, that why i chose sqlite3_column_text() and not sqlite3_column_int()

Help Pleasee
  • 211
  • 3
  • 15
  • Compare http://stackoverflow.com/questions/25168030/issue-with-unsafepointeruint8-in-sqlite-project-in-swift for how the result from sqlite3_column_text can be converted to a Swift String. – Martin R Oct 05 '16 at 15:39
  • 1
    Shouldn't the first column have index 0 instead of 1? – Martin R Oct 05 '16 at 15:40
  • try with index = 0 instead of 1, or use directly the name of column: `a` – Duyen-Hoa Oct 05 '16 at 15:40
  • I tried it with index = 0 with no success. It seems like the real problem is to convert sqlite3_column_text to swift string as @Martin R said – Help Pleasee Oct 05 '16 at 15:55

2 Answers2

12

SOLVED:

the problem was to convert the function sqlite3_column_text() to string and the solution in swift3 is:

let string = String(cString: sqlite3_column_text(statement, 0))
Help Pleasee
  • 211
  • 3
  • 15
0

I think the data type of a is Int and you are fetching it as text sqlite3_column_text.

Try this instead: sqlite3_column_int

If doesn't work try these combinations:

  • Int with row 0: sqlite3_column_int(q, 0)

  • text with row 0 : sqlite3_column_text(q, 0)

Bista
  • 7,869
  • 3
  • 27
  • 55