UPDATE
After passing execute (), the list of lines is as suggested by Nathan, below, the code executes further, but still gets stuck in the execution function. Error message:
query = query % db.literal(args) TypeError: not all arguments converted during string formatting
So it still does not work. Does anyone know why there is a type error?
END UPDATE
I have a large mailing list in .xls format. I am using python with xlrd to get the name and email address from the xls file in two lists. Now I want to put each name and email address in the mysql database. I am using MySQLdb for this part. Obviously, I do not want to do an insert statement for each element of the list. Here is what I still have.
from xlrd import open_workbook, cellname import MySQLdb dbname = 'h4h' host = 'localhost' pwd = ' P@ssw0rd ' user = 'root' book = open_workbook('h4hlist.xls') sheet = book.sheet_by_index(0) mailing_list = {} name_list = [] email_list = [] for row in range(sheet.nrows): """name is in the 0th col. email is the 4th col.""" name = sheet.cell(row, 0).value email = sheet.cell(row, 4).value if name and email: mailing_list[name] = email for n, e in sorted(mailing_list.iteritems()): name_list.append(n) email_list.append(e) db = MySQLdb.connect(host=host, user=user, db=dbname, passwd=pwd) cursor = db.cursor() cursor.execute("""INSERT INTO mailing_list (name,email) VALUES (%s,%s)""", (name_list, email_list))
Problem while executing cursor. This is the error: _mysql_exceptions.OperationalError: (1241, 'Operand should contain 1 column(s)') I tried to put my query in var first, but then it just closed the message about passing the tuple to execute ().
What am I doing wrong? Is it possible?
The list is huge, and I definitely can't let the insert insert into the loop. I looked at using LOAD DATA INFILE, but I really don't understand how to format a file or query, and my eyes are cleared when I have to read MySQL documents. I know that I could probably use some xls online applications for mysql converter, but this is also an exercise for me. Is there a better way ?