By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,655 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

SQL: Exception using variables in queries

P: 3
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 + "')"
  11.  
  12.  
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
Share this Question
Share on Google+
3 Replies


jlm699
100+
P: 314
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

P: 3
Thanks for the suggestion. Just tried it--still the same error.
Mar 31 '08 #3

P: 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.