Re: [sqlite] Odd issue when SELECT querying

2008-01-30 Thread David Hautbois

Thanks all !

I will receive a C book tomorrow.
I need it.

David.

James Dennett wrote:

David Hautbois wrote:

  

Hi
I have an odd issue.


My function :

char * get_config_value (sqlite3 * db, char * config_name) {



[...]
 
  

configvalue = (char *) sqlite3_column_text(stmt, 0);



[...]

  

return configvalue;
}
Why the variable content changes ??

Why the variable configvalue has not the same content ??



The variable has the same content (a pointer), but the pointer is
invalid by the time your function returns it.  You need to copy the
*string*, not just a pointer to it.

This is essentially the same issue that Igor described when he wrote:

  
Strings passed to the callback are valid only within the callback. As 
soon as the callback returns, the memory may be deallocated or reused 
for other purposes. If the callback wants to keep some strings around 
beyond a single call, it should allocate its own memory and copy the 
value over.



It's vitally important when using C libraries that you read the
documentation and avoid making any assumptions about the lifetimes of
objects referenced by pointers.

C++ wrappers can return std::string objects and avoid this issue (though
even in C++ it's important to consider validity/lifetime issues for both
pointers and iterators).

-- James


-
To unsubscribe, send email to [EMAIL PROTECTED]
-


  


--
Web site : http://david.hautbois.free.fr
Tablet users map : http://david.hautbois.free.fr/maps/index.php



-
To unsubscribe, send email to [EMAIL PROTECTED]
-



Re: [sqlite] Odd issue when SELECT querying

2008-01-30 Thread David Hautbois

I found the solution :
I replaced this line :
configvalue = (char *) sqlite3_column_text(stmt, 0);
by
configvalue = g_strdup((gchar *) sqlite3_column_text(stmt, 0));

and the configvalue type : gchar

Now it works !!

A newbie error...

David.

David Hautbois wrote:

Hi
I have an odd issue.

My database schema :
sqlite> .schema
CREATE TABLE config  (id INTEGER PRIMARY KEY,  config_name TEXT ,  
config_value TEXT);
CREATE TABLE waypoints (id INTEGER PRIMARY KEY, wp_date INTEGER, 
wp_longitude REAL, wp_latitude REAL, wp_speed REAL);


The config table content :
sqlite> SELECT * from config;
1|version|1
2|ftpserver|A
3|ftp_remotedir|
4|ftp_login|
5|ftp_password|

My function :

