You're passing a char * to a routine that expects a char **, and then
immediately trying to indirect through it, which means it's taking the
text, treating it as a pointer, and passing the random data it's pointing
to as a string to sqlite.

On Sun., 21 Oct. 2018, 11:55 Ratheendran R, <ratheendra...@gmail.com> wrote:

> Hi,
>
> I am a embedded engineer and new to sqlite,we want to use sqlite for our
> local storage instead of file i/o.
>
> I have created a table with key and value records of char type,now if I try
> store a value with string length more than 50 char I get segmentation
> fault,please see the code below and let me know if I can improve it.
>
> sqlite3 *mod_init() {
>
>     /* Open database */
>     //rc = sqlite3_open("test.db", &dbObj->db);
>     lastError = sqlite3_open_v2("test.db", &db, SQLITE_OPEN_READWRITE |
> SQLITE_OPEN_CREATE | SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_NOMUTEX , NULL);
>     if( lastError ) {
>         fprintf(stderr, "Can't open database: %s\n",
> sqlite3_errmsg(dbObj->db));
>     free(query);
>     return(0);
>     } else {
>         fprintf(stdout, "Opened database successfully\n");
>     }
>     memset(query,0,200);
>     strcpy(query,"CREATE TABLE IF NOT EXISTS cosmos_db("  \
>     "key   TEXT PRIMARY KEY       NOT NULL," \
>     "value        VARCHAR(100));");
>
>     /* Execute SQL statement */
>     lastError = sqlite3_exec(db, query, 0, 0, &zErrMsg);
>
>
>     if( lastError != SQLITE_OK ){
>         fprintf(stderr, "SQL error: %s\n", zErrMsg);
>         sqlite3_free(zErrMsg);
>     } else {
>         fprintf(stdout, "Table created successfully\n");
>     }
> return db;
> }
>
>
>
> int mydef_set(cf_db_t *dbObj,char *key, char **value)
> {
>     char *zErrMsg = 0;
>     int rc;
>     sprintf(query,"INSERT OR REPLACE INTO cosmos_db (key,value) values
> ('%s', '%s');",key,*value);
>     /* Execute SQL statement */
>     lastError = sqlite3_exec(db, query, 0, 0, &zErrMsg);
>     if( lastError != SQLITE_OK ) {
>                   fprintf(stderr, "SQL error: %s\n", zErrMsg);
>                   sqlite3_free(zErrMsg);
>            } else {
>                   fprintf(stdout, "Update done successfully\n");
>            }
>     return lastError;
> }
>
>
> int main()
> {
>     sqlite3 *db;
>     db=mod_init();
>         mydef_set(db,"sssi","Hitjkahzdsdhdjksdhjsdhsjfhjsdhfjhsjd bcn
> bsdbgfhjsdgcsdfcbscbshdfgchdsfbbsdfcsfg");
> }
>
> Thanks,
> Ratheendran
> _______________________________________________
> sqlite-users mailing list
> sqlite-users@mailinglists.sqlite.org
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
>
_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to