simpli int/str problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sinan .

    #1

    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.
  • wittempj@hotmail.com

    #2
    Re: simpli int/str problem

    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>

    Comment

    • Paul McGuire

      #3
      Re: simpli int/str problem

      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

      Comment

      • Dennis Lee Bieber

        #4
        Re: simpli int/str problem

        On Fri, 12 Aug 2005 17:44:41 +0300, "sinan ."
        <orome.the.vala r@gmail.com> declaimed the following in comp.lang.pytho n:
        [color=blue]
        > 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.[/color]

        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.
        --[color=blue]
        > =============== =============== =============== =============== == <
        > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
        > wulfraed@dm.net | Bestiaria Support Staff <
        > =============== =============== =============== =============== == <
        > Home Page: <http://www.dm.net/~wulfraed/> <
        > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

        Comment

        Working...