Re: [sqlite] sqlite3_step getting core dumbed.
On 10 Feb 2012 at 07:55, bhaskarReddywrote: > PRAGMA table_info(yourtablename); will display colNumber, colName, colType, > > ex: 0|slotId|INTEGER|0||0 > 1|ponChannelId|INTEGER|0||0 > 2|onuType|INTEGER|0||0 > 3|onuSerialNumber|TEXT|0||0 > 4|onuId|INTEGER|0||0 > 5|plannedSwVersion|TEXT|0||0 > 6|adminStatus|INTEGER|0||0 > > In that how can we extract INTEGER. I expect that in the result set, third column (index 2) says INTEGER. -- Cheers -- Tim ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] sqlite3_step getting core dumbed.
PRAGMA table_info(yourtablename); will display colNumber, colName, colType, ex: 0|slotId|INTEGER|0||0 1|ponChannelId|INTEGER|0||0 2|onuType|INTEGER|0||0 3|onuSerialNumber|TEXT|0||0 4|onuId|INTEGER|0||0 5|plannedSwVersion|TEXT|0||0 6|adminStatus|INTEGER|0||0 In that how can we extract INTEGER. Regards, Bhaskar. Kees Nuyt wrote: > > On Thu, 9 Feb 2012 05:07:05 -0800 (PST), bhaskarReddy> wrote: > >> >>Is it possible to find the data type, even if the table doesn't have data. >>That means, my requirement is, >> >>1. I want to find the number of columns in the table. ( I already done.) >>2. I want to find the names of the columns. (I already done.) >>3. I want to find the data types. Because, if it is an integer i'll use >>s***_bind_int(), else if it is test i'll use s***_bind_text(). >> >>Now if the table is empty, i want to find the data types and according to >>that i want to fill the row. >> >>So is there any way to find the data types even if the table is empty. >>(i.e., Table is there, columns and their names are there but no data in that >>table.) > > http://www.sqlite.org/c3ref/table_column_metadata.html > or > http://www.sqlite.org/c3ref/column_decltype.html > or execute the > PRAGMA table_info(yourtablename); > statement and process the resultset. > >>Regards, >>Bhaskar. > > -- > Regards, > > Kees Nuyt > > ___ > sqlite-users mailing list > sqlite-users@sqlite.org > http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users > > -- View this message in context: http://old.nabble.com/sqlite3_step-getting-core-dumbed.-tp33292180p33298627.html Sent from the SQLite mailing list archive at Nabble.com. ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] sqlite3_step getting core dumbed.
On Thu, 9 Feb 2012 05:07:05 -0800 (PST), bhaskarReddywrote: > >Is it possible to find the data type, even if the table doesn't have data. >That means, my requirement is, > >1. I want to find the number of columns in the table. ( I already done.) >2. I want to find the names of the columns. (I already done.) >3. I want to find the data types. Because, if it is an integer i'll use >s***_bind_int(), else if it is test i'll use s***_bind_text(). > >Now if the table is empty, i want to find the data types and according to >that i want to fill the row. > >So is there any way to find the data types even if the table is empty. >(i.e., Table is there, columns and their names are there but no data in that >table.) http://www.sqlite.org/c3ref/table_column_metadata.html or http://www.sqlite.org/c3ref/column_decltype.html or execute the PRAGMA table_info(yourtablename); statement and process the resultset. >Regards, >Bhaskar. -- Regards, Kees Nuyt ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] sqlite3_step getting core dumbed.
Is it possible to find the data type, even if the table doesn't have data. That means, my requirement is, 1. I want to find the number of columns in the table. ( I already done.) 2. I want to find the names of the columns. (I already done.) 3. I want to find the data types. Because, if it is an integer i'll use s***_bind_int(), else if it is test i'll use s***_bind_text(). Now if the table is empty, i want to find the data types and according to that i want to fill the row. So is there any way to find the data types even if the table is empty. (i.e., Table is there, columns and their names are there but no data in that table.) Regards, Bhaskar. Marcus Grimm wrote: > > > On 09.02.2012 13:12, bhaskarReddy wrote: >> >> Thank you. Its working. >> >> But why i am getting sqlite3_column_type returning 5. >> > > I can only guess: > > a) because you have no data in that table. > b) probably you have no known type defined in table > definition and sqlite does a conversion anyhow. > c) is sqlite3_step(stmt); really required here ? > what is the result of step ? is it SQLITE_ROW ? > If not, probably sqlite3_column_type doesn't make much sense. > > > Marcus > > >> >> >> >> Marcus Grimm wrote: >>> >>> 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 #include #include "sqlitedb1.h" #include #include #include #include #include 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,); 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,, 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,, 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"); }
Re: [sqlite] sqlite3_step getting core dumbed.
On 09.02.2012 13:12, bhaskarReddy wrote: Thank you. Its working. But why i am getting sqlite3_column_type returning 5. I can only guess: a) because you have no data in that table. b) probably you have no known type defined in table definition and sqlite does a conversion anyhow. c) is sqlite3_step(stmt); really required here ? what is the result of step ? is it SQLITE_ROW ? If not, probably sqlite3_column_type doesn't make much sense. Marcus Marcus Grimm wrote: 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 #include #include "sqlitedb1.h" #include #include #include #include #include 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,); 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,, 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,, 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,, 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
Re: [sqlite] sqlite3_step getting core dumbed.
Thank you. Its working. But why i am getting sqlite3_column_type returning 5. Marcus Grimm wrote: > > 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 >> #include >> #include "sqlitedb1.h" >> #include >> #include >> #include >> #include >> #include >> >> 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,); >> 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,, 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,, >> 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,, 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. >> >
Re: [sqlite] sqlite3_step getting core dumbed.
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 #include #include "sqlitedb1.h" #include #include #include #include #include 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,); 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,, 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,, 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,, 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
Re: [sqlite] sqlite3_step getting core dumbed.
No, that is not problem. Taleeb Anwar wrote: > > /* 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,","); > } > > The problem is, perhaps, in the query created through this loop. An extra > ',' will get appended after the last column name. that is if you have two > columns called column1 and column2 so your query will be > SELECT column1,column2, FROM... //(note ',' after column2) > > I may be wrong for more or less I've forgotten "C"; but personally I think > the sqlite should be throwing exception... > > Thanks & Regards > Taleeb bin Waquar > > *Hum Mashriq Ke "Miskeenon" Ka Dil Maghrib men Ja Atka Hai!!* > > > > > On Thu, Feb 9, 2012 at 3:51 PM, bhaskarReddywrote: > >> >> HI Friends, >> >> I dont know why the sqlite3_step getting core dumped. >> >> #include >> #include >> #include "sqlitedb1.h" >> #include >> #include >> #include >> #include >> #include >> >> 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, ); >> 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, , 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, , >> 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, , 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] =
Re: [sqlite] sqlite3_step getting core dumbed.
/* 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,","); } The problem is, perhaps, in the query created through this loop. An extra ',' will get appended after the last column name. that is if you have two columns called column1 and column2 so your query will be SELECT column1,column2, FROM... //(note ',' after column2) I may be wrong for more or less I've forgotten "C"; but personally I think the sqlite should be throwing exception... Thanks & Regards Taleeb bin Waquar *Hum Mashriq Ke "Miskeenon" Ka Dil Maghrib men Ja Atka Hai!!* On Thu, Feb 9, 2012 at 3:51 PM, bhaskarReddywrote: > > HI Friends, > > I dont know why the sqlite3_step getting core dumped. > > #include > #include > #include "sqlitedb1.h" > #include > #include > #include > #include > #include > > 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, ); > 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, , 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, , 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, , 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);