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 James Dennett
David Hautbois [mailto:[EMAIL PROTECTED] wrote:
> 
> 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...

Now you just need to watch out for memory leaks.

-- James


-
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), &stmt, &tail);

   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] Odd issue when SELECT querying

2008-01-30 Thread James Dennett
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]
-



[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), &stmt, &tail);

   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]
-