try changing this:

colNames = (char **)malloc(sizeof(char));

into something like

colNames = (char **)malloc(sizeof(char * ) * noOfColumns);


On 09.02.2012 11:21, bhaskarReddy wrote:

HI Friends,

           I dont know why the sqlite3_step getting core dumped.

  #include<stdio.h>
  #include<stdlib.h>
  #include "sqlitedb1.h"
  #include<string.h>
  #include<sqlite3.h>
  #include<sys/types.h>
  #include<sys/stat.h>
  #include<fcntl.h>

  int db_retrieve_row(char *db_name, char *table_name, int num_of_keys, char
* col_names[],column_value_t * col_values);

int main(void)
{
       column_type_t enm[2];
    //   int retVal;
       char *col_name[3];
       char *db_name = "LR6002.db";
       char *table_name = "ONTTable";
       column_value_t col_val[3];

       enm[0] = COLUMN_TYPE_INT;   // slotId
       enm[1] = COLUMN_TYPE_STRING;

       col_val[0].number = 1;
       col_val[1].number = 2;
       col_val[2].number = 3;
     /* Array of Column Names. */
      col_name[0] = "slotId";
      col_name[1] = "ponChannelId";
      col_name[2] = "onuType";

    db_retrieve_row(db_name, table_name, 3,  col_name,col_val);
    return 0;
  }
  int db_retrieve_row(char *db_name, char *table_name, int num_of_keys, char
* col_names[],column_value_t * col_values)
  {
      sqlite3 *db;
      sqlite3_stmt *stmt;
      int status = 0,loopVar,noOfColumns;
      char *query = NULL,**colNames,*str;
      int  retVal,*colType;

      retVal = sqlite3_open(db_name,&db);
      if(retVal) {
          fprintf(stderr,"Can't  open database: %s\n",sqlite3_errmsg(db));
          sqlite3_close(db);
          exit(1);
      }
     /* A Query to find the number  of columns in the table. */
     query = (char *)malloc(sizeof(char) * 255);
     memset(query,0,255);
     strcat(query, "SELECT * FROM ");
     strcat(query, table_name);
     strcat(query, ";");

     status = sqlite3_prepare_v2(db,query,strlen(query) + 1,&stmt, NULL);
     if(status != SQLITE_OK) {
         printf("Prepare error: %s\n", sqlite3_errmsg(db));
         exit(1);
     }
     noOfColumns = sqlite3_column_count(stmt);
     if(SQLITE_OK != sqlite3_finalize(stmt))
     {
          printf("The prepared statement is Not deleted.\n");
     }
     free(query);
     query = NULL;

       /* A Query to find the Names of each column. */
       query = (char *)malloc(sizeof(char) * 255);
      memset(query,0,255);
       strcat(query, "SELECT * FROM ");
       strcat(query, table_name);
       strcat(query, ";");

       //stmt = NULL;
       status = sqlite3_prepare_v2(db,query,strlen(query) + 1,&stmt, NULL);
      if(status != SQLITE_OK) {
          printf("Prepare error: %s\n", sqlite3_errmsg(db));
           exit(1);
       }

       colNames = (char **)malloc(sizeof(char));
       memset(colNames,0,1);

        for(loopVar = 0; loopVar<  noOfColumns; loopVar++)
        {
            colNames[loopVar] = (char *)malloc(sizeof(char) * 20);
            memset(colNames[loopVar], 0, 20);
             strcat(colNames[loopVar],sqlite3_column_name(stmt, loopVar));
        }
        if(SQLITE_OK != sqlite3_finalize(stmt))
        {
           printf("The prepared statement is Not deleted.\n");
        }
        free(query);
       query = NULL;

   /*  A Query to find types of each column. */
       str = (char *)malloc(sizeof(char) * 255);
        memset(str, 0, 255);
        strcat(str, "SELECT ");
        for(loopVar = 0; loopVar<  noOfColumns; loopVar++)
        {
          strcat(str, colNames[loopVar]);
          if(loopVar<  (noOfColumns -1))
                strcat(str,",");
        }
        strcat(str, " FROM ");
        strcat(str, table_name);
        strcat(str, ";");

        status=sqlite3_prepare_v2(db,str,strlen(str)+1,&stmt, NULL);
        if (status != SQLITE_OK) {
           printf("prepare error:%s\n",sqlite3_errmsg(db));
           exit(1);
       }
       sqlite3_step(stmt);       //Causing Segmentation Fault. //
       colType = (int *)malloc(sizeof(int) * noOfColumns);
       for(loopVar = 0; loopVar<  noOfColumns; loopVar++){
           colType[loopVar] = sqlite3_column_type(stmt, loopVar);
           printf("Column Types = %d\n",colType[loopVar]);
        }
       if(SQLITE_OK != sqlite3_finalize(stmt))
       {
          printf("The prepared statement is Not deleted.\n");
       }
       free(query);
       query = NULL;

}

And the API 'sqlite3_column_type" always returning 5, i.e., NULL.


Can any one tell me what was the problem that i did.


Regards,
Bhaskar.

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to