|
Eric Walstad
eric at ericwalstad.com
Sat Sep 17 16:35:49 EDT 2005
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'.
|