sqlite3 help

i want return a value for the GetHP function, i don't know how i return this value from the sql result.
i have the int returnvalue declared on my header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//=============================================================================
#include "CDBMGR.h"
#include <string>       // std::string
#include <sqlite3.h> 
#include <iostream>
#include <sstream>

#include <cstring>
//=============================================================================
CDBMGR::CDBMGR() {
}

//=============================================================================
bool CDBMGR::open( char dbname[256])
{

rc = sqlite3_open("rpg.db", &db);

}
 int callback(void *NotUsed, int argc, char **argv, char **azColName){
   int i;
   for(i=0; i<argc; i++){
     printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");

   }
//returnvalue = (int)argv[i];
   printf("\n");
   return 0;
}
void CDBMGR::SetHP(char username[256], int value)
{
std::ostringstream ss;

ss<<"UPDATE USERS SET HP = ";
ss<<value;
ss<<" WHERE username = '";
ss<<username;
ss<<"'";
strcpy(sql, ss.str().c_str());

rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
   if( rc != SQLITE_OK ){
      fprintf(stderr, "SQL error: %s\n", zErrMsg);
      sqlite3_free(zErrMsg);
   }else{
      fprintf(stdout, "Operation done successfully\n");
   }



}
int CDBMGR::GetHP( char username[256])
{
std::ostringstream ss;

ss<<"select HP from USERS where username = '";
ss<<username;
ss<<"'";
strcpy(sql, ss.str().c_str());

rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);

}
Do you mean line 61? You want to return rc? You aren't being very clear.

If you are wanting to return success or failure of sqlite3_exec(/*...*/); then you can do return sqlite3_exec(/*...*/); or simply return rc after line 61.

Otherwise you have me lost on what you mean.
i want return the sql result in the function
int CDBMGR::GetHP( char username[256])
the sql result, the HP value
Last edited on
That code doesn't make any sense to me. Let's look at GetHP.
1
2
3
4
5
6
7
8
9
10
11
12
int CDBMGR::GetHP( char username[256])
{
std::ostringstream ss;

ss<<"select HP from USERS where username = '";
ss<<username;
ss<<"'";
strcpy(sql, ss.str().c_str());  // Why copy the string?  And whare you copying the string to?

rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);  // What is data?

}


I would expect the code to look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int GetHPCallback(void* data, int argc, char** argv, char** azColName)
{
    // It's good practice to use col names rather than position
    if (int* hp = reinterpret_cast<int*>(data))
        for (int i = 0; i < argc; ++i)
            if (strcmp(azColName[i], "HP") == 0)
            {
                *hp = atoi(argv[i]);
                break;
            }

    return 0;
}

int CDBMGR::GetHP( char username[256])
{
    std::ostringstream ss;
    ss<<"select HP from USERS where username = '" <<username <<"'";
    std::string sql = ss.str();

    int hp = 0;
    char *zErrMsg = 0;
    int rc = sqlite3_exec(db, sql.c_str(), GetHPCallback, reinterpret_cast<void*>(&hp), &zErrMsg);
    if (rc == SQLITE_OK)
        return hp;

    std::string err = zErrMsg;
    sqlite3_free(zErrMsg)
    throw std::runtime_error(err);
}


EDIT: Fix applied, thanks.
Last edited on
rror: cannot convert ‘char**’ to ‘const char*’ for argument ‘1’ to ‘int strcmp(const char*, const char*)’
if (strcmp(azColName, "HP") == 0)
Surely you can fix that, right?

I forgot the index ... You're expected to get the idea, not get working code.
i fixed
Topic archived. No new replies allowed.