2

I need to display the set of row values from database. I have assigned the string into array of pointer variable in structure from database. While reading the string from array of pointer variable its returns with empty value.

Expected output:

username: admin
password: admin
status: active
created time: Wed Oct 19 17:53:46 2016
updated time: Mon Oct 31 15:26:53 2016

Actual Output I'm getting :

username:
password:
status:
created time:
updated time:

#include<stdio.h>
#include<stdlib.h>
#include<sqlite3.h>

const char* data = "Callback function called";

sqlite3 *db;
int sql_result;
char *sql;
char *zErrMsg;
int count = 0;

char *store[50];
char **result;

struct user
{
    char *user[50];
} *vu;

static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
  static int j = 0;
  int i = 0;
  printf("\n j_value:%d\n",j);
  printf("\n argc: %d", argc);
  for(i = 0; i < argc; i++)
  {
        printf("\n %s = %s", azColName[i], argv[i] ? argv[i] : "NULL"); 
        result[i] = argv[i]; /* Getting database values */
  }

  if(j < count)
  {
     store[j] = result[0];  /* Assigning data to array of pointer */
     printf("\n output[%d]:%s\n",j, store[j]);
     j++;
     if(j == count)
        j = 0;
  }
  for(i = 0;i < count;i++)
  {
     printf("\n Final output[%d]:%s\n",i, store[i]);
  }
  return 0;
}

int open_db() /* Open database */
{

  sql_result = sqlite3_open("test.db", &db);
  if( sql_result ){
  printf( "Error:%s", sqlite3_errmsg(db));
  exit(0);
}else{
 //printf("\nsuccess\n");
}
return 0;
}

int exec_db()  /* Execute SQL statement */
{
   sql_result = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
   if( sql_result != SQLITE_OK ){
   printf( "Error:%s", zErrMsg);
   sqlite3_free(zErrMsg);
 }else{
  //printf("\nsuccess\n");
 }
return 0;
}

int user_count(char *username)
{
    result = (char**)calloc(100, sizeof(char*));
    int count = 0;
    user_sql = (char*)calloc(100, sizeof(user_sql));
    sprintf(user_sql, "SELECT COUNT(*) FROM t_user WHERE USERNAME='%s'",username);

    sql_result = sqlite3_exec(db, user_sql, callback, (void*)data, &zErrMsg);
    free(user_sql);
    count = atoi(result[0]);
    free(result);

    return count;
}

int userpush()
{
    int z = 0;

    for(z = 0; z < 5; z++)
    {
        vu->user[z] = (char*)malloc(sizeof(vu->user));
        strcpy(vu->user[z], result[z]);
    }

    return 0;
}

int main()
{
    open_db();
    int value = 0, count = 0;
    char username[20];
    printf("\n Enter username: ");
    scanf("%s", username);
    count = user_count(username);
    if(count == 1)
    {
        result = (char**)calloc(800, sizeof(result));   
        sql = (char*)calloc(100, sizeof(sql));
        sprintf(sql, "SELECT * FROM t_user WHERE USERNAME='%s'",username);
        exec_db();free(sql);
        vu = (struct user*)malloc(2 * sizeof(struct user));
        userpush();
        printf("\n username: %s \t password: %s \t status: %s \t created time: %s \t updated time: %s \n", vu->user[0], vu->user[1], vu->user[2], vu->user[3], vu->user[4]);
        value = 1;
    }
    else
        value = 0;

    return value;
}

Any other best way to get the value from database and display in string format.

Muthukumar
  • 23
  • 2
  • Did you check `sql_result` in `user_count`? Did you do basic debugging ? – Jabberwocky Nov 03 '16 at 07:57
  • Where is the declaration of `user_sql`? – David Ranieri Nov 03 '16 at 08:00
  • 2
    Your problem is that you're trying to use `sqlite3_exec()`. [Read the data with a cursor](http://stackoverflow.com/documentation/sqlite/5456/sqlite3-stmt-prepared-statement-c-api/19406/reading-data-from-a-cursor) instead. – CL. Nov 03 '16 at 08:01
  • `int exec_db()` --> `int exec_db(void)` and so on for all functions. Take a look at [this SO post](http://stackoverflow.com/questions/693788/is-it-better-to-use-c-void-arguments-void-foovoid-or-not-void-foo) – LPs Nov 03 '16 at 08:05
  • `vu->user[z] = (char*)malloc(sizeof(vu->user));` is probably allocating `400 bytes` (assuming 64 bit platform) for each user item. You probably need `strlen(result[z])+1` – LPs Nov 03 '16 at 08:28

1 Answers1

1

I guess, the problem is with the callback function. There you have this loop

for(i = 0; i < argc; i++)
{
    printf("\n %s = %s", azColName[i], argv[i] ? argv[i] : "NULL"); 
    result[i] = argv[i]; /* Getting database values */
}

In this loop you copy the pointers, but not the content. To copy the strings, you must allocate memory first and then do strcpy, e.g.

int len;
/* ... */
for(i = 0; i < argc; i++)
{
    printf("\n %s = %s", azColName[i], argv[i] ? argv[i] : "NULL");
    if (argv[i] != NULL) {
        len = strlen(argv[i]);
        result[i] = malloc(len + 1); /* strlen() + NUL byte */
        strcpy(result[i], argv[i]); /* Getting database values */
    } else {
        result[i] = NULL;
    }
}
Olaf Dietsche
  • 66,104
  • 6
  • 91
  • 177