Skip to content

Commit 535571e

Browse files
committed
Added sqlite3_errmsg().
1 parent fba5c70 commit 535571e

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/sqlite.f90

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ module sqlite
124124
public :: sqlite3_column_type
125125
public :: sqlite3_data_count
126126
public :: sqlite3_db_status
127+
public :: sqlite3_errmsg
128+
public :: sqlite3_errmsg_
127129
public :: sqlite3_exec
128130
public :: sqlite3_exec_
129131
public :: sqlite3_finalize
@@ -155,6 +157,8 @@ module sqlite
155157
public :: c_f_str_ptr
156158
public :: c_strlen
157159

160+
private :: copy
161+
158162
interface
159163
! int sqlite3_bind_double(sqlite3_stmt *stmt, int idx, double val)
160164
function sqlite3_bind_double(stmt, idx, val) bind(c, name='sqlite3_bind_double')
@@ -259,7 +263,14 @@ function sqlite3_db_status(db, op, current, highwater, reset_flag) bind(c, name=
259263
integer(kind=c_int) :: sqlite3_db_status
260264
end function sqlite3_db_status
261265

262-
! int sqlite3_exec(sqlite3* db, const char *sql, int (*callback)(void *, int, char **, char **), void *client_data, char **errmsg)
266+
! const char *sqlite3_errmsg(sqlite3 *db)
267+
function sqlite3_errmsg_(db) bind(c, name='sqlite3_errmsg')
268+
import :: c_ptr
269+
type(c_ptr), intent(in), value :: db
270+
type(c_ptr) :: sqlite3_errmsg_
271+
end function sqlite3_errmsg_
272+
273+
! int sqlite3_exec(sqlite3 *db, const char *sql, int (*callback)(void *, int, char **, char **), void *client_data, char **errmsg)
263274
function sqlite3_exec_(db, sql, callback, client_data, errmsg) bind(c, name='sqlite3_exec')
264275
import :: c_char, c_funptr, c_int, c_ptr
265276
type(c_ptr), intent(in), value :: db
@@ -277,7 +288,7 @@ function sqlite3_finalize(stmt) bind(c, name='sqlite3_finalize')
277288
integer(kind=c_int) :: sqlite3_finalize
278289
end function sqlite3_finalize
279290

280-
! sqlite3_int64 sqlite3_last_insert_rowid(sqlite3* db)
291+
! sqlite3_int64 sqlite3_last_insert_rowid(sqlite3 *db)
281292
function sqlite3_last_insert_rowid(db) bind(c, name='sqlite3_last_insert_rowid')
282293
import :: c_int64_t, c_ptr
283294
type(c_ptr), intent(in), value :: db
@@ -505,6 +516,18 @@ function sqlite3_column_text(stmt, icol)
505516
call c_f_str_ptr(ptr, sqlite3_column_text)
506517
end function sqlite3_column_text
507518

519+
function sqlite3_errmsg(db)
520+
type(c_ptr), intent(in) :: db
521+
character(len=:), allocatable :: sqlite3_errmsg
522+
type(c_ptr) :: ptr
523+
524+
ptr = sqlite3_errmsg_(db)
525+
526+
if (c_associated(ptr)) then
527+
call c_f_str_ptr(ptr, sqlite3_errmsg)
528+
end if
529+
end function sqlite3_errmsg
530+
508531
function sqlite3_exec(db, sql, callback, client_data, errmsg)
509532
type(c_ptr), intent(in) :: db
510533
character(len=*), intent(in) :: sql

test/test_sqlite.f90

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@ program test_sqlite
9191
! Register update hook.
9292
udp = sqlite3_update_hook(db, c_funloc(update_callback), c_null_ptr)
9393

94+
! Query SQLite version.
95+
rc = sqlite3_prepare(db, "SELECT SQLITE_VERSION()", stmt)
96+
if (rc /= SQLITE_OK) print '("sqlite3_prepare(): failed")'
97+
98+
if (rc /= SQLITE_OK) then
99+
print '("Failed to fetch data: ", a)', sqlite3_errmsg(db)
100+
else
101+
rc = sqlite3_step(stmt)
102+
103+
if (rc == SQLITE_ROW) then
104+
print '("SQLite version from query: ", a)', sqlite3_column_text(stmt, 0)
105+
end if
106+
end if
107+
108+
rc = sqlite3_finalize(stmt)
109+
if (rc /= SQLITE_OK) print '("sqlite3_finalize(): failed")'
110+
94111
! Create table.
95112
rc = sqlite3_exec(db, "CREATE TABLE " // DB_TABLE // " (" // &
96113
"id INTEGER PRIMARY KEY," // &

0 commit comments

Comments
 (0)