Can't write file using MySQL data

I get this error:

TyperError: sequence item 2: expected string, long found

Here is my code:

import MySQLdb
connection = MySQLdb.connect (host = "localhost", user = "root", passwd = "*****", db = "test")
cursor = connection.cursor ()
cursor.execute ("SELECT * FROM names")
data = cursor.fetchall ()
no = []
no += ["<users>\n"]
for row in data :
        no += ["        <user id='", row[0], "' name='", row[1], "' blend='", row[2], "'/>\n"]
        no += ["                <nick color='0x", row[3], "' font='", row[4], "'/>\n"]
        no += ["                <glow color='0x", row[5], "' alpha='", row[6], "' blurX='", row[7], "' blurY='", row[8], "' strength='", row[9], "' quality='", row[10], "'/>\n"]
        no += ["        </user>\n", row[11]]
cursor.close ()
connection.close ()
no += ["</users>"]
s = ''.join(no)
file = open('test.xml','w')
file.write(s)

Answers


Instead of concatenating strings into sub lists the way you're doing it, you can do something like the following

# Don't do this
no += ["        <user id='", row[0], "' name='", row[1], "' blend='", row[2], "'/>\n"]

# Do this instead
no += ["        <user id='%s' name='%s' blend='%s'/>\n" % (row[0], row[1], row[2])]

You have to convert your retrieved data to string first using str() so anywhere where it says row[INDEX] you have to say: str(row[INDEX]). str(row[3]) for example.


Need Your Help

Trying to deploy GWT project in Tomcat ends with offending class error

java gwt tomcat deployment webserver

I'm trying to deploy my GWT project in Tomcat but i'm always getting this error:

eclipse dies every few minutes with latest jdk saying 'out of space in CodeCache for adapters'

eclipse java

Since I installed latest jdk (1.6.0_25) I am having a very annoying issue that constantly makes my eclipse restart. This happens while doing any action, not a big clean project or anything.