0

I'm using SQLlite3 for my database and i'm trying use "select * table where * = ?" in Swift with Sqlite3 but I'm stuck using where here's the method I used but i get no response from the methode .

    func DoesExist(titles: String?){

        ArticleList.removeAll()
        let queryStrings = "SELECT * FROM articles WHERE title = ? "
        var stmt3:OpaquePointer?
        if sqlite3_prepare(db, queryStrings, -1, &stmt3, nil) != SQLITE_OK{
            let errmsg = String(cString: sqlite3_errmsg(db)!)
            print("error preparing Select: \(errmsg)")
            return
        }
        if sqlite3_bind_text(stmt3, 1, titles, -1, nil) != SQLITE_OK{
            let errmsg = String(cString: sqlite3_errmsg(db)!)
            print("failure binding title: \(errmsg)")
            return
        }

        while(sqlite3_step(stmt3) == SQLITE_ROW){
            let id = sqlite3_column_int(stmt3, 0)
            let author = String(cString: sqlite3_column_text(stmt3, 1))
            let title = sqlite3_column_int(stmt3, 2)

            //adding values to list
            ArticleList.append(Article(id: Int(id), author: String(describing: author), title: String(describing: title)))
             print(ArticleList.count)
 }

    }

i don't know if that helps but when i try printing sqlite3_step(stmt3) i get 101 I don't get what I did wrong so if anyone is familiar with this error help, please?

*****UPDATE*****

i added

if sqlite3_step(stmt3) != SQLITE_DONE {
        let errmsg = String(cString: sqlite3_errmsg(db)!)
        print("failure inserting foo: \(errmsg)")
    }
    print("---------------------\(sqlite3_step(stmt3))")

and istead of getting 101 i got 21 which means : out of memory

Nessrine Hafi
  • 87
  • 2
  • 16
  • Trying to bind an optional is suspect. Also see https://stackoverflow.com/questions/48859139/sqlite-swift-binding-and-retrieving?r=SearchResults&s=2|29.2100 – rmaddy Aug 31 '19 at 14:12
  • 1
    Unrelated but you are not cleaning things up properly. Always call `sqlite3_finalize` on a prepared statement when you are done with it. – rmaddy Aug 31 '19 at 14:13
  • @rmaddy i followed it i think the problem is with sqlite3_step. in the while loop i got nothing – Nessrine Hafi Aug 31 '19 at 14:20
  • @rmaddy ok i will add it :) – Nessrine Hafi Aug 31 '19 at 14:21
  • "... that doesn't work.", you need to be more precise when describing your issue. – Joakim Danielson Aug 31 '19 at 14:21
  • @JoakimDanielson the thing I don't get any errors so I don't understand the issue my self that way – Nessrine Hafi Aug 31 '19 at 14:23
  • Then add that information to your question, error, no rows returned, crash whatever it is it should be mentioned in the question so we don't have to guess what you are asking – Joakim Danielson Aug 31 '19 at 14:24
  • Have you checked [this answer](https://stackoverflow.com/a/28642293/9223839) to a previous question about sqlite? – Joakim Danielson Aug 31 '19 at 14:26
  • @JoakimDanielson i edited my issue i hope it will be more cleaser :) – Nessrine Hafi Aug 31 '19 at 14:30
  • @JoakimDanielson i will check it now – Nessrine Hafi Aug 31 '19 at 14:30
  • 101 means Done, i.e no error, according to [this table](https://www.sqlite.org/c3ref/c_abort.html). Are you sure you have any matching data in the database. – Joakim Danielson Aug 31 '19 at 14:37
  • @JoakimDanielson i followed the answer you gave me and i was missing a sentence so when i added it i got 21 error which means out of memory – Nessrine Hafi Aug 31 '19 at 14:40
  • Are you sure the `db` handle references an open database? – rmaddy Aug 31 '19 at 16:37
  • @rmaddy this how i declared it `var db: OpaquePointer` – Nessrine Hafi Aug 31 '19 at 17:16
  • But did you call `sqlite3_open_v2`? You have to actually open the database before you can use it. – rmaddy Aug 31 '19 at 17:16
  • @rmaddy yes yes i did and it works i tried "insert" and "select" and they both work . – Nessrine Hafi Aug 31 '19 at 17:19
  • 1
    Yes, but “out of memory” is SQLite’s way of saying that the `db` pointer is `nil`. Print `db` before the `sqlite3_xxx` call that gave you the “out of memory” error, and I wager you’ll confirm it is, indeed `nil`. You must have some path of execution where you reset it, or maybe you have two `db` references floating about (e.g. some local var and the ivar). But I tested the above code and it works. There are a lot of improvements I might suggest if you’re interested, but the above works. – Rob Aug 31 '19 at 17:57
  • @Rob thank you i will check. If you have any suggestion i'm happy to learn from :) – Nessrine Hafi Aug 31 '19 at 18:04
  • 1
    Perhaps https://gist.github.com/robertmryan/b6e7e32de1f1c3d79d5c151eaa32643e – Rob Aug 31 '19 at 18:22
  • @Rob when i tried it i got Thread 1: Precondition failed: Database not open . i guess the error was because of the database – Nessrine Hafi Aug 31 '19 at 18:48

1 Answers1

1

The (admittedly misleading) “out of memory” error is SQLite’s way of saying that the db pointer is nil. Print/test db before the sqlite3_xxx call that gave you the “out of memory” error, and you will see that it is, indeed, nil. If you are confident that you previously opened the database, then you must have some path of execution where you reset it to nil, or perhaps you have multiple db references floating about (e.g. some local var and the ivar).

You can add this test at the start of your method that has a precondition that the database must already be opened:

precondition(db != nil, "Database not open")
Rob
  • 415,655
  • 72
  • 787
  • 1,044