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

simpli int/str problem

hi all,
i have a string and int values in same dictionary like this
dict = {'str_name': 'etc' , 'int_name' : 112 }
the error occures when do this
SQL = "INSERT INTO (`AH`, `BH` ) VALUES ('" + dict['str_name'] + "',
'" + dict['int_name'] + "')"
cursor.execute(SQL)
python does not accep dict['int_name'] in SQL variable but when i
convert this variable to the str , python accepts but i cannot insert
that into database because database only accept int in `BH `
thanks.
Aug 12 '05 #1
3 1497
Use substitution like below.
Hope this helps

py> d = {'str_name': 'etc' , 'int_name' : 112 }
py> SQL = "INSERT INTO (`AH`, `BH` ) VALUES ('" + d['str_name'] + "',
'" + d['int_name'] + "')"

Traceback (most recent call last):
File "<pyshell#1>", line 1, in -toplevel-
SQL = "INSERT INTO (`AH`, `BH` ) VALUES ('" + d['str_name'] + "',
'" + d['int_name'] + "')"
TypeError: cannot concatenate 'str' and 'int' objects
py> SQL = "INSERT INTO (`AH`, `BH` ) VALUES ('%s', %d)" %
(d['str_name'], d['int_name'])
py> print SQL
INSERT INTO (`AH`, `BH` ) VALUES ('etc', 112)
py>

Aug 12 '05 #2
I just did this sort of thing the other day!

Your database only accepts ints for BH, but remember, you are building
an SQL *string* to be executed. To show SQL that your BH value is an
int, not a string, do not enclose it in quotes.

(Another style hint: don't name dict's "dict", as this will mask the
actual type name. Let's try "vDict" for now, meaning "value dict".)

SQL = "INSERT INTO XYZ('AH', 'BH' ) VALUES ('" + vDict['str_name'] + \
"', " + str(vDict['int_name']) + ")"

In my program, I found it a bit easier to follow if I used string
interpolation (the string % operation), and named format fields. Try
this:

SQL = "INSERT INTO XYZ('AH', 'BH' ) VALUES ('%(str_name)s',
%(int_name)d)" % vDict

Again, note that the string value is surrounded by quotes, but the
integer value is not.

Also, you will need to replace XYZ with the actual table name. :)

-- Paul

Aug 12 '05 #3
On Fri, 12 Aug 2005 17:44:41 +0300, "sinan ."
<or*************@gmail.com> declaimed the following in comp.lang.python:
hi all,
i have a string and int values in same dictionary like this
dict = {'str_name': 'etc' , 'int_name' : 112 }
the error occures when do this
SQL = "INSERT INTO (`AH`, `BH` ) VALUES ('" + dict['str_name'] + "',
'" + dict['int_name'] + "')"
cursor.execute(SQL)
python does not accep dict['int_name'] in SQL variable but when i
convert this variable to the str , python accepts but i cannot insert
that into database because database only accept int in `BH `
thanks.
Ignoring that naming a dictionary "dict" is potentially a
problem...

You don't mention which database module, since they sometimes
have different "wildcards" but most are designed to do the string
replacement IN THE .execute() CALL...

rawSQL = "insert into ('AH', 'BH') values (%s, %s)"
cursor.execute(rawSQL, (dict["str_name"], dict["int_name"]))

Some may even support dictionary replacement

rawSQL = "insert into ('AH', 'BH') values (%(str_name)s, %(int_name)s)"
cursor.execute(rawSQL, dict)

One should NOT be trying to format the data values into a static
SQL string to be fed to .execute() -- .execute() itself will determine
the quoting needed by the arguments.
-- ================================================== ============ <
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/> <

Aug 12 '05 #4

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

Similar topics

0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
0
by: Refky Wahib | last post by:
Hi I need Technical Support I finished a Great project using .Net and SQL Server and .Net Mobile Control My Business case is to implement this Program to accept about 1 Million concurrent...
9
by: Sudesh Sawant | last post by:
Hello, We have an application which communicates using remoting. There is a server which is a Windows Service. The server exposes an object which is a singleton. The client is a Web Application...
117
by: Peter Olcott | last post by:
www.halting-problem.com
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.