[mod_python] MySQLdb error - PLEASE SAVE ME!

Ed Hotchkiss edhotchkiss at gmail.com
Sat Sep 17 16:58:01 EDT 2005


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> 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
> http://mailman.modpython.org/mailman/listinfo/mod_python
> 



-- 
edward hotchkiss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20050917/97d08ced/attachment.html


More information about the Mod_python mailing list