[Tutor] SQLite database not update correctly

Emmanuel Ruellan emmanuel.ruellan at laposte.net
Sun Nov 8 20:10:49 CET 2009


Hi tutors,

I've got a functions that should update an sqlite database, among other
things.  However the database doesn't get updated. When used in isolation,
the update statement works fine. What am I doing wrong?

Below is the function. The whole script can be found at
http://pastebin.com/m53978ffa

def update_DB_chi_square():
    global stopwords
    conn = sqlite3.connect(os.path.join('.', 'collocations_base.sqlite'))
    c = conn.cursor()
    c.execute('select * from word_couples')
    couples_and_nbrs = c.fetchall()
    for couple_and_nbr in couples_and_nbrs:
        w1, w2, o11, chi_square = couple_and_nbr
        if o11 < 5 or w1 in stopwords or w2 in stopwords:
            continue
        o12 = c.execute("""select sum(nbr_occ)
                        from word_couples
                        where w1 = ? and w2 != ?
                        """, (w1, w2)).fetchall()[0][0] or 0
        o21 = c.execute("""select sum(nbr_occ)
                        from word_couples
                        where w1 != ? and w2 = ?
                        """, (w1, w2)).fetchall()[0][0] or 0
        o22 = c.execute("""select sum(nbr_occ)
                        from word_couples
                        where w1 != ? and w2 != ?
                        """, (w1, w2)).fetchall()[0][0] or 0
        n = c.execute("select sum(nbr_occ) from
word_couples").fetchall()[0][0]
        chi_square = float((n * (o11 * o22 - o12 * o21) ** 2))/ ((o11 + o12)
* (o11 + o21) * (o12 + o22) * (o21 + o22))
        c.execute('update word_couples set chi_square = ? where w1 = ? and
w2 = ?', (chi_square, w1, w2))
        print 'check:', '\t'.join((w1, w2, str(chi_square)))
    c.close()
    conn.close()

Best regards,
Emmanuel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091108/f25b9dcf/attachment.htm>


More information about the Tutor mailing list