char * get_config_value (sqlite3 * db, char * config_name) {

   int ncols;
   sqlite3_stmt *stmt;
   char *sql;
   const char *tail;
   char * configvalue = 0;

   sql = g_strdup_printf("SELECT config_value FROM config WHERE 
config_name='%s'\n", config_name);

   printf (sql);
   sqlite3_prepare(db , sql, (int)strlen(sql), , );

   ncols = sqlite3_column_count(stmt);

   while(sqlite3_step(stmt) == SQLITE_ROW) {
   printf ("gpstracer-cfg.c - get_config_value : Getting column 
content\n");

   configvalue = (char *) sqlite3_column_text(stmt, 0);
   printf ("gpstracer-cfg.c - get_config_value : content=%s\n", 
configvalue);  > note this line

   }
   printf ("gpstracer-cfg.c - get_config_value : %s=%s\n", 
config_name, configvalue);  >and this one

   sqlite3_finalize(stmt);

   return configvalue;
}
***

When I call this function : get_config_value (db, "version")

I get :
SELECT config_value FROM config WHERE config_name='version'
gpstracer-cfg.c - get_config_value : Getting column content
gpstracer-cfg.c - get_config_value : 
content=1   > ok
gpstracer-cfg.c - get_config_value : version=ftp_password  
> Why the variable content changes ??


Why the variable configvalue has not the same content ??

It's the same issue than my previous post (Callback issue - using 
pointer as argument), when I used a callback.


Thanks.

David.




--
Web site : http://david.hautbois.free.fr
Tablet users map : http://david.hautbois.free.fr/maps/index.php



-
To unsubscribe, send email to [EMAIL PROTECTED]
-



[sqlite] Odd issue when SELECT querying

2008-01-30 Thread David Hautbois

Hi
I have an odd issue.

My database schema :
sqlite> .schema
CREATE TABLE config  (id INTEGER PRIMARY KEY,  config_name TEXT ,  
config_value TEXT);
CREATE TABLE waypoints (id INTEGER PRIMARY KEY, wp_date INTEGER, 
wp_longitude REAL, wp_latitude REAL, wp_speed REAL);


The config table content :
sqlite> SELECT * from config;
1|version|1
2|ftpserver|A
3|ftp_remotedir|
4|ftp_login|
5|ftp_password|

My function :

char * get_config_value (sqlite3 * db, char * config_name) {

   int ncols;
   sqlite3_stmt *stmt;
   char *sql;
   const char *tail;
   char * configvalue = 0;

   sql = g_strdup_printf("SELECT config_value FROM config WHERE 
config_name='%s'\n", config_name);

   printf (sql);
   sqlite3_prepare(db , sql, (int)strlen(sql), , );

   ncols = sqlite3_column_count(stmt);

   while(sqlite3_step(stmt) == SQLITE_ROW) {
   printf ("gpstracer-cfg.c - get_config_value : Getting column 
content\n");

   configvalue = (char *) sqlite3_column_text(stmt, 0);
   printf ("gpstracer-cfg.c - get_config_value : content=%s\n", 
configvalue);  > note this line

   }
   printf ("gpstracer-cfg.c - get_config_value : %s=%s\n", config_name, 
configvalue);  >and this one

   sqlite3_finalize(stmt);

   return configvalue;
}
***

When I call this function : get_config_value (db, "version")

I get :
SELECT config_value FROM config WHERE config_name='version'
gpstracer-cfg.c - get_config_value : Getting column content
gpstracer-cfg.c - get_config_value : 
content=1   > ok
gpstracer-cfg.c - get_config_value : version=ftp_password  > 
Why the variable content changes ??


Why the variable configvalue has not the same content ??

It's the same issue than my previous post (Callback issue - using 
pointer as argument), when I used a callback.


Thanks.

David.


--
Web site : http://david.hautbois.free.fr
Tablet users map : http://david.hautbois.free.fr/maps/index.php



-
To unsubscribe, send email to [EMAIL PROTECTED]
-



Re: [sqlite] Re: Callback issue - using pointer as argument

2008-01-30 Thread David HAUTBOIS

It works better.

Thanks.

David.

Igor Tandetnik wrote:

David Hautbois <[EMAIL PROTECTED]> wrote:


char * get_config_value (sqlite3 * db, char * config_name) {

   TabResult res;

   rc= sqlite3_exec( db, query, exec_get_config_value_cb , ,
);


Strings passed to the callback are valid only within the callback. As 
soon as the callback returns, the memory may be deallocated or reused 
for other purposes. If the callback wants to keep some strings around 
beyond a single call, it should allocate its own memory and copy the 
value over.


Better still, stop using sqlite3_exec and switch over to 
sqlite3_prepare / sqlite3_step / sqlite3_finalize interface.


Igor Tandetnik

- 


To unsubscribe, send email to [EMAIL PROTECTED]
- 





--
http://david.hautbois.free.fr
http://slugplayer.free.fr


-
To unsubscribe, send email to [EMAIL PROTECTED]
-



[sqlite] Callback issue - using pointer as argument

2008-01-29 Thread David Hautbois

Hi
I spent 6 hours on this issue and I don't understand why I get a wrong 
value.


My database content :
sqlite> select * from config;
1|version|1
2|ftpserver|A
3|ftp_remotedir|
4|ftp_login|
5|ftp_password|

A simple query :
sqlite> SELECT config_value FROM config WHERE config_name="version";
1

Ok, all right.

Now I try to execute the same query with this code :

*
typedef struct TabResult {
 char *value;
} TabResult;

/***/
/* Get a config 
value  
*/

/***/

char * get_config_value (sqlite3 * db, char * config_name) {

   gchar * query;
   int rc;
   char *zErrMsg = 0;
   char *config_value=0;
   TabResult res;
   
   query = g_strdup_printf("SELECT config_value FROM config WHERE 
config_name='version'\n");

   printf (query);
   rc= sqlite3_exec( db, query, exec_get_config_value_cb , , );
 
   if (rc!=SQLITE_OK) {

   fprintf(stderr, "SQL error: %s\n", zErrMsg);
   sqlite3_free(zErrMsg);
   return NULL;
   }
   printf ("gpstracer-cfg.c - get_config_value : %s : %s\n", 
config_name, (char *)res.value);

   return config_value;
}


/***/
/* Callback of the get_config_value 
query  */

/***/

static int exec_get_config_value_cb(void *result, int argc, char **argv, 
char **azColName) {


   int i;
   TabResult *p = (TabResult*)result;

   for(i=0; ivalue = argv[0];
   printf ("gpstracer-cfg.c - exec_get_config_value_cb : returned value 
: %s\n", argv[0]);

   printf ("%s,%s\n", p->value, argv[0]);
   return 0;
}
*

When I call the function :
get_config_value (sqlite3 * db, "version")

I get :
1: SELECT config_value FROM config WHERE config_name="version"
2: 0 : config_value = 1
3:
4: gpstracer-cfg.c - exec_get_config_value_cb : returned value : 1
5: 
1,1   
-> ok
6: gpstracer-cfg.c - get_config_value : version : 
ftp_password   ->nok


Line #4 : all right : version = 1
Line #5 : The returned value change to "ftp-password" !

I don't know why !

I use libsqlite3-0 3.4.1

Can some help me to understand this ?

Thanks.

David.

--
Web site : http://david.hautbois.free.fr
Tablet users map : http://david.hautbois.free.fr/maps/index.php



-
To unsubscribe, send email to [EMAIL PROTECTED]
-