Delphi SQL insert into statement error - sql

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

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 ;

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.

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 ;

May be you have to call qryreg.SQL.Clear before your first line.
Why not to use parameters ?

Related

Perl concatenation for SQL query

I'm trying to transfer the content of a CSV file into a table in PostgreSQL using Perl.
I'm able to update my table successfully, but the terminal returns an error:
Use of uninitialized value in concatenation (.) or string
Syntax error near ","
INSERT INTO test VALUES (, '', '', '', '',, )
Here is the code where it fails :
for (my $i=0 ; $i<=50; $i++){
$dbh ->do("INSERT INTO test VALUES ('$LastName[$i]', '$Street[$i]', $Balance_account[$i])") ;
If more information is needed just ask them.
Sorry for the bad English.
--
Thomas
Use placeholders,
for (my $i=0 ; $i<=50; $i++){
$dbh->do("INSERT INTO test VALUES (?,?,?)",
undef,
$LastName[$i], $Street[$i], $Balance_account[$i]
);
}
Ideally, you should prepare the query and execute for each set of values, something like this:
my $sth = $dbh->prepare('INSERT INTO test VALUES (?,?,?)');
for (my $i=0 ; $i<=50; $i++){
$sth->execute($LastName[$i], $Street[$i], $Balance_account[$i]);
}
My guess is that your error is being caused by not specifying the column names in your insert, while simultaneously having the wrong number/types of columns. I would expect the following Perl code to not error out:
for (my $i=0 ; $i<=50; $i++){
$dbh -> do("INSERT INTO test (lastname, street, balance) VALUES ('$LastName[$i]', '$Street[$i]', $Balance_account[$i])");
Here is what a working query might look like:
INSERT INTO test (lastname, street, balance)
VALUES
('Skeet', '100 Thatcher Street', 100.50);
It is generally considered bad practice to not include column names while doing an insert, because even if you get it right, it could easily break later on.

Apex parse error when creating SQL query with sql function

I have the following function:
CREATE OR REPLACE FUNCTION calc_a(BIDoctor number) RETURN number
IS
num_a number;
BEGIN
select count(NAppoint)
into num_a
from Appointment a
where BIDoctor = a.BIDoctor;
RETURN num_a;
END calc_a;
What we want is adding a column to a report that shows us the number of appointments that doc have.
select a.BIdoctor "NUM_ALUNO",
a.NameP "Nome",
a.Address "Local",
a.Salary "salary",
a.Phone "phone",
a.NumberService "Curso",
c.BIdoctor "bi",
calc_media(a.BIdoctor) "consultas"
FROM "#OWNER#"."v_Doctor" a, "#OWNER#"."Appointment" c
WHERE a.BIdoctor = c.BIdoctor;
and we got this when we are writing the region source on apex.
But it shows a parse error, I was looking for this about 2 hours and nothing.
Apex shows me this:
PARSE ERROR ON THE FOLLOWING QUERY
This is probably because of all your double quotes, you seem to have randomly cased everything. Double quotes indicate that you're using quoted identifiers, i.e. the object/column must be created with that exact name - "Hi" is not the same as "hi". Judging by your function get rid of all the double quotes - you don't seem to need them.
More generally don't use quoted identifiers. Ever. They cause far more trouble then they're worth. You'll know when you want to use them in the future, if it ever becomes necessary.
There are a few more problems with your SELECT statement.
You're using implicit joins. Explicit joins were added in SQL-92; it's time to start using them - for your future career where you might interact with other RDBMS if nothing else.
There's absolutely no need for your function; you can use the analytic function, COUNT() instead.
Your aliases are a bit wonky - why does a refer to doctors and c to appointments?
Putting all of this together you get:
select d.bidoctor as num_aluno
, d.namep as nome
, d.address as local
, d.salary as salary
, d.phone as phone
, d.numberservice as curso
, a.bidoctor as bi
, count(nappoint) over (partition by a.bidoctor) as consultas
from #owner#.v_doctor a
join #owner#.appointment c
on d.bidoctor = a.bidoctor;
I'm guessing at what the primary keys of APPOINTMENT and V_DOCTOR are but I'm hoping they're NAPPOINT and BIDOCTOR respectively.
Incidentally, your function will never have returned the correct result because you haven't limited the scope of the parameter in your query; you would have just counted the number of records in APPOINTMENT. When you're naming parameters the same as columns in a table you have to explicitly limit the scope to the parameter in any queries you write, for instance:
select count(nappoint) into num_a
from appointment a
where calc_a.bidoctor = a.bidoctor; -- HERE

[ODBC Microsoft Access Driver]COUNT field incorrect

$q = 'INSERT INTO MyTable(proddesc, qnty, PriceH, PriceA, PriceL) VALUES(?,?,?,?,?)';
$sth = odbc_prepare($dbConn, $q);
$success = odbc_execute($sth, array(my 5 variables that are not null));
It gives me the above error - [ODBC Microsoft Access Driver] COUNT field incorrect. I know that the query is correct because I ran it in Access and it was fine. I think I may be using the prepare/execute statements incorrectly.
I also encountered this now and the solution I did to fix it is to quote the variables properly.
Try printing your $q and you will see if it needs to be quoted.
You can try these too:
INSERT INTO TABLE -- quote db and table names using (`) "grave accent" character
VALUES( 'Fed''s' ) -- quote the apostrophes

SQL- Adding a condition

I am just starting to learn SQL.
How do you add a condition to a statement? I am trying to sort the destination to 'BNA' which is the airport code.
SELECT
CHARTER.CUS_CODE,
CHARTER.DESTINATION "AIRPORT",
CHARTER.CHAR_DATE,
CHARTER.CHAR_DISTANCE,
CHARTER.AC_NUMBER,
FROM C.CHARTER ;
WHERE DESTINATION = 'BNA' ;
Any hints in the right direction would be great.
The following is your query with the syntax corrected:
SELECT CHARTER.CUS_CODE,
CHARTER.DESTINATION "AIRPORT",
CHARTER.CHAR_DATE,
CHARTER.CHAR_DISTANCE,
CHARTER.AC_NUMBER
FROM CHARTER
WHERE DESTINATION = 'BNA';
The semicolon goes at the end only.
Get rid of "c." from the table name in your from clause. You might have been thinking of giving it an alias of "c" which, if if that's the case, you would put it after the table name (and then use it as a prefix for each field).
SELECT
CHARTER.CUS_CODE,
CHARTER.DESTINATION "AIRPORT",
CHARTER.CHAR_DATE,
CHARTER.CHAR_DISTANCE,
CHARTER.AC_NUMBER,
FROM C.CHARTER
WHERE DESTINATION = 'BNA' ;
The ; character is a statement terminator; you only need one per SQL statement.
there is ";" at the end of the FROM statement, remove it. and try the sql again. Pay attention with the double quote too on the AIRPORT text.
SELECT CHARTER.DESTINATION + 'AIRPORT '
FROM C.CHARTER
WHERE DESTINATION = 'BNA' ;

Add Special Characters to H2 database

Working with H2 I get this error when I try to write a row with the first element being
ABC and the second being [C#26afa68a
Syntax error in SQL statement "INSERT INTO USER VALUES(ABC,[[*]C#F4D5BC9) " expected "), DEFAULT, NOT, EXISTS, SELECT, FROM"; SQL statement:INSERT INTO user VALUES(abc,[C#f4d5bc9) [42001-167]
I don't know if there is a way to get H2 to accept Special Characters, but it would be great to know how to deal with this.
Thanks!
You should use a PreparedStatement:
PreparedStatement prep = conn.prepareStatement("INSERT INTO USER VALUES(?, ?)");
prep.setString(1, "ABC");
prep.setString(2, "[C#f4d5bc9");
prep.executeUpdate();
prep.close();
Using a PreparedStatement is the preferred solution, because that way you don't have to escape the data. If ABC and / and [C#f4d5bc9 are constants, you could use:
Statement stat = conn.createStatement();
stat.executeUpdate("INSERT INTO USER VALUES('ABC', '[C#f4d5bc9');
stat.close();