[mod_python] MySQLdb error - PLEASE SAVE ME!

Jim Gallacher jg.lists at sympatico.ca
Sat Sep 17 17:42:25 EDT 2005


Ed Hotchkiss wrote:
> Hmm ... Actually, now that I use it with a much longer file (the past 
> file was like 6 links), this file is several hundred. I get this error now:
>  
>  >>>
> Traceback (most recent call last):
>   File "G:\Python\myCode\Links Database\addfromtext2.py", line 30, in ?
>     cursor.execute ("""
> IndexError: list index out of range
>  >>>
>  
> I'm guessing that it has something to do with the primary index, "ID"? I 
> tried auto increment, but no go with that too, same error ...

More likely one of the lines in sites.txt has fewer than 3 fields. Try 
running the following:

inp = open ("sites1.txt","r")
for line in inp.readlines():
     links = map(str, line.split(","))
     print links[0], links[1], links[2]


It will like generate the same error. You may want to consider using the 
python csv module rather than split.

Jim

>  
> On 9/17/05, *Ed Hotchkiss* <edhotchkiss at gmail.com 
> <mailto:edhotchkiss at gmail.com>> wrote:
> 
>      
>     Thanks alot! I guess I don't even need arr (why did I have it there,
>     no idea -)
>     The only thing I was wondering (the code is awesome, but I love to
>     learn :P )
>      
>     Is UID a placeholder the ID? How do I have it automatically
>     increment .. and is that a better idea, to autoincrement?
>      
>     Thanks again. It feels good to migrate completely to linux now, and
>     transfer all of my access files to csv, then into mysql.
>      
>      
>     uid = 0
>     inp = open ("sites1.txt","r")
>     for line in inp.readlines():
>        uid += 1
>        links = map(str, line.split(","))
>        cursor.execute ("""
>     INSERT INTO links (ID, Name, URL, Category)
>        VALUES (%d, "%s", "%s", "%s")""" % \
>            (uid, links[0], links[1], links[2])
>            )
> 
>     ----
>     Also, what is the \ for after the % sign to the right of VALUES? thanks.
>     -edward
>      
>      
>      
>      
>      
>      
>     On 9/17/05, *Eric Walstad* <eric at ericwalstad.com
>     <mailto:eric at ericwalstad.com>> wrote:
> 
>         On Saturday 17 September 2005 12:40 pm, Ed Hotchkiss wrote:
>         [snip]
>>  stmt = """CREATE TABLE links (
>>     ID INT NOT NULL,
>>     Name TEXT,
>>     URL LONGTEXT,
>>     Category LONGTEXT,
>>     primary key (ID)
>>  )"""
>>  cursor.execute(stmt)
>>
>>
>>  arr=[]
>>  inp = open ("sites1.txt","r")
>>  #read line into array
> 
>>  for line in inp.readlines():
>>     links = map(str, line.split(","))
>>     arr.append(links)
>>     cursor.execute ("""
>>  INSERT INTO links (Name, URL, category)
>>     VALUES (%s, %s, %s)""" % tuple(links[0:3])
>>         )
>>  cursor.close()
>>  conn.close()
> 
>         Perhaps this is a bit naive but would this do it for you (you didn't
>         say what error(s) you are getting)?
> 
>         uid = 0
>         for line in inp.readlines():
>            uid += 1
>            links = map(str, line.split(","))
>            arr.append(links)
>            cursor.execute ("""
>         INSERT INTO links (ID, Name, URL, Category)
>            VALUES (%d, "%s", "%s", "%s")""" % \
>                (uid, links[0], links[1], links[2])
>                )
> 
>         The differences are:
>         - Your table def doesn't specify an auto-incrementing id and won't
>         allow null values so we need to insert a number.
>         - Quotes around the string data.  I this might not be needed.  I'm
>         not sure if MySQLdb does it for you.
>         - I think MySQL is sensitive to case, so I changed 'category' to
>         'Category'.
>         _______________________________________________
>         Mod_python mailing list
>         Mod_python at modpython.org <mailto:Mod_python at modpython.org>
>         http://mailman.modpython.org/mailman/listinfo/mod_python
>         <http://mailman.modpython.org/mailman/listinfo/mod_python>
> 
> 
> 
> 
>     -- 
>     edward hotchkiss 
> 
> 
> 
> 
> -- 
> edward hotchkiss
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python



More information about the Mod_python mailing list