SQLite Forum

Getting the error
Login

Getting the error

(1) By oneeyeman on 2021-03-17 06:40:17 [link] [source]

Hi, ALL,

Is it better to call sqlite3_errcode() or sqlite3_errmsg()?

What is the difference between those 2?

I presume both return SQLITE_OK on success (meaning error is successfully retrieved).

Thank you.

(2.1) By Gunter Hick (gunter_hick) on 2021-03-17 07:49:55 edited from 2.0 in reply to 1 [link] [source]

See https://sqlite.org/c3ref/errcode.html

sqlite3_errcode() returns the latest result_code/extended_result_code, which is probably not SQLITE_OK, because you would not be calling this unless one of the API calls has returned an error. Are you checking result codes on each and every call?

sqlite3_errmsg() returns a pointer to string containing the textual representation of the latest error. This is also not SQLITE_OK.

Use the interface that matches your needs...

(3.1) By oneeyeman on 2021-03-17 15:45:16 edited from 3.0 in reply to 2.1 [link] [source]

Hi, Gunter,

The only reason I'm asking is because I tried to call

[code]

sqlite3_prepare_v2()

sqlite3_errcode()

[/code]

and first returned 1 while second returned 0.

Any suggestions on debugging?

This is under MSVC 2017 / Windows 8.1

Thank you.

(4) By oneeyeman on 2021-03-17 15:44:40 in reply to 3.0 [link] [source]

Gunter, I tried to execute following:

[code]

SELECT * FROM master.leagues;

[/code]

I am connected to only 1 database (for the time being).

When I tried to use sqlite3_errmesg(), it returned:

[quote]

No such table exist: master.leagues

[/quote]

but the table "leagues" is definitely exist.

Besides the first API call shouldn't return 0 (SQLITE_OK) as you said...

Any help?

Thank you.

P.S.: I'm using 3.24.0 release.

(5.1) By Gunter Hick (gunter_hick) on 2021-03-17 15:57:17 edited from 5.0 in reply to 1 [link] [source]

Since I saw your currently waiting messages...

The schema name for the main database, i.e. the one attatched by the call to sqlite3_open(), is "main", not "master".

(6) By David Raymond (dvdraymond) on 2021-03-17 16:00:43 in reply to 4 [link] [source]

SELECT * FROM master.leagues;

I am connected to only 1 database (for the time being).

Not sure if this is what you meant, but the database you connect to at the start is "main". So you'd either want "select * from leagues;" or "select * from main.leagues;".

You'd only run the above if you had ATTACH'd a new database under the name of "master"

ATTACH DATABASE

(7.1) By Gunter Hick (gunter_hick) on 2021-03-17 16:05:19 edited from 7.0 in reply to 4 [source]

I strongly suggest testing your statements in the sqlite shell first, and listening to what SQLite says about them. You can handle problems with your API calls later, when you have already validated the SQL-

(8) By oneeyeman on 2021-03-17 16:40:01 in reply to 6 [link] [source]

Gunter & David,

Thank you guys.

When I come home I will see where the "master" comes from.

I should have queried the appropriate PRAGMA for the proper schema name.

Will check.

(9) By oneeyeman on 2021-03-17 16:42:03 in reply to 7.1 [link] [source]

Gunter,

The thing about "master" vs "main" was surprising. I thought I queried the PRAGMA for proper schema name. Apparently not.

Nevertheless - the issue with the _errcode() vs _errmsg() is weird...

Thank you.

(10) By Gunter Hick (gunter_hick) on 2021-03-18 06:59:20 in reply to 9 [link] [source]

It would be easier to help if you could show
- what you did
- what the result was
- why you think the result is incorrect

Ther is nothing weird about the _errcode() and _errmsg(). The former returns a numerical code that is useful for the calling program to decide a plan of action, while the latter returns a descriptive text that a programmer can use to determine the cause of the problem.

So what would "the PRAGMA for proper schema name" be, what did it return and how did you determine that "master" was the schema name of the database implicitly attached by calling sqlite3_open()?

(11) By oneeyeman on 2021-03-18 13:24:45 in reply to 10 [link] [source]

Hi, Gunter,

For a second lets forget about my last message.

This was my code:

if(  ( res = sqlite3_prepare_v2( m_db, "SELECT * FROM master.leagues", -1, &m_stmt, NULL ) ) != 0 )
{
    int code = sqlite_errcode( m_db );
}

This was my original code.

As you can see the _prepare call should fail and sqlite_errcode() line will be executed.

What I saw is the "code" variable will become 0 and therefore no error processing will occur.

When I changed that line to be:

char *message = sqlite_errmsg( m_db );

I did get an error message which said "No table master.leagues" and after I fixed the code everything started working correctly.

So, now the question is - are you able to get a 0 in my scenario #1?

I have MSVC 2017/Windows 8.1/SQLite 3.24.0 library embedded in the project.

Thank you.

P.S. Note - I hardcoded the query with the schema and table names to indicate a problem.