473,322 Members | 1,806 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,322 software developers and data experts.

MySQLDB - parameterised SQL - "TypeError: not all arguments converted during string formatting"

Hi - I have written some python to insert a row into a table using
MySQLDB. I have never before written SQL/Python using embedded
parameters in the SQL and I'm having some difficulties. Could someone
point me in the right direction please ?

The python looks like this :

import MySQLdb
import MySQLdb.cursors
conn = MySQLdb.Connect(host='localhost', user='abc,passwd='def',
db='ghi',compress=1)

cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor )
sqlQuery = "INSERT INTO EVA_COMPANY
(COM_ADDRESS,COM_AUTOID,COM_COMPANY_ADDRESS,COM_ST RUCT_OWNER_CODE)
VALUES (?,?,?,?) "
print sqlQuery
sql = cursor.execute(sqlQuery,("test","NULL","Tester1017 ",5))
cursor.close()
conn.commit()

.... and the table looks like this ...

CREATE TABLE `eva_company` (
`COM_AUTOID` int(9) unsigned NOT NULL auto_increment,
`COM_COMPANY_NAME` varchar(128) NOT NULL,
`COM_TRADING_NAME` varchar(128) default NULL,
`COM_ADDRESS` varchar(256) NOT NULL,
`COM_POSTAL_ADDRESS` varchar(256) default NULL,
`COM_CONTACT_NAME` varchar(56) default NULL,
`COM_CONTACT_POSITION` varchar(56) default NULL,
`COM_CONTACT_TELEPHONE` varchar(16) default NULL,
`COM_CONTACT_FAX` varchar(16) default NULL,
`COM_CONTACT_EMAIL` varchar(256) default NULL,
`COM_STRUCT_OWNER_CODE` int(9) unsigned default NULL,
`COM_INDUSTRY_CODE` varchar(16) default NULL,
`COM_INDUSTRY_DESC` varchar(56) default NULL,
`COM_NUMBER_OF_SITES` int(9) default NULL,
`COM_NATURE_OF_RELATIONSHIP` varchar(128) default NULL,
PRIMARY KEY (`COM_AUTOID`),
KEY `IDX_COM1` (`COM_STRUCT_OWNER_CODE`),
KEY `IDX_COM0` (`COM_COMPANY_NAME`),
CONSTRAINT `FK_COS_COM0` FOREIGN KEY (`COM_STRUCT_OWNER_CODE`)
REFERENCES `eva_code_comp_struct_ownership` (`COS_AUTOID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

.... but when I try to execute the python I get an error ...

Traceback (most recent call last):
File "sheadbtest1.py", line 8, in ?
sql = cursor.execute(sqlQuery,("test","NULL","Tester1017 ",5))
File
"C:\bin\installed\Python2.4.1\Lib\site-packages\MySQLdb\cursors.py",
line 132, in execute
self.errorhandler(self, TypeError, m)
File
"C:\bin\installed\Python2.4.1\Lib\site-packages\MySQLdb\connections.py",
line 33, in defaulterrorhandler
raise errorclass, errorvalue
TypeError: not all arguments converted during string formatting

.... as I say if anyone could provide me with some tips I'd appreciate
it.

thanks

richard shea.

Feb 20 '06 #1
1 7395
Hi Dennis - Thanks for your help with this ....

Dennis Lee Bieber wrote:
On 19 Feb 2006 16:26:15 -0800, sh*********@gmail.com declaimed the
following in comp.lang.python:
Hi - I have written some python to insert a row into a table using
MySQLDB. I have never before written SQL/Python using embedded
parameters in the SQL and I'm having some difficulties. Could someone
point me in the right direction please ?
sqlQuery = "INSERT INTO EVA_COMPANY
(COM_ADDRESS,COM_AUTOID,COM_COMPANY_ADDRESS,COM_ST RUCT_OWNER_CODE)
VALUES (?,?,?,?) "
One: I believe MySQLdb uses %s placeholders, not ?
import MySQLdb
MySQLdb.paramstyle

'format'

From the DB-API PEP

paramstyle

String constant stating the type of parameter marker
formatting expected by the interface. Possible values are
[2]:

'qmark' Question mark style,
e.g. '...WHERE name=?'
'numeric' Numeric, positional style,
e.g. '...WHERE name=:1'
'named' Named style,
e.g. '...WHERE name=:name'
'format' ANSI C printf format codes,
e.g. '...WHERE name=%s'
'pyformat' Python extended format codes,
e.g. '...WHERE name=%(name)s'


Great stuff. I had seen some example code which used question marks and
rather too slavishly used it as a model. As you say MySQLDb does indeed
use 'format' and so %s are the way to go ...
print sqlQuery
sql = cursor.execute(sqlQuery,("test","NULL","Tester1017 ",5))
Two: I think it is smart enough to translate a Python None to a
MySQL NULL -- which is different from a four-character string containing
NULL (or even a zero-character string "").


.... just to confirm that yes using a Python None works fine if you wish
to put a Null into a column ...

CREATE TABLE `eva_company` (
`COM_AUTOID` int(9) unsigned NOT NULL auto_increment,
As an auto-increment field, the recommendation would be to not even
mention the field in the INSERT SQL -- that also takes care of the
problem of specifying NULL to a non-null field!


.... yes it didn't start off that way but I was busy trying everthing I
could think of to try to make it happier.

thanks for your help it's all working now.

regards

richard.

--
> ================================================== ============ <
> wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
> wu******@dm.net | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <


Feb 20 '06 #2

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

Similar topics

1
by: J. Muenchbourg | last post by:
I'm getting an "Arguments are of the wrong type, out of acceptable range, in conflicts with each other " error , pointing to the sql statement in this block: Dim rstime Set rstime =...
0
by: ttomic | last post by:
I have asp.net application written in jscript. On POST request, during Page_Load, I receive string which I evaluate using jscript eval method. String looks like: my_fun("arg") where my_fun is...
4
by: dbuchanan | last post by:
Hello, I have an audit table into which I insert information about the use of the application. This works sometimes and other times fails. I cannot find any reason for it failing. It is always...
0
by: wan | last post by:
Hi, Any idea of why a proper running Application in VS 2003 , could not run after converted to VS 2005 with the error "Could not load file or assembly , . Strong name validation failed".... The...
42
by: Holger | last post by:
Hi guys Tried searching for a solution to this, but the error message is so generic, that I could not get any meaningfull results. Anyways - errormessage:...
7
by: Girish Sahani | last post by:
Hi, Please check out the following loop,here indexList1 and indexList2 are a list of numbers. for index1 in indexList1: for index2 in indexList2: if ti1 == ti2 and not index1 !=...
1
by: OtisUsenet | last post by:
Hi, I have a bookmarklet that works perfectly in Firefox, IE, Konqueror, and Opera, but in Safari 2.0.3 (417.9.2) it doesn't work. I enabled debugging and I can see "TypeError - Undefined...
17
by: Petyr David | last post by:
Just looking for the simplest. right now my perl script returns an error messge to the user if the date string is invalid. would like to do this before accessing the server. TX
7
by: lukertin | last post by:
I have a 2-D array stored as an object, when i go to call up the array using my $testvar = @ {$self->Peptides}; it gives me the error Can't use string ("0") as an ARRAY ref while "strict...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.