problems with mysql db

Petr Messner petr.messner at gmail.com
Mon Jun 29 15:32:40 CEST 2009


Hi,

use %s instead of %d in SQL statements, because (AFAIK) conversions
(including SQL escaping) from Python values to SQL values are done
before the % operator is called - that value is not a number by that
point.

I hope you understood it, sorry for my English :-) You can also check
MySQLdb module source, it's pretty clear.

PM

2009/6/29 golu <bhardwajjayesh7 at gmail.com>:
> here i have posted my code...plz tell why am i getting the error "int
> argument required" on the hash marked line(see below) although i am
> giving an int value
> #the code
> import os
> import string
> import MySQLdb
> import stopcheck
> conn = MySQLdb.connect(host='localhost',user='root',db='urdb')
>
> def file_extractor(dir_name):
>    url_count = 0
>
>    for file in os.listdir(dir_name):
>      if(file[-4:] == '.txt'):
>        file_path = os.path.join(dir_name,file)
>        curse = conn.cursor()
>        url_count += 1
>        curse.execute("INSERT INTO URL_TABLE VALUES(%d,%s)",
> (url_count,file_path)) #error
>        word_extractor(url_count,file_path)
> def word_extractor(url_count,file):
>    fhandle =  open(file)
>    line = fhandle.readline()
>    k=stopcheck.checker()
>    k.create()
>
>    while line:
>       words = line.split()
>       cursor = conn.cursor()
>       for word1 in words:
>           if word1 not in string.punctuation:
>                if (k.check(word1) is 0) and (word1[0:4] != 'http') :
>                   word_count+=1
>                   try:
>                    cursor.execute("INSERT INTO word_table(id,word)
> VALUES(%d,%s)" , (word_count,word1))
>                    cursor.execute("INSERT INTO wordmatch
> (word_id,url_id) values(%d,%d)",(word_count,url_count))
>                   except MySQLdb.Error, e:
>                         print "Error %d: %s" % (e.args[0], e.args[1])
>       line=fhandle.readline()
>
> if __name__ == '__main__':
>    #url_count=0
>    #word_count=0
>
>    dir = os.path.join('D://','acm')
>    file_extractor(dir)
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list