@@ -2,7 +2,9 @@ module SQLite
2
2
3
3
using DataFrames
4
4
5
- export sqlitedb, readdlmsql, query, connect, createtable, droptable
5
+ export sqlitedb, readdlmsql, query, createtable, droptable
6
+
7
+ import Base: show, close
6
8
7
9
include (" SQLite_consts.jl" )
8
10
include (" SQLite_api.jl" )
@@ -13,7 +15,7 @@ type SQLiteDB
13
15
resultset:: Any
14
16
end
15
17
function show (io:: IO ,db:: SQLiteDB )
16
- if db == null_SQLiteDB
18
+ if db. handle == C_NULL
17
19
print (io," Null sqlite connection" )
18
20
else
19
21
println (io," sqlite connection" )
@@ -47,6 +49,16 @@ function connect(file::String)
47
49
return (sqlitedb = SQLiteDB (file,handle[1 ],null_resultset))
48
50
end
49
51
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
50
62
function internal_query (conn:: SQLiteDB ,q:: String ,finalize:: Bool = true ,stepped:: Bool = true )
51
63
stmt = Array (Ptr{Void},1 )
52
64
if @FAILED sqlite3_prepare_v2 (conn. handle,utf8 (q),stmt,[C_NULL ])
@@ -69,8 +81,7 @@ function internal_query(conn::SQLiteDB,q::String,finalize::Bool=true,stepped::Bo
69
81
end
70
82
function query (q:: String ,conn:: SQLiteDB = sqlitedb)
71
83
conn == null_SQLiteDB && error (" [sqlite]: A valid SQLiteDB was not specified (and no valid default SQLiteDB exists)" )
72
- stmt, r = SQLite. internal_query (conn,q,false )
73
- r == SQLITE_DONE && return DataFrame (" No Rows Returned" )
84
+ stmt, status = SQLite. internal_query (conn,q,false )
74
85
# get resultset metadata: column count, column types, and column names
75
86
ncols = SQLite. sqlite3_column_count (stmt)
76
87
colnames = Array (UTF8String,ncols)
@@ -93,7 +104,7 @@ function query(q::String,conn::SQLiteDB=sqlitedb)
93
104
end
94
105
end
95
106
# retrieve resultset
96
- while true
107
+ while status != SQLITE_DONE
97
108
for i = 1 : ncols
98
109
t = SQLite. sqlite3_column_type (stmt,i- 1 )
99
110
if t == SQLITE3_TEXT
@@ -107,13 +118,13 @@ function query(q::String,conn::SQLiteDB=sqlitedb)
107
118
end
108
119
push! (resultset[i],r)
109
120
end
110
- sqlite3_step (stmt) == SQLITE_DONE && break
121
+ status = sqlite3_step (stmt)
111
122
end
112
123
# this is for columns we couldn't get the type for earlier (NULL in row 1); should be the exception
113
124
if check != ncols
114
125
nrows = length (resultset[1 ])
115
126
for i = 1 : ncols
116
- if isna (resultset[i][1 ])
127
+ if nrows > 0 && isna (resultset[i][1 ])
117
128
d = resultset[i]
118
129
for j = 2 : nrows
119
130
if ! isna (d[j])
@@ -146,9 +157,6 @@ function createtable(input::TableInput,conn::SQLiteDB=sqlitedb;name::String="",d
146
157
return r
147
158
end
148
159
function df2table (df:: DataFrame ,conn:: SQLiteDB ,name:: String )
149
- # get column names and types
150
- ncols = length (df)
151
- colnames = join (df. colindex. names,' ,' )
152
160
# get df name for table name if not provided
153
161
dfname = name
154
162
if dfname == " "
@@ -158,8 +166,22 @@ function df2table(df::DataFrame,conn::SQLiteDB,name::String)
158
166
end
159
167
end
160
168
end
161
- # should we loop through column types to specify in create table statement?
162
- internal_query (conn," create table $dfname ($colnames )" )
169
+ # build column specifications
170
+ ncols = length (df)
171
+ colnames = copy (df. colindex. names)
172
+ for col = 1 : ncols
173
+ t = eltype (df[col])
174
+ if t <: FloatingPoint
175
+ colnames[col] *= " REAL"
176
+ elseif t <: Integer
177
+ colnames[col] *= " INT"
178
+ elseif t <: String
179
+ colnames[col] *= " TEXT"
180
+ end
181
+ end
182
+ colspec = join (colnames, " ," )
183
+ # create table
184
+ internal_query (conn," create table $dfname ($colspec )" )
163
185
internal_query (conn," BEGIN TRANSACTION" )
164
186
# prepare insert table with parameters for column values
165
187
params = chop (repeat (" ?," ,ncols))
0 commit comments