FAQ
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.

Search Discussions

  • Wittempj at Aug 12, 2005 at 2:54 pm
    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>
  • Paul McGuire at Aug 12, 2005 at 3:02 pm
    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
  • Paolino at Aug 12, 2005 at 3:55 pm

    sinan . wrote:
    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.
    Try use:
    SQL = "INSERT INTO (`AH`, `BH` ) VALUES
    ('%s,%d)"%(dict['str_name'],dict['int_name'])

    Paolino


    ___________________________________
    Yahoo! Messenger: chiamate gratuite in tutto il mondo
    http://it.beta.messenger.yahoo.com
  • Scott David Daniels at Aug 12, 2005 at 6:08 pm

    Paolino wrote:
    sinan . wrote:
    hi all,
    i have a string and int values in same dictionary like this
    dict = {'str_name': 'etc' , 'int_name' : 112 }
    Bad idea to call this "dict" -- that is a system-defined name
    for the dictionary type. I'll use "holder" below.
    the error occures when do this
    SQL = "INSERT INTO (`AH`, `BH` ) VALUES ('" + dict['str_name'] + "',
    '" + dict['int_name'] + "')"
    ^^ I suspect this should be:
    SQL = "INSERT INTO TABLENAME(AH, BH) VALUES (...
    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.
    Try use:
    SQL = "INSERT INTO (`AH`, `BH` ) VALUES
    ('%s,%d)"%(dict['str_name'],dict['int_name'])
    This will work, but the DB interface allows you to leave the
    variable formatting to it. Depending on the parameter style
    your DB interface supports, you may use at least one of:

    cursor.execute("INSERT INTO TABLENAME(AH, BH) VALUES(?, ?)",
    [(holder['str_name'], holder['int_name'])])
    or
    cursor.execute("INSERT INTO TABLENAME(AH, BH) VALUES(:1, :2)",
    [(holder['str_name'], holder['int_name'])])
    ### I think, it _might_ be ...(0, :1)",
    or
    cursor.execute("INSERT INTO TABLENAME(AH, BH) "
    "VALUES(:str_name, :int_name)",
    holder)

    --Scott David Daniels
    Scott.Daniels at Acm.Org
  • Sinan . at Aug 13, 2005 at 11:52 am
    i thank you all, first of al ill change my dictinary variable name :)
    then ill use %s and %d . and thanks for other examples,these examples
    enchance my python-way thinking. :)
    On 8/12/05, Scott David Daniels wrote:
    Paolino wrote:
    sinan . wrote:
    hi all,
    i have a string and int values in same dictionary like this
    dict = {'str_name': 'etc' , 'int_name' : 112 }
    Bad idea to call this "dict" -- that is a system-defined name
    for the dictionary type. I'll use "holder" below.
    the error occures when do this
    SQL = "INSERT INTO (`AH`, `BH` ) VALUES ('" + dict['str_name'] + "',
    '" + dict['int_name'] + "')"
    ^^ I suspect this should be:
    SQL = "INSERT INTO TABLENAME(AH, BH) VALUES (...
    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.
    Try use:
    SQL = "INSERT INTO (`AH`, `BH` ) VALUES
    ('%s,%d)"%(dict['str_name'],dict['int_name'])
    This will work, but the DB interface allows you to leave the
    variable formatting to it. Depending on the parameter style
    your DB interface supports, you may use at least one of:

    cursor.execute("INSERT INTO TABLENAME(AH, BH) VALUES(?, ?)",
    [(holder['str_name'], holder['int_name'])])
    or
    cursor.execute("INSERT INTO TABLENAME(AH, BH) VALUES(:1, :2)",
    [(holder['str_name'], holder['int_name'])])
    ### I think, it _might_ be ...(0, :1)",
    or
    cursor.execute("INSERT INTO TABLENAME(AH, BH) "
    "VALUES(:str_name, :int_name)",
    holder)

    --Scott David Daniels
    Scott.Daniels at Acm.Org
    --
    http://mail.python.org/mailman/listinfo/python-list

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 12, '05 at 2:44p
activeAug 13, '05 at 11:52a
posts6
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase