tags:

views:

175

answers:

4
qryreg.SQL.Add('Insert into RegistreerTB');
qryreg.SQL.add('Name , Surname, E-mail, Password)');
qryreg.SQL.Add('Values ('+quotedstr(edtname.Text)+','+quotedstr(edtsname.Text)+','+quotedstr(edtemail.Text)+','+quotedstr(edtpassuse.Text)+')');
qryreg.ExecSQL ;
qryreg.SQL.Text := 'Select * from RegistreerTB';
qryreg.Open ;

This is the code im using atm with delphi im trying to save data to my database from editboxes. The error im getting is EOELeException "Insert into statement"

ty in advance

+1  A: 

Your problem is in the first line. I made the correction below. you need an opening parenthesis.

qryreg.SQL.Add('Insert into RegistreerTB ('); 
qryreg.SQL.Add('Name , Surname, E-mail, Password)'); 
qryreg.SQL.Add('Values ('+quotedstr(edtname.Text)+','+quotedstr(edtsname.Text)+','+quotedstr(edtemail.Text)+','+quotedstr(edtpassuse.Text)+')'); 
qryreg.ExecSQL ; 
qryreg.SQL.Text := 'Select * from RegistreerTB'; 
qryreg.Open ; 

see if this works

qryreg.SQL.Add("Insert into RegistreerTB ("); 
qryreg.SQL.Add("Name , Surname, E-mail, Password)"); 
qryreg.SQL.Add("Values ('"+edtname.Text+"','"+edtsname.Text +"','"+edtemail.Text+"','"+edtpassuse.Text +"')"); 
qryreg.ExecSQL ; 
qryreg.SQL.Text := "Select * from RegistreerTB"; 
qryreg.Open ; 
John Hartsock
i have changed the code as you said i still get the same error i have been trying to figure this out for past hour and i can't find my error but thanks anyway :)
Shadowriz
@Shadowriz... What does quoted string return for example: quotedstr("hello") will it return "hello" or 'hello'
John Hartsock
@Shadowriz: What John stated seems like the only problem with this code(I hope quotedstr returns single quotes). Are you sure your table definition fits this statement? Are you sure your field names are correct?
Erkan Haspulat
ye the field names are correct @john i have never done this before i am working from a textbook atm :) quotedstr(edt1.text) wil return the text entered into the edit box idk if that was of any help but ty for comments
Shadowriz
@ joe I have added it still same error :(
Shadowriz
@Shadowriz...try the code I added...let me know if it works
John Hartsock
A: 
  1. May be you have to call qryreg.SQL.Clear before your first line.
  2. Why not to use parameters ?
oodesigner
hey im using an example from a textbook and i have just changed the values to the ones im using atm could you please elaborate on paramaters
Shadowriz
@Shadowriz: please include the name of the textbook in your answer.
Jeroen Pluimers
+4  A: 

As John's answer points out, you need to have parentheses around the column names before VALUES. You need to make sure all the column names are valid SQL identifiers. If they aren't, as in the case for E-mail, you need to quote or escape them according to your database's syntax rules. For example, MySQL uses grave accents, Microsoft SQL uses brackets, and Oracle and Postgresql use quotation marks.

Rob Kennedy
+1 Good catch. I completely overlooked that one.
Joe Stefanelli
+7  A: 

As oodesigner stated, a better method would be to use parameters. I don't know what text book you are looking at, but the code given isn't really best practice (it isn't worst practice either, at least it uses QuotedStr rather than '''' + edtname.Text + '''' which fails the first time you use something like O'Connell, and allows SQL injection attacks.

Using parameters and assuming SQL Server syntax as per Rob's answe, and assuming TADOQuery (based on the EOLEException) the code would be something like:

qryreg.SQL.Add('Insert into RegistreerTB');  
qryreg.SQL.Add('(Name , Surname, [E-mail], Password)');  //SQL Server syntax with square brackets

// OR qryreg.SQL.Add('(Name , Surname, "E-mail", Password)');  //Oracle/Postgres syntax with double quotes
// OR qryreg.SQL.Add('(Name , Surname, `E-mail`, Password)');  //MySQL syntax with grave accent

qryreg.SQL.Add('Values :Name, :Surname, :Email, :Password)'); 

qryreg.Parameters.ParamByName('Name').Value := edtName.Text;
qryreg.Parameters.ParamByName('Surname').Value := edtSName.Text;
qryreg.Parameters.ParamByName('Email').Value := edtEmail.Text;
qryreg.Parameters.ParamByName('Password').Value := edtPassUse.Text;

qryreg.ExecSQL;  
qryreg.SQL.Text := 'Select * from RegistreerTB';  
qryreg.Open ;  
Gerry
Parameters also avoid hard-parsing the query each time, and allow reuse an already parsed statements. Some database don't "like" much being flooded by a lot of statements as it happens when the same statement is resubmitted over and over again just changing some literals.
ldsandon
Ty i will look into using paramters
Shadowriz