mercredi 14 octobre 2015

Repetition of data for import_from_csv_file() in web2py for sqlite database table

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.

Aucun commentaire:

Enregistrer un commentaire