Problem with inserting values read from a text file into mysql database

29 views
Skip to first unread message

Oguz Erkman

unread,
Jun 21, 2016, 6:05:40 PM6/21/16
to web2py-users
Hello,

I wrote a module to use the Part of Speech Tagger Hunpos in web2py.

The module takes a text file tokenized as one token (word) per line as input and perfectly creates and writes into another text file with one token and its POS (Part of Speech) Tag separated with a tab character in one line.

Here is a part of my output file to give an idea how it looks:

Literature NN
gives VBZ
a DT
form NN
to TO
life NN
, ,
helps VBZ
us PRP
to TO
know VB
who WP
we PRP
are VBP
, ,
how WRB
we PRP
feel VBP
and CC
what WP
is VBZ
the DT
point NN
of IN
the DT
whole JJ
unutterably JJ
rummy NN
business NN
. .
 

When I try to open and read from the output file and write the items inside into the mysql database it simply does not insert the items.
Here is the part of my code that I used for database insertion:

...

            # Write to database

            with io.open(outfile, 'r') as ffile:
                id_list = []
                for line in ffile:
                    if not line.isspace():
                        pt = line.split('\t')
                        _id = db.en_pos_analysis.insert(token=pt[0],postag=pt[1])
                        id_list.append(_id)

    print 'POS Tagging completed'

    return

I used the same syntax in other modules of the program before and there was no problem inserting in the database.

Thanks in advance.

Anthony

unread,
Jun 21, 2016, 8:45:44 PM6/21/16
to web2py-users
Is this in an application during an http request, or in a script run on it's own? If the latter, you must call db.commit() to commit the inserts.

Also, do you get any errors?

Anthony

Oguz Erkman

unread,
Jun 21, 2016, 10:20:18 PM6/21/16
to web2py-users
I found the solution and I knew it was a silly mistake.
I forgot to add db = current.db at the beginning of the function.
Thanks anyway, maybe this answer will help others, there was no other mistake in the code, it works like a charm.

22 Haziran 2016 Çarşamba 01:05:40 UTC+3 tarihinde Oguz Erkman yazdı:
Reply all
Reply to author
Forward
0 new messages