Skip to content

Commit b3f57a2

Browse files
committed
Merge branch 'master' of https://github.com/karbarcca/SQLite.jl
2 parents 05e98c1 + 068a64d commit b3f57a2

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DataFrames

src/SQLite.jl

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ module SQLite
22

33
using DataFrames
44

5-
export sqlitedb, readdlmsql, query, connect, createtable, droptable
5+
export sqlitedb, readdlmsql, query, createtable, droptable
6+
7+
import Base: show, close
68

79
include("SQLite_consts.jl")
810
include("SQLite_api.jl")
@@ -13,7 +15,7 @@ type SQLiteDB
1315
resultset::Any
1416
end
1517
function show(io::IO,db::SQLiteDB)
16-
if db == null_SQLiteDB
18+
if db.handle == C_NULL
1719
print(io,"Null sqlite connection")
1820
else
1921
println(io,"sqlite connection")
@@ -47,6 +49,16 @@ function connect(file::String)
4749
return (sqlitedb = SQLiteDB(file,handle[1],null_resultset))
4850
end
4951
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
5062
function internal_query(conn::SQLiteDB,q::String,finalize::Bool=true,stepped::Bool=true)
5163
stmt = Array(Ptr{Void},1)
5264
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
6981
end
7082
function query(q::String,conn::SQLiteDB=sqlitedb)
7183
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)
7485
#get resultset metadata: column count, column types, and column names
7586
ncols = SQLite.sqlite3_column_count(stmt)
7687
colnames = Array(UTF8String,ncols)
@@ -93,7 +104,7 @@ function query(q::String,conn::SQLiteDB=sqlitedb)
93104
end
94105
end
95106
#retrieve resultset
96-
while true
107+
while status != SQLITE_DONE
97108
for i = 1:ncols
98109
t = SQLite.sqlite3_column_type(stmt,i-1)
99110
if t == SQLITE3_TEXT
@@ -107,13 +118,13 @@ function query(q::String,conn::SQLiteDB=sqlitedb)
107118
end
108119
push!(resultset[i],r)
109120
end
110-
sqlite3_step(stmt) == SQLITE_DONE && break
121+
status = sqlite3_step(stmt)
111122
end
112123
#this is for columns we couldn't get the type for earlier (NULL in row 1); should be the exception
113124
if check != ncols
114125
nrows = length(resultset[1])
115126
for i = 1:ncols
116-
if isna(resultset[i][1])
127+
if nrows > 0 && isna(resultset[i][1])
117128
d = resultset[i]
118129
for j = 2:nrows
119130
if !isna(d[j])
@@ -146,9 +157,6 @@ function createtable(input::TableInput,conn::SQLiteDB=sqlitedb;name::String="",d
146157
return r
147158
end
148159
function df2table(df::DataFrame,conn::SQLiteDB,name::String)
149-
#get column names and types
150-
ncols = length(df)
151-
colnames = join(df.colindex.names,',')
152160
#get df name for table name if not provided
153161
dfname = name
154162
if dfname == ""
@@ -158,8 +166,22 @@ function df2table(df::DataFrame,conn::SQLiteDB,name::String)
158166
end
159167
end
160168
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)")
163185
internal_query(conn,"BEGIN TRANSACTION")
164186
#prepare insert table with parameters for column values
165187
params = chop(repeat("?,",ncols))

0 commit comments

Comments
 (0)