473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Newbie problem inserting into MySQL

len
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
Aug 18 '08 #1
3 2607
On Aug 19, 1:54 am, len <lsumn...@gmail.comwrote:
| lottryid | int(11) | NO | PRI | NULL |
auto_increment |
tupcnt += 1
rectuple = tupcnt, thekey, a, b, c, d, e, mysum, 0
listofrec.append(rectuple)
cursor.executemany('''insert into
littlelottery
values (?,?,?,?,?,?,?,?,?)''', listofrec)

i get the following error on insert;
raise errorclass, errorvalue
TypeError: not all arguments converted during string formatting
{caveat: I'm not a MySQL user]

Could this be caused by trying to insert a value for an auto_increment
column using the form of insert where you don't specify column names?
IOW, your tuple has one more item than it is expecting ...

In any case, why have an auto_increment column but then increment it
yourself?

HTH,
John

Aug 18 '08 #2
len wrote:
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.
import MySQLdb
cursor.executemany('''insert into
littlelottery
values (?,?,?,?,?,?,?,?,?)''', listofrec)
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?
>>import MySQLdb
MySQLdb.paramstyle
'format'

So it looks like you need to replace the '?' in your SQL statement
with '%s'.

Peter
Aug 18 '08 #3
len wrote:
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.
1. As someone else mentioned, the placeholder for MySQL data
is "%s", not "?".
2. After inserting, you must call "db.commit()", or, when the
program exits, all the insertions will be backed out.
(Assuming you're using a table type that supports
transactions, like InnoDB. But commit anyway.)
3. If you're inserting a huge number of records, look into
LOAD DATA. It's much faster.
4. Your code will make 10000 entries, then exit. Is that
what you want?
5. Creating a database of computed values is a useful exercise,
but not all that useful.

John Nagle
Aug 19 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Raqueeb Hassan | last post by:
Hello There! Have you guys tried inserting variables in mySQL tables? Do I have to use 'quote' as we had been doing to insert strings? mysql> INSERT INTO occurrence (word_id,page_id) VALUES...
3
pafranklin
by: pafranklin | last post by:
Hi there, Apologies in advance for the "simple question", but I am relatively new to MySQL. I have a database driven website and have within the database a number of fields in a certain...
1
Ajm113
by: Ajm113 | last post by:
Ok, when I was new to this I had this problem and I bet a lot of other people did when they where new to PHP and Mysql. So this mite be your question; "Ok, no errors or warnings in mysql and php so...
0
by: gp | last post by:
I am and have been using PDO for about a year now...and have finally gotten around to solving the "DB NULL value" issues I ran into early on... I am looking for suggestions and techniques to...
1
by: djmeltdown | last post by:
I'm having trouble getting a foreach() loop to insert a record into a MySQL Database. Which I've never had any trouble before, it just seems quirky. I've tried the mysql_query statement without a...
0
by: Alan Silver | last post by:
Hello, I'm a newbie at PHP and MySql, although I have wads of experience of ASP, ASP.NET, SQL Server, etc. I have just installed PHP 5.2.3 on a server (Windows Server 2003), as well as MySql...
0
by: Edwin.Madari | last post by:
-----Original Message----- 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...
6
by: Bunty | last post by:
I want to insert values in the database.If i insert values one by one then it works till 4 or 5 fields then after it gives error.In my database there are more than 20 field.Pls help me.
5
by: atlanteavila | last post by:
Hello all, I have a problem with inserting text with quotes, or html code in to a MYSql Database. I've been trying to create my own content management system, and unfortunately I've come accross a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.