Dont kown how to fetch database?

后端 未结 1 1491
死守一世寂寞
死守一世寂寞 2021-01-29 00:22

I\'m complete beginner with sqlite and gtk. I\'m making my 1st project in code::blocks. I have a problem in fetching the database my code is:

#include 

        
相关标签:
1条回答
  • 2021-01-29 00:41

    sqlite3_exec is not suitable if you want to handle the returned data at the place where you are querying it, and has some other disadvantages.

    For a query, you should always use sqlite3_prepare_v2, and then call sqlite3_step in a loop. To get variable values into the statement, use parameter markers (?) and the sqlite3_bind* functions. To read returned values, use the sqlite3_column_* functions (but in this case, you don't actually want to read any values).

    char *user = "Supu";
    char *password = "secret";
    sqlite3_stmt *stmt;
    const char *sql = "SELECT username, password FROM user WHERE username = ? AND password = ?";
    rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "error: %s, %s\n", sql, sqlite3_errmsg(db));
    } else {
        sqlite3_bind_text(stmt, 1, user, -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(stmt, 2, password, -1, SQLITE_TRANSIENT);
        while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
            printf("returned row: user = %s, password = %s\n",
                   sqlite3_column_text(stmt, 0),
                   sqlite3_column_text(stmt, 1));
            count++;
        }
        if (rc != SQLITE_DONE)
            fprintf(stderr, "error: %s\n", sqlite3_errmsg(db));
        sqlite3_finalize(stmt);
    }
    
    0 讨论(0)
提交回复
热议问题