473,748 Members | 4,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MySQL Insert

Hi,

Its one of those days. I cannot solve this. Any help would be greatly
appreciated!
When I execute this:

class Db(object):
def insertAccount(s elf, date, accountNumber, description,
openingBalance) :
dec = decimal.Decimal (openingBalance )
db = MySQLdb.connect (host="localhos t", user="dumb",
passwd="dumber" , db="rdc")
cursor = db.cursor()
cursor.execute( "INSERT INTO es_accounts (dateCreated,
accountNumber, description, openingBalance) VALUES (%s, %s, %s, %d)",
(date, accountNumber, description, dec))

I get this error:
Traceback (most recent call last):
File "main.py", line 59, in <module>
main()
File "main.py", line 40, in main
dbObj.insertAcc ount(dateTo, item[0], item[1], item[8])
File "C:\projects\wo rkspace\INYR_ES _0.1\src\db.py" , line 19, in
insertAccount
cursor.execute( "INSERT INTO es_accounts (dateCreated,
accountNumber, description, openingBalance) VALUES (%s, %s, %s
, %d)", (date, accountNumber, description, dec))
File "c:\python25\li b\site-packages\MySQLd b\cursors.py", line 151,
in execute
query = query % db.literal(args )
TypeError: int argument required

My table is defined as:
CREATE TABLE es_accounts (
id int(6) not null auto_increment,
dateCreated date DEFAULT '0000-00-00',
accountNumber int(6) not null,
description varchar(255) not null,
openingBalance decimal(15,8) NOT NULL DEFAULT 0.00000000,
primary key (id)
);

TIA
Jul 15 '08 #1
5 3571
maestroQC wrote:
cursor.execute( "INSERT INTO es_accounts (dateCreated,
accountNumber, description, openingBalance) VALUES (%s, %s, %s
, %d)", (date, accountNumber, description, dec))
File "c:\python25\li b\site-packages\MySQLd b\cursors.py", line 151,
in execute
query = query % db.literal(args )
TypeError: int argument required
The placeholder for query parameters in MySQLdb is always %s, regardless
of the data type the actual value might have.

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net
Jul 15 '08 #2
maestroQC wrote:
Hi,

Its one of those days. I cannot solve this. Any help would be greatly
appreciated!
When I execute this:

class Db(object):
def insertAccount(s elf, date, accountNumber, description,
openingBalance) :
dec = decimal.Decimal (openingBalance )
db = MySQLdb.connect (host="localhos t", user="dumb",
passwd="dumber" , db="rdc")
cursor = db.cursor()
cursor.execute( "INSERT INTO es_accounts (dateCreated,
accountNumber, description, openingBalance) VALUES (%s, %s, %s, %d)",
(date, accountNumber, description, dec))

I get this error:
Traceback (most recent call last):
File "main.py", line 59, in <module>
main()
File "main.py", line 40, in main
dbObj.insertAcc ount(dateTo, item[0], item[1], item[8])
File "C:\projects\wo rkspace\INYR_ES _0.1\src\db.py" , line 19, in
insertAccount
cursor.execute( "INSERT INTO es_accounts (dateCreated,
accountNumber, description, openingBalance) VALUES (%s, %s, %s
, %d)", (date, accountNumber, description, dec))
File "c:\python25\li b\site-packages\MySQLd b\cursors.py", line 151,
in execute
query = query % db.literal(args )
TypeError: int argument required

My table is defined as:
CREATE TABLE es_accounts (
id int(6) not null auto_increment,
dateCreated date DEFAULT '0000-00-00',
accountNumber int(6) not null,
description varchar(255) not null,
openingBalance decimal(15,8) NOT NULL DEFAULT 0.00000000,
primary key (id)
);

TIA
How embarassing. My previous post is not correct in it's explanation,
unless there is some way to tell Decimal not to allow int coercion...

--from decimal import Decimal
--n10_25 = Decimal('10.25' )
--n10_25
Decimal("10.25" )
--"%d" % n10_25
'10'

About the only thing it had even partially right is not using %d, as it
will truncate the fractional part of your opening balance. As far as
why you are getting that error, I now have no idea, and I apologize for
any confusion created by my error.

~Ethan
Jul 15 '08 #3
maestroQC wrote:
Hi,

Its one of those days. I cannot solve this. Any help would be greatly
appreciated!
When I execute this:

class Db(object):
def insertAccount(s elf, date, accountNumber, description,
openingBalance) :
dec = decimal.Decimal (openingBalance )
db = MySQLdb.connect (host="localhos t", user="dumb",
passwd="dumber" , db="rdc")
cursor = db.cursor()
cursor.execute( "INSERT INTO es_accounts (dateCreated,
accountNumber, description, openingBalance) VALUES (%s, %s, %s, %d)",
(date, accountNumber, description, dec))

I get this error:
Traceback (most recent call last):
File "main.py", line 59, in <module>
main()
File "main.py", line 40, in main
dbObj.insertAcc ount(dateTo, item[0], item[1], item[8])
File "C:\projects\wo rkspace\INYR_ES _0.1\src\db.py" , line 19, in
insertAccount
cursor.execute( "INSERT INTO es_accounts (dateCreated,
accountNumber, description, openingBalance) VALUES (%s, %s, %s
, %d)", (date, accountNumber, description, dec))
File "c:\python25\li b\site-packages\MySQLd b\cursors.py", line 151,
in execute
query = query % db.literal(args )
TypeError: int argument required

My table is defined as:
CREATE TABLE es_accounts (
id int(6) not null auto_increment,
dateCreated date DEFAULT '0000-00-00',
accountNumber int(6) not null,
description varchar(255) not null,
openingBalance decimal(15,8) NOT NULL DEFAULT 0.00000000,
primary key (id)
);

TIA
The problem is your %d in Values, and then using a Decimal type to feed
it. Depending on the type of your openingBalance field, you may want to
change the %d to %s, or change dec to int(dec), or change the %d to %f
and change dec to float(dec). My experience with the Decimal type is
limited, so there may be other options as well.

~Ethan
Jul 15 '08 #4
Thanks for the information.
However I must stick to decimal since I'm dealing with monetary
values.
I loose the decimals with int and float has only supports one decimal
place.

I tried as you suggested to use the %s instead of the %d to no avail.
Jul 15 '08 #5
Thanks to all for your time and patience with this!
The insert is working fine based on the suggestions in this thread.

I now have another problem that should be resolved with a regular
expression to remove currency formatting before inserting into the db.
Jul 16 '08 #6

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

Similar topics

2
3946
by: Simon | last post by:
Hi, I am having a little problem with my PHP - MySQl code, I have two tables (shown below) and I am trying populate a template page with data from both. <disclaimer>Now I would like to say my skills, especially with MySQL are rudimentary</disclaimer> However my code (link below) fails, the nested database call does not return any data and this has me stumped. Any help will be much appreciated. Many thanks in advance
0
3526
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary form for a number of platforms from our download pages at http://www.mysql.com/downloads/ and mirror sites.
0
3946
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest version of this document can be found at: http://prdownloads.sourceforge.net/souptonuts/README_mysql.txt?download
1
3025
by: Saqib Ali | last post by:
I have created 2 tables in my MySQL database. A_TAB and B_TAB. They have auto-incrementing integer primary keys respectively named A_ID & B_ID. When I created B_TAB, I declared a field named A_ID which references A_TAB.AID. I insert valid data into both tables. However, the foreign key constraint is NOT being enforced. The database allows be to enter any integer into B_TAB.AID regardless of weather that value exists anywhere in the...
34
5067
by: Karam Chand | last post by:
Hello I have been working with Access and MySQL for pretty long time. Very simple and able to perform their jobs. I dont need to start a flame anymore :) I have to work with PGSQL for my companies current project. I have been able to setup postgresql in my rh box and
1
3379
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer uses his own php shopping cart to receive customer orders. The configuration was done via cPanel with no external modifications - which produced no protests when built, ran and connected with no
2
7047
josie23
by: josie23 | last post by:
Egad, I'm not a coder/programmer by nature or occupation but understand things like html and css and a small amount of perl. So, basically, I'm a perl/mysql imbecile. But, I've been trying to find syntax to insert values into a mysql database table. I'm able to use the below syntax to insert hard-coded values like 'josie' and 'smith' but can't find working syntax to insert $scalar data from another file (which is really what perl-mysql is...
6
38516
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get through this without much trouble. Programming knowledge is not required. Index What is SQL? Why MySQL? Installing MySQL. Using the MySQL command line interface
1
9582
ssnaik84
by: ssnaik84 | last post by:
Hi Guys, Last year I got a chance to work with R&D team, which was working on DB scripts conversion.. Though there is migration tool available, it converts only tables and constraints.. Rest of things (stored procedures, functions).. we have to manually edit. That time, we face some interesting challenges.. I failed to document all of them, but whatever I can share with u.. I will try.. :) ...
0
9541
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9370
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.