[sqlite] Is this code OK?

2013-09-19 Thread Igor Korot
Hi, ALL,
Here is the code I'm trying to use:

char *errmsg = NULL;
sqlite3_exec( handle, BEGIN, 0, 0, errmsg );
if( sqlite3_exec( , errmsg ) != SQLITE_OK )
{
   printf( Error executing query: %s, sqlite3_errmsg( m_handle ) );
   sqlite3_exec( handle, ROLLBACK, 0, 0, errmsg );
}

Can I reuse errmsg variable like this or do I have to call sqlite3_free()
and then execute ROLLBACK statement?

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


Re: [sqlite] Is this code OK?

2013-09-19 Thread Stephan Beal
On Thu, Sep 19, 2013 at 8:55 AM, Igor Korot ikoro...@gmail.com wrote:

 Can I reuse errmsg variable like this or do I have to call sqlite3_free()

and then execute ROLLBACK statement?


Per the API docs:

** ^If an error occurs while evaluating the SQL statements passed into
** sqlite3_exec(), then execution of the current statement stops and
** subsequent statements are skipped.  ^If the 5th parameter to
sqlite3_exec()
** is not NULL then any error message is written into memory obtained
** from [sqlite3_malloc()] and passed back through the 5th parameter.
** To avoid memory leaks, the application should invoke [sqlite3_free()]
** on error message strings returned through the 5th parameter of
** of sqlite3_exec() after the error message string is no longer needed.
** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
** NULL before returning.



-- 
- stephan beal
http://wanderinghorse.net/home/stephan/
http://gplus.to/sgbeal
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] Is this code OK?

2013-09-19 Thread Igor Korot
Stephan,

On Thu, Sep 19, 2013 at 12:46 AM, Stephan Beal sgb...@googlemail.comwrote:

 On Thu, Sep 19, 2013 at 8:55 AM, Igor Korot ikoro...@gmail.com wrote:

  Can I reuse errmsg variable like this or do I have to call sqlite3_free()

 and then execute ROLLBACK statement?
 

 Per the API docs:

 ** ^If an error occurs while evaluating the SQL statements passed into
 ** sqlite3_exec(), then execution of the current statement stops and
 ** subsequent statements are skipped.  ^If the 5th parameter to
 sqlite3_exec()
 ** is not NULL then any error message is written into memory obtained
 ** from [sqlite3_malloc()] and passed back through the 5th parameter.
 ** To avoid memory leaks, the application should invoke [sqlite3_free()]
 ** on error message strings returned through the 5th parameter of
 ** of sqlite3_exec() after the error message string is no longer needed.
 ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
 ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
 ** NULL before returning.


OK, so I guess I have to call sqlite3_free().
Now, I do have to execute ROLLBACK statement, right?

Thank you.





 --
 - stephan beal
 http://wanderinghorse.net/home/stephan/
 http://gplus.to/sgbeal
 ___
 sqlite-users mailing list
 sqlite-users@sqlite.org
 http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

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


Re: [sqlite] Is this code OK?

2013-09-19 Thread Stephan Beal
On Thu, Sep 19, 2013 at 10:06 AM, Igor Korot ikoro...@gmail.com wrote:

 OK, so I guess I have to call sqlite3_free().
 Now, I do have to execute ROLLBACK statement, right?


Correct - IMO, you have all the pieces in the right place, you just need to
free the errmsg string. (i assume you have a corresponding COMMIT somewhere
down further).)

But... you're not using the error message:

  printf( Error executing query: %s, sqlite3_errmsg( m_handle ) );
   sqlite3_exec( handle, ROLLBACK, 0, 0, errmsg );

instead you're using sqlite3_errmsg(), which means you don't need errmsg
unless you use it at another place which isn't shown here. What's the
difference between errmsg and calling sqlite3_errmsg()? i'm not sure there
is one, and one of the astute listers will probably see this and explain
the difference (if any) to us.


-- 
- stephan beal
http://wanderinghorse.net/home/stephan/
http://gplus.to/sgbeal
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] Is this code OK?

2013-09-19 Thread Igor Korot
Stephan,

On Thu, Sep 19, 2013 at 1:09 AM, Stephan Beal sgb...@googlemail.com wrote:

 On Thu, Sep 19, 2013 at 10:06 AM, Igor Korot ikoro...@gmail.com wrote:

  OK, so I guess I have to call sqlite3_free().
  Now, I do have to execute ROLLBACK statement, right?
 

 Correct - IMO, you have all the pieces in the right place, you just need to
 free the errmsg string. (i assume you have a corresponding COMMIT somewhere
 down further).)


Yes, I do. ;-)



 But... you're not using the error message:

   printf( Error executing query: %s, sqlite3_errmsg( m_handle ) );
sqlite3_exec( handle, ROLLBACK, 0, 0, errmsg );

 instead you're using sqlite3_errmsg(), which means you don't need errmsg
 unless you use it at another place which isn't shown here. What's the
 difference between errmsg and calling sqlite3_errmsg()? i'm not sure there
 is one, and one of the astute listers will probably see this and explain
 the difference (if any) to us.


It would be interesting to know the difference (if any).
At least I (and probably someone else) will know what  is better to use.

Thank you.




 --
 - stephan beal
 http://wanderinghorse.net/home/stephan/
 http://gplus.to/sgbeal
 ___
 sqlite-users mailing list
 sqlite-users@sqlite.org
 http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

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


Re: [sqlite] Is this code OK?

2013-09-19 Thread Simon Slavin

On 19 Sep 2013, at 9:09am, Stephan Beal sgb...@googlemail.com wrote:

 What's the
 difference between errmsg and calling sqlite3_errmsg()?

No difference in terms of the result, they're just to cope with two different 
programming styles.  The function is provided for situations where you have 
already lost touch with errmsg or never kept it to start with.

By the way, Igor, if this code is being shipped away from your own personal 
computer you should theoretically be checking the result returned by the 
execution of BEGIN too.  Depending on what goes wrong, it may be the BEGIN 
which fails, and the result code from subsequent operations wouldn't tell you 
what the real problem is.  Though you may have just simplified your code for 
posting.

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


Re: [sqlite] Is this code OK?

2013-09-19 Thread Igor Tandetnik

On 9/19/2013 2:55 AM, Igor Korot wrote:

Here is the code I'm trying to use:

char *errmsg = NULL;
sqlite3_exec( handle, BEGIN, 0, 0, errmsg );
if( sqlite3_exec( , errmsg ) != SQLITE_OK )


As you are not checking the return value of the first sqlite3_exec, and 
are not using the error message it returns - why do you pass errmsg at 
all? Just pass NULL there.


If you do pass a non-NULL pointer as the last parameter, then SQLite 
would allocate memory for it. You should then free said memory, or else 
you leak it.

--
Igor Tandetnik

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


Re: [sqlite] Is this code OK?

2013-09-19 Thread Igor Korot
Hi, Simon,


On Thu, Sep 19, 2013 at 5:16 AM, Simon Slavin slav...@bigfraud.org wrote:


 On 19 Sep 2013, at 9:09am, Stephan Beal sgb...@googlemail.com wrote:

  What's the
  difference between errmsg and calling sqlite3_errmsg()?

 No difference in terms of the result, they're just to cope with two
 different programming styles.  The function is provided for situations
 where you have already lost touch with errmsg or never kept it to start
 with.

 By the way, Igor, if this code is being shipped away from your own
 personal computer you should theoretically be checking the result returned
 by the execution of BEGIN too.  Depending on what goes wrong, it may be the
 BEGIN which fails, and the result code from subsequent operations wouldn't
 tell you what the real problem is.  Though you may have just simplified
 your code for posting.


Yes, the check code omitted for simplicity.

And thank you for clarification. It is hard to imagine the situation when
the variable will go out of scope, but that's OK.



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

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


Re: [sqlite] Is this code OK?

2013-09-19 Thread Igor Korot
Hi, Igor,


On Thu, Sep 19, 2013 at 5:54 AM, Igor Tandetnik i...@tandetnik.org wrote:

 On 9/19/2013 2:55 AM, Igor Korot wrote:

 Here is the code I'm trying to use:

 char *errmsg = NULL;
 sqlite3_exec( handle, BEGIN, 0, 0, errmsg );
 if( sqlite3_exec( , errmsg ) != SQLITE_OK )


 As you are not checking the return value of the first sqlite3_exec, and
 are not using the error message it returns - why do you pass errmsg at
 all? Just pass NULL there.


I'm. See my reply to Simon.
But as he explain, I changed my code to use sqlite3_errmsg() and am passing
0 to sqlite3_exec().



 If you do pass a non-NULL pointer as the last parameter, then SQLite would
 allocate memory for it. You should then free said memory, or else you leak
 it.


Yes, I understand that.
My question was more about re-using the variable between to calls to SQLite.

Thank you.


 --
 Igor Tandetnik


 __**_
 sqlite-users mailing list
 sqlite-users@sqlite.org
 http://sqlite.org:8080/cgi-**bin/mailman/listinfo/sqlite-**usershttp://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

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


Re: [sqlite] Is this code OK?

2013-09-19 Thread Igor Tandetnik

On 9/19/2013 5:43 PM, Igor Korot wrote:

If you do pass a non-NULL pointer as the last parameter, then SQLite would
allocate memory for it. You should then free said memory, or else you leak
it.


Yes, I understand that.
My question was more about re-using the variable between to calls to SQLite.


Well, you can reuse the variable *after* you free the memory it points 
to. Consider:


// OK
char* p = new char[42];
delete[] p;
p = new char[84];

// Not OK
char* p = new char[42];
p = new char[84];

Your situation is the same:  sqlite3_exec effectively acts as a memory 
allocation routine. It's not reusing the variable per se that's a 
problem, it's losing a pointer to memory that was allocated but not yet 
freed.

--
Igor Tandetnik

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