SQL: Exception using variables in queries

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NatashaB
    New Member
    • Mar 2008
    • 3

    SQL: Exception using variables in queries

    I'm getting this error when I try to insert a variable in a SQL query:

    'Exception occurred.', (0, 'Microsoft JET Database Engine', 'No value given for one or more required parameters.'

    Code:
    def linker (Function):
        conn = win32com.client.Dispatch(r'ADODB.Connection')
        DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=db8.mdb';
        conn.Open(DSN)
        rs = win32com.client.Dispatch(r'ADODB.Recordset')
        rs.Open("SELECT MAX(COID) as ID from SYS", conn)
        NewID=rs.Fields("ID").Value+1
        strNewID=str(NewID)
        conn.Close()
        sql_statement= "INSERT INTO Table1 (Comp, Fam) VALUES (" + strNewID + ", '" + Function + "')"
    The problem is with the Function variable. When I hardcode a string it works fine, and even when I print the statement, it looks exactly as when I hardcoded it. But for some reason, it doesn't like it like that.

    Any ideas?
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    You could always try to use string formatting instead... maybe see if there's really something else going on here?
    [code=python]
    sql_statement= "INSERT INTO Table1 (Comp, Fam) VALUES (%s, '%s')" % (strNewID, Function)[/code]

    Comment

    • NatashaB
      New Member
      • Mar 2008
      • 3

      #3
      Thanks for the suggestion. Just tried it--still the same error.

      Comment

      • NatashaB
        New Member
        • Mar 2008
        • 3

        #4
        Oh wait, it worked!

        Thank you!

        Comment

        Working...