Skip to content

Commit 6b17ed4

Browse files
committed
Added bugfixes.
1 parent 17e4efb commit 6b17ed4

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ The following SQL schema will be created by the example:
4747

4848
```sql
4949
CREATE TABLE example_table (
50-
id INTEGER PRIMARY KEY AUTOINCREMENT,
50+
id INTEGER PRIMARY KEY,
5151
string VARCHAR(32),
5252
value INTEGER
5353
);
@@ -71,7 +71,7 @@ program example
7171
7272
! Create table.
7373
rc = sqlite3_exec(db, "CREATE TABLE example_table (" // &
74-
"id INTEGER PRIMARY KEY AUTOINCREMENT," // &
74+
"id INTEGER PRIMARY KEY," // &
7575
"string VARCHAR(32)," // &
7676
"value INTEGER)", c_null_ptr, c_null_ptr, errmsg)
7777
if (rc /= SQLITE_OK) print '("sqlite3_exec(): ", a)', errmsg

src/sqlite.f90

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,9 @@ end function sqlite3_libversion_number
299299
! int sqlite3_open(const char *filename, sqlite3 **db)
300300
function sqlite3_open_(filename, db) bind(c, name='sqlite3_open')
301301
import :: c_char, c_int, c_ptr
302-
character(kind=c_char), intent(in) :: filename
303-
type(c_ptr), intent(in) :: db
304-
integer(kind=c_int) :: sqlite3_open_
302+
character(kind=c_char), intent(in) :: filename
303+
type(c_ptr), intent(inout) :: db
304+
integer(kind=c_int) :: sqlite3_open_
305305
end function sqlite3_open_
306306

307307
! int sqlite3_prepare(sqlite3 *db, const char *sql, int nbyte, sqlite3_stmt **stmt, const char **tail)
@@ -310,7 +310,7 @@ function sqlite3_prepare_(db, sql, nbyte, stmt, tail) bind(c, name='sqlite3_prep
310310
type(c_ptr), intent(in), value :: db
311311
character(kind=c_char), intent(in) :: sql
312312
integer(kind=c_int), intent(in), value :: nbyte
313-
type(c_ptr), intent(in) :: stmt
313+
type(c_ptr), intent(inout) :: stmt
314314
type(c_ptr), intent(in) :: tail
315315
integer(kind=c_int) :: sqlite3_prepare_
316316
end function sqlite3_prepare_
@@ -471,7 +471,7 @@ subroutine c_f_str_ptr(c_str, f_str)
471471

472472
if (.not. c_associated(c_str)) return
473473
sz = c_strlen(c_str)
474-
if (sz <= 0) return
474+
if (sz < 0) return
475475
call c_f_pointer(c_str, ptrs, [ sz ])
476476
allocate (character(len=sz) :: f_str)
477477
f_str = copy(ptrs)

test/test_sqlite.f90

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ subroutine update_callback(udp, type, db_name, tbl_name, rowid) bind(c)
6262
print '("Row ", i0, " in table ", a, " of database ", a, " has been deleted!")', &
6363
rowid, tbl_str, db_str
6464
end select
65+
66+
if (allocated(db_str)) deallocate(db_str)
67+
if (allocated(db_str)) deallocate(tbl_str)
6568
end subroutine update_callback
6669
end module callbacks
6770

@@ -90,16 +93,16 @@ program test_sqlite
9093

9194
! Create table.
9295
rc = sqlite3_exec(db, "CREATE TABLE " // DB_TABLE // " (" // &
93-
"id INTEGER PRIMARY KEY AUTOINCREMENT," // &
96+
"id INTEGER PRIMARY KEY," // &
9497
"string VARCHAR(32)," // &
9598
"value INTEGER)", &
96-
c_null_ptr, c_null_ptr, errmsg)
97-
if (rc /= SQLITE_OK) print '("sqlite3_exec(): ", a)', errmsg
99+
c_null_funptr, c_null_ptr, errmsg)
100+
call print_error(rc, 'sqlite3_exec', errmsg)
98101

99102
! Insert values.
100103
rc = sqlite3_exec(db, "INSERT INTO " // DB_TABLE // "(string, value) VALUES('one', 12345)", &
101-
c_null_ptr, c_null_ptr, errmsg)
102-
if (rc /= SQLITE_OK) print '("sqlite3_exec(): ", a)', errmsg
104+
c_null_funptr, c_null_ptr, errmsg)
105+
call print_error(rc, 'sqlite3_exec', errmsg)
103106

104107
! Prepare statement.
105108
rc = sqlite3_prepare(db, "INSERT INTO " // DB_TABLE // "(string, value) VALUES(?, ?)", stmt)
@@ -131,7 +134,7 @@ program test_sqlite
131134
if (rc /= SQLITE_OK) print '("sqlite3_finalize(): failed")'
132135

133136
! Read values.
134-
print '(/, a)', '--- TESTING PREPARE/STEP'
137+
print '(/, "--- TESTING PREPARE/STEP")'
135138
rc = sqlite3_prepare(db, "SELECT * FROM " // DB_TABLE, stmt)
136139
if (rc /= SQLITE_OK) print '("sqlite3_prepare(): failed")'
137140

@@ -140,23 +143,33 @@ program test_sqlite
140143
end do
141144

142145
rc = sqlite3_finalize(stmt)
143-
if (rc /= SQLITE_OK) print '("sqlite3_finalize(): failed")'
146+
call print_error(rc, 'sqlite3_finalize', errmsg)
144147

145148
! Read values using callback function.
146-
print '(/, a)', '--- TESTING CALLBACK FUNCTION'
149+
print '(/, "--- TESTING CALLBACK FUNCTION")'
147150
rc = sqlite3_exec(db, "SELECT * FROM " // DB_TABLE, &
148151
c_funloc(exec_callback), c_null_ptr, errmsg)
149-
if (rc /= SQLITE_OK) print '("sqlite3_exec(): ", a)', errmsg
152+
call print_error(rc, 'sqlite3_exec', errmsg)
150153

151154
! Close SQLite handle.
152155
rc = sqlite3_close(db)
153156
if (rc /= SQLITE_OK) stop 'sqlite3_close(): failed'
154157
contains
158+
subroutine print_error(rc, func, errmsg)
159+
integer, intent(in) :: rc
160+
character(len=*), intent(in) :: func
161+
character(len=:), allocatable, intent(inout) :: errmsg
162+
163+
if (rc /= SQLITE_OK) print '(a, "(): ", a)', trim(func), errmsg
164+
if (allocated(errmsg)) deallocate(errmsg)
165+
end subroutine print_error
166+
155167
subroutine print_values(stmt, ncols)
156-
type(c_ptr), intent(inout) :: stmt
157-
integer, intent(in) :: ncols
158-
integer :: col_type
159-
integer :: i
168+
type(c_ptr), intent(inout) :: stmt
169+
integer, intent(in) :: ncols
170+
integer :: col_type
171+
integer :: i
172+
character(len=:), allocatable :: buf
160173

161174
do i = 0, ncols - 1
162175
col_type = sqlite3_column_type(stmt, i)
@@ -169,7 +182,11 @@ subroutine print_values(stmt, ncols)
169182
write (*, '(f0.8)', advance='no') sqlite3_column_double(stmt, i)
170183

171184
case (SQLITE_TEXT)
172-
write (*, '(a12)', advance='no') sqlite3_column_text(stmt, i)
185+
buf = sqlite3_column_text(stmt, i)
186+
if (allocated(buf)) then
187+
write (*, '(a12)', advance='no') buf
188+
deallocate(buf)
189+
end if
173190

174191
case default
175192
write (*, '(" unsupported")', advance='no')

0 commit comments

Comments
 (0)