问题
I want to load data from csv file into SQlite database. My code is :
Model:
db.define_table('data_table',
Field('Title',requires =IS_NOT_EMPTY()),
Field('Link',requires =IS_NOT_EMPTY()))
db.data_table.import_from_csv_file(open('mycsv'),'rb')
Controller:
def index():
query=((db.data_table.id))
fields = (db.data_table.id, db.data_table.Title, db.data_table.Link)
headers = {'data_table.id': 'ID',
'db.data_table.Title': 'Title',
'db.data_table.Link': 'Link',
}
default_sort_order=[db.data_table.id]
form = SQLFORM.grid(query=query, fields=fields, headers=headers,orderby=default_sort_order,
create=False, deletable=False, editable=False, maxtextlength=64, paginate=10)
return dict(form=form)
Here when I load this form in index.html I am getting repeated rows in Gridview.
e.g. If I have 5 rows in csv file then very first time it will show 5 records but when I refresh the index.html then each time it adds on those 5 records.
On 1st refresh it gives me 45 records. On 2nd refresh it gives 100 records. In this way it goes on increasing.
My doubt is where should I write this line
db.data_table.import_from_csv_file(open('mycsv'),'rb')
In Model or Controller or View
Please help me in this.
Thank you in advance.
来源:https://stackoverflow.com/questions/33118947/repetition-of-data-for-import-from-csv-file-in-web2py-for-sqlite-database-tabl