Re: [sqlite] Reference to an undefined field

2013-07-10 Thread Stephan Beal
On Wed, Jul 10, 2013 at 12:37 AM, Igor Korot  wrote:

> Hi, ALL,
> Consider following code:
>
> std::string query = "SELECT a FROM foo;";
>
> sqlite3_prepare_v2( handle, query, -1, , 0 );
> sqlite3_step( stmt );

int id = sqlite_column_int( stmt, 0 );
>

Right here the results are undefined. sqlite3_column_xxx() and friends are
only legal to call if step() has succeeded, and you code skips over the
error check, making any further analysis of errors essentially meaningless.


> int code = sqlite3_column_int( stmt, 1 );
>




>
> Shouldn't the engine assert in this case?
>

The engine expects you to do the right thing with the result codes - they
are there for a reason. Assertion is a last-ditch safety effort which is
only enabled in debug builds.


-- 
- 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] Reference to an undefined field

2013-07-09 Thread Simon Slavin

On 9 Jul 2013, at 11:54pm, Igor Korot  wrote:

> Shouldn't the engine warn you?
> What I mean is: developer don't know that he fail with the query results
> until further execution or running the program under debugger and explore
> the data.
> I'm not saying it's wrong, I'm just trying to understand the reasoning...

It would indeed be useful to have the developer warned.  But look what it would 
take in this situation.  The call is

int sqlite3_column_int(sqlite3_stmt*, int iCol)

The normal way the API warns of errors is by returning a value other than 
SQLITE_OK.  But this call is unusual: instead of returning a result code it 
returns the value of the field specified.  So to conform with the normal API 
pattern the call would need to be changed to be more bulky and annoying to use 
which everyone would hate.

So the alternative way of warning of an error is an assert.  But SQLite doesn’t 
use this method of warning (except, IIRC, when a compilation option for 
debugging is set).  And an asset is not trappable by the program calling the 
API.  Which would be nontrappable by the programmer, which is not the way 
SQLite normally does things.  So again, you have somthing which wouldn’t 
conform to the usual SQLite way of doing things.

So there’s no good way to deliver what would be this useful warning without 
changing expectations about the SQLite API.  The only elegant thing to do is to 
rely on the programmer calling sqlite3_column_count() in the weird and unusual 
situation where they don’t know how many columns to expect.

By the way, does anyone understand the difference between 
sqlite3_column_count() and sqlite3_data_count() ?  I mean, I can read the 
definitions, but why are they both provided ?  Is one more useful than another 
sometimes ?

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


Re: [sqlite] Reference to an undefined field

2013-07-09 Thread Igor Korot
Igor,

On Tue, Jul 9, 2013 at 3:42 PM, Igor Tandetnik  wrote:

> On 7/9/2013 6:37 PM, Igor Korot wrote:
>
>> Hi, ALL,
>> Consider following code:
>>
>> std::string query = "SELECT a FROM foo;";
>>
>> sqlite3_prepare_v2( handle, query, -1, , 0 );
>> sqlite3_step( stmt );
>> int id = sqlite_column_int( stmt, 0 );
>> int code = sqlite3_column_int( stmt, 1 );
>>
>> Shouldn't the engine assert in this case?
>>
>
> http://www.sqlite.org/c3ref/**column_blob.html
> If the SQL statement does not currently point to a valid row, or if the
> column index is out of range, the result is undefined.
>

Shouldn't the engine warn you?
What I mean is: developer don't know that he fail with the query results
until further execution or running the program under debugger and explore
the data.
I'm not saying it's wrong, I'm just trying to understand the reasoning...

Thank you.


>
> --
> Igor Tandetnik
>
> __**_
> 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] Reference to an undefined field

2013-07-09 Thread Igor Tandetnik

On 7/9/2013 6:37 PM, Igor Korot wrote:

Hi, ALL,
Consider following code:

std::string query = "SELECT a FROM foo;";

sqlite3_prepare_v2( handle, query, -1, , 0 );
sqlite3_step( stmt );
int id = sqlite_column_int( stmt, 0 );
int code = sqlite3_column_int( stmt, 1 );

Shouldn't the engine assert in this case?


http://www.sqlite.org/c3ref/column_blob.html
If the SQL statement does not currently point to a valid row, or if the 
column index is out of range, the result is undefined.


--
Igor Tandetnik

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


[sqlite] Reference to an undefined field

2013-07-09 Thread Igor Korot
Hi, ALL,
Consider following code:

std::string query = "SELECT a FROM foo;";

sqlite3_prepare_v2( handle, query, -1, , 0 );
sqlite3_step( stmt );
int id = sqlite_column_int( stmt, 0 );
int code = sqlite3_column_int( stmt, 1 );

Shouldn't the engine assert in this case?

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