Skip to content

Commit daa15a7

Browse files
committed
Merge pull request #13 from lindahua/dh/coltype
Adding column types for df2table
2 parents e723465 + 1183251 commit daa15a7

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/SQLite.jl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,6 @@ function createtable(input::TableInput,conn::SQLiteDB=sqlitedb;name::String="",d
147147
return r
148148
end
149149
function df2table(df::DataFrame,conn::SQLiteDB,name::String)
150-
#get column names and types
151-
ncols = length(df)
152-
colnames = join(df.colindex.names,',')
153150
#get df name for table name if not provided
154151
dfname = name
155152
if dfname == ""
@@ -159,8 +156,22 @@ function df2table(df::DataFrame,conn::SQLiteDB,name::String)
159156
end
160157
end
161158
end
162-
#should we loop through column types to specify in create table statement?
163-
internal_query(conn,"create table $dfname ($colnames)")
159+
# build column specifications
160+
ncols = length(df)
161+
colnames = copy(df.colindex.names)
162+
for col = 1 : ncols
163+
t = eltype(df[col])
164+
if t <: FloatingPoint
165+
colnames[col] *= " REAL"
166+
elseif t <: Integer
167+
colnames[col] *= " INT"
168+
elseif t <: String
169+
colnames[col] *= " TEXT"
170+
end
171+
end
172+
colspec = join(colnames, ",")
173+
# create table
174+
internal_query(conn,"create table $dfname ($colspec)")
164175
internal_query(conn,"BEGIN TRANSACTION")
165176
#prepare insert table with parameters for column values
166177
params = chop(repeat("?,",ncols))

0 commit comments

Comments
 (0)