Import csv outside the web2py causes multiple entries in db corresponding to each column.

49 views
Skip to first unread message

Sarbjit

unread,
Nov 4, 2013, 10:07:44 PM11/4/13
to web...@googlegroups.com
I am using import csv option to insert records in the database. When I use the csv file from the web application, then  a single record is inserted in the database with all the records being updated properly as per the headers.

But If I try to use "import_from_csv_file" outside the web2py, it inserts multiple records in the database. This seems to be an bug to me.

Code for updating db outside web2py

libraryPath = r'D:\web2py'
dbPath = r'D:\web2py\applications\testapp\databases'

import sys
sys.path.append(libraryPath)
from gluon import DAL
db = DAL('sqlite://storage.sqlite',folder=dbPath,auto_import=True)
table = db['person']
file = r'D:\file.csv'
table.import_from_csv_file(file)
db.commit()

On using the same file (csv file) using the web2py, it works as expected, code :-

file = request.vars.csvfile.file
table.import_from_csv_file(file)

Can some one please comment on this behavior, what am I missing here?

-Sarbjit

Derek

unread,
Nov 7, 2013, 1:38:57 PM11/7/13
to web...@googlegroups.com
pass it the 'restore=True' to truncate the table before inserting. Otherwise, it doesn't appear that the CSV import would do an 'insert or update'.

Derek

unread,
Nov 7, 2013, 1:42:17 PM11/7/13
to web...@googlegroups.com
it looks like the id_map will tell it which column to use to map IDs, so it may do update or insert as appropriate. How convenient!
Reply all
Reply to author
Forward
0 new messages