By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,199 Members | 1,458 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,199 IT Pros & Developers. It's quick & easy.

Re: Newbie problem inserting into MySQL

P: n/a
-----Original Message-----
From: py************************************************ **@python.org
[mailto:python-list-bounces+edwin.madari=verizonwireless.com@p
ython.org]
On Behalf Of len
Sent: Monday, August 18, 2008 11:55 AM
To: py*********@python.org
Subject: Newbie problem inserting into MySQL
Hi All

I have started a little pet project to learn python and MySQL. The
project involves figuring out all the combinations for a 5 number
lottery and storing the data in a MySQL file.

The file looks like this;
+----------+---------------------+------+-----+---------
+----------------+
| Field | Type | Null | Key | Default |
Extra |
+----------+---------------------+------+-----+---------
+----------------+
| lottryid | int(11) | NO | PRI | NULL |
auto_increment |
| lottryno | char(10) | YES | | NULL
| |
| no1 | tinyint(3) unsigned | NO | | NULL
| |
| no2 | tinyint(3) unsigned | NO | | NULL
| |
| no3 | tinyint(3) unsigned | NO | | NULL
| |
| no4 | tinyint(3) unsigned | NO | | NULL
| |
| no5 | tinyint(3) unsigned | NO | | NULL
| |
| nosum | tinyint(3) unsigned | NO | | NULL
| |
| nohits | int(10) unsigned | YES | | NULL
| |
+----------+---------------------+------+-----+---------
+----------------+

The code looks as follows;
#!/usr/lib/env python

import MySQLdb
import datetime

db = MySQLdb.Connection(host="localhost", user="lenyel",
passwd="lsumnler", \
db="lottery")

cursor = db.cursor()

cursor.execute('delete from littlelottery')

listofrec = []

tupcnt = 0
print "first tuple created"
for a in xrange(1,36):
for b in xrange(2,37):
for c in xrange(3,38):
for d in xrange(4,39):
for e in xrange(5,40):
tupcnt += 1
thekey = ('%02i%02i%02i%02i%02i' % (a,b,c,d,e))
mysum = a + b + c + d + e
rectuple = tupcnt, thekey, a, b, c, d, e, mysum, 0
listofrec.append(rectuple)
if tupcnt % 10000 == 0:
print "beginnign of mysql write"
print datetime.datetime.now().time()
cursor.executemany('''insert into
littlelottery
values (?,?,?,?,?,?,?,?,?)''', listofrec)
db.close()
print "end of mysql write"
print datetime.datetime.now().time()
os._exit()

print "insert into mysql completed"

i get the following error on insert;
raise errorclass, errorvalue
TypeError: not all arguments converted during string formatting
Script terminated.

Do I have to covert all of the fields in the tuple records to string
or what?

Len Sumnler
--
http://mail.python.org/mailman/listinfo/python-list
statement prepared first and executed many times with exectemany - db API http://www.python.org/dev/peps/pep-0249/
inline statemets can be exeucuted only.

hope that helps
Edwin

The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure. If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof. Thank you.
Aug 18 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.