469,279 Members | 2,200 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,279 developers. It's quick & easy.

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

Expand|Select|Wrap|Line Numbers
  1. def linker (Function):
  2.     conn = win32com.client.Dispatch(r'ADODB.Connection')
  3.     DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=db8.mdb';
  4.     conn.Open(DSN)
  5.     rs = win32com.client.Dispatch(r'ADODB.Recordset')
  6.     rs.Open("SELECT MAX(COID) as ID from SYS", conn)
  7.     NewID=rs.Fields("ID").Value+1
  8.     strNewID=str(NewID)
  9.     conn.Close()
  10.     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?
Mar 31 '08 #1
3 1631
314 100+
You could always try to use string formatting instead... maybe see if there's really something else going on here?
Expand|Select|Wrap|Line Numbers
  1. sql_statement= "INSERT INTO Table1 (Comp, Fam) VALUES (%s, '%s')" % (strNewID, Function)
Mar 31 '08 #2
Thanks for the suggestion. Just tried it--still the same error.
Mar 31 '08 #3
Oh wait, it worked!

Thank you!
Mar 31 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Stuart | last post: by
42 posts views Thread by PC Datasheet | last post: by
138 posts views Thread by Ian Boyd | last post: by
reply views Thread by Medhatithi | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.