Skip to content

Commit da8f106

Browse files
committed
allow to safely close a sqlite db
1 parent e723465 commit da8f106

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/SQLite.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using DataFrames
44

55
export sqlitedb, readdlmsql, query, createtable, droptable
66

7-
import Base: show
7+
import Base: show, close
88

99
include("SQLite_consts.jl")
1010
include("SQLite_api.jl")
@@ -15,7 +15,7 @@ type SQLiteDB
1515
resultset::Any
1616
end
1717
function show(io::IO,db::SQLiteDB)
18-
if db == null_SQLiteDB
18+
if db.handle == C_NULL
1919
print(io,"Null sqlite connection")
2020
else
2121
println(io,"sqlite connection")
@@ -49,6 +49,16 @@ function connect(file::String)
4949
return (sqlitedb = SQLiteDB(file,handle[1],null_resultset))
5050
end
5151
end
52+
function close(conn::SQLiteDB)
53+
# if is fine to close when conn.handle is NULL (as stated in sqlite3's document)
54+
if @FAILED sqlite3_close(conn.handle)
55+
error("[sqlite]: Error closing $(conn.file); $(bytestring(sqlite3_errmsg(conn.handle)))")
56+
else
57+
conn.file = ""
58+
conn.handle = C_NULL
59+
conn.resultset = null_resultset
60+
end
61+
end
5262
function internal_query(conn::SQLiteDB,q::String,finalize::Bool=true,stepped::Bool=true)
5363
stmt = Array(Ptr{Void},1)
5464
if @FAILED sqlite3_prepare_v2(conn.handle,utf8(q),stmt,[C_NULL])

0 commit comments

Comments
 (0)