passing artibrary strings into a database

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • schwehr@gmail.com

    #1

    passing artibrary strings into a database

    Hi All,

    I was wondering if there is a helper library out there that will nicely
    encode artibrary text so that I can put in into a TEXT field in a
    database and then retrieve it without getting into trouble with ',",new
    lines or other such things that would foul the sql insert call and or
    be a security hazard? This feels like a newbee type question, but I
    haven't found anything with a quick search.

    Thanks,
    -kurt

  • Fredrik Lundh

    #2
    Re: passing artibrary strings into a database

    schwehr@gmail.c om wrote:
    [color=blue]
    > I was wondering if there is a helper library out there that will nicely
    > encode artibrary text so that I can put in into a TEXT field in a
    > database and then retrieve it without getting into trouble with ',",new
    > lines or other such things that would foul the sql insert call and or
    > be a security hazard?[/color]

    don't ever use string formatting to add values to an SQL statement.
    the right way to pass variables to the database engine is to use para-
    meters (aka bound variables):

    cursor.execute(
    "insert into table (col1, col2) values ?, ?",
    value1, value2
    )

    the exact marker depends on the database; use the paramstyle attribute
    to figure out what's the right parameter marker to use for your database.
    see the DB-API 2 spec for more information:

    This API has been defined to encourage similarity between the Python modules that are used to access databases. By doing this, we hope to achieve a consistency leading to more easily understood modules, code that is generally more portable across datab...


    </F>



    Comment

    • Diez B. Roggisch

      #3
      Re: passing artibrary strings into a database

      schwehr@gmail.c om wrote:[color=blue]
      > Hi All,
      >
      > I was wondering if there is a helper library out there that will nicely
      > encode artibrary text so that I can put in into a TEXT field in a
      > database and then retrieve it without getting into trouble with ',",new
      > lines or other such things that would foul the sql insert call and or
      > be a security hazard? This feels like a newbee type question, but I
      > haven't found anything with a quick search.[/color]

      Use paramtetrized cursor.execute( ..) That is instead of doing

      c.execute("inse rt into foo values ('%s')" % mytext)

      do

      c.execute("inse rt into foo values (?)", mytext)

      Attention, the actual style of a parameter is dependand on your
      database, e.g. oracle uses a differnet one:

      c.execute("inse rt into foo values (:mytext)", dict(mytext=myt ext))


      The actual style to use is given in the docs, or can be queried with

      connection.para mstyle

      I recommend reading the DB-API 2.0 specs.

      Diez

      Comment

      • schwehr@gmail.com

        #4
        Re: passing artibrary strings into a database

        Thanks! Looks like I need to get a newer version of pysqlite into the
        fink package tree since pysqlite 1.0.1 does not appear support that

        Comment

        Working...