0

I'm getting SQLITE_DONE instead of getting SQLITE_ROW while selecting a single record from db. And it also doesn't shows the returned row.

Tried to check with SQLITE_DONE and tried to print record but it was giving exception read access violation which i have asked here

here is the code

int login() {
   char username[50], password[50];
   int i = 0;
   char c;
   cout << "Enter Username: ";
   cin >> username;
   cout << "Enter Password: ";
   c = _getch();
   while (c != 13) // for hiding password on console
   {
       password[i] = c;
       i++;
       cout << "*";
       c = _getch();    
   }

   sqlite3 * conn = NULL;

   int res = sqlite3_open( "./mtbs.db" , &conn);

   if (SQLITE_OK != res) {
       printf("%s\n", sqlite3_errmsg(conn));
       return res;
   }

   sqlite3_stmt * stmt = NULL;
   char * sql = "SELECT id, username FROM admin WHERE username = ? AND password = ? LIMIT 1";
    res = sqlite3_prepare_v2(conn,
          sql,
          -1,
          &stmt,
          NULL);

   if (SQLITE_OK != res) {
       printf("\n%s\n", sqlite3_errmsg(conn));
       sqlite3_close(conn);
       return res;
   }

   res = sqlite3_bind_text(stmt, 1, username, strlen(username), SQLITE_STATIC);
   if (SQLITE_OK != res) {
       printf("\n%s\n", sqlite3_errmsg(conn));
       sqlite3_close(conn);
       return res;
   }

   res = sqlite3_bind_text(stmt, 2, password, strlen(password), SQLITE_STATIC);
   if(SQLITE_OK != res){
       printf("\n%s\n", sqlite3_errmsg(conn));
       sqlite3_close(conn);
       return res;
   }

   if (SQLITE_ROW == sqlite3_step(stmt)) { // this ends up as SQLITE_DONE
       cout<< endl << sqlite3_column_text(stmt, 0) << "\t" << sqlite3_column_text(stmt, 1) << endl;
   }
   sqlite3_finalize(stmt);
   sqlite3_close(conn);
   return 1;
}
Community
  • 1
  • 1
Hiren
  • 613
  • 1
  • 8
  • 25

3 Answers3

2

You don't terminate your password with nul after reading. When you send the value to SQLite it may be whatever.

Use the debugger to step through the code and you will see what's happening.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
1

This occurs when no records match the query.

Print out the username and password as a test. One thing I see is that you never actually NULL terminate the password. I'm not positive, but you may be collecting a carriage return and line feed as the start of the password as well.

contrapants
  • 775
  • 4
  • 16
0

Finally got answer. It was password with non null terminated value. So to keep the functionality of hiding chars as * i used this:

char username[50], password[50];
string pass;
char c;
...
cout << "Enter Password: ";
c = _getch();
while (c != 13)
{
    pass.push_back(c);
    cout << "*";
    c = _getch();   
}

strcpy_s(password, pass.c_str());  // this saved me...

And now query return SQLITE_ROW as expected.

Hiren
  • 613
  • 1
  • 8
  • 25