@@ -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
6669end 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'
154157contains
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