如何使用C从数据库中将字符串分配和读取到结构中的指针变量数组中

时间:2016-11-03 07:53:13

标签: c arrays sqlite pointers structure

我需要显示数据库中的行值集。我已经将字符串分配到数据库结构中的指针变量数组中。从指针变量数组中读取字符串时,它返回空值。

预期产出:

用户名:admin
密码:admin
状态:活跃的 创建时间:10月19日星期三17:53:46 2016 更新时间:2016年10月31日星期一15:26:53

我得到的实际输出:

用户名:
密码:
状态:
创造时间:
更新时间:

#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;
}

从数据库获取值并以字符串格式显示的任何其他最佳方法。

1 个答案:

答案 0 :(得分:1)

我猜,问题在于回调函数。你有这个循环

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

在此循环中,您复制指针,但不复制内容。要复制字符串,必须先分配内存,然后执行strcpy,例如

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;
    }
}