1

in my Xcode Project, I use SQLite to save user order list

My question is if my data like this enter image description here

Is there any way like table.count and I can get how many row it is ?

Here's my insert method I hope it can be like this

just create a model, and use directly

func insert(_ tableName :String, rowInfo :[String:String]) -> Bool {
    var statement :OpaquePointer? = nil
    let sql = "insert into \(tableName) "
        + "(\(rowInfo.keys.joined(separator: ","))) "
        + "values (\(rowInfo.values.joined(separator: ",")))"

    if sqlite3_prepare_v2(self.db, sql.cString(using: String.Encoding.utf8), -1, &statement, nil) == SQLITE_OK {
        if sqlite3_step(statement) == SQLITE_DONE {
            return true
        }
        sqlite3_finalize(statement)
    }

    return false
} 
1
  • 1
    No its not there, you need to create one. Commented Nov 26, 2018 at 10:31

2 Answers 2

4
let query = "select count(*) from tableName;"
if sqlite3_prepare(self.db, query, -1, &queryStatement, nil) == SQLITE_OK{
      while(sqlite3_step(queryStatement) == SQLITE_ROW){
           let count = sqlite3_column_int(queryStatement, 0)
           print("\(count)")
      }

}

Above code will give you the row count of the table.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using pure SQLite, you must use SQL requests to get any info from your database. In your case the request will be like this one:

"SELECT COUNT(*) FROM \(tableName)"

But why don't you use CoreData? It has not only a lot of necessary methods to make your data access easier, but also tons of optimisation code for both speed and memory using (what is much more important in my eyes).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.