How Insert if not exists with parameters? - actionscript-3

When trying to perform the following instructions I get an error: SQLError: 'Error #3115: SQL Error.', details:'near 'WHERE': syntax error', operation:'execute', detailID:'2003'. Any thoughts? Thanks!
dbStatement.text = "INSERT INTO person (idPerson,image) VALUES (:idPerson,:image) " +
"WHERE NOT EXISTS (SELECT idPerson FROM person WHERE idPerson=:idPerson)";
dbStatement.parameters[":idPerson"] = person.idPerson;
dbStatement.parameters[":image"] = person.image;
dbStatement.execute();

You're probably looking for INSERT OR REPLACE person ( ... ) VALUES( ... ).

Related

CTE + INSERT INTO + SQLAlchemy

I am trying to insert some date into another table. At first I´ve tried to use sqlalchemy to create such queries, but as I got some error when executing, I tried to solve it through raw SQL, but the error still the same.
I am not very used to CTE commands, so I don´t know if there are some restrinctions over them.
WITH Conv_Pre_Pagos AS
(SELECT CONVENIO.COD_IDEN, CONVENIO.D_CLIENTE_NOM
FROM db2rpc.CONVENIO
WHERE CONVENIO.COD_ESPC = 52)
INSERT INTO DB2I023A.ANL_TARF_PAGAS_PREPAGO (convenio, convenente) SELECT CBR_TARF_REC.NR_DOC_SIS_OGM, Conv_Pre_Pagos.D_CLIENTE_NOM
FROM DB2TFA.CBR_TARF_REC JOIN Conv_Pre_Pagos ON CBR_TARF_REC.NR_DOC_SIS_OGM = Conv_Pre_Pagos.COD_IDEN
The sentence is bigger, but I removed some data to bring it cleaner. Still, the same error:
ibm_db_dbi::ProgrammingError: SQLNumResultCols failed: [IBM][CLI Driver][DB2] SQL0199N The use of the reserved word "INSERT" following "INSERT" is not valid.
Expected tokens may include: "(SELECT ,". SQLSTATE=42601 SQLCODE=-199
[SQL: WITH Conv_Pre_Pagos AS (SELECT CONVENIO.COD_IDEN, CONVENIO.D_CLIENTE_NOM
FROM db2rpc.CONVENIO WHERE CONVENIO.COD_ESPC = 52)
INSERT INTO DB2I023A.ANL_TARF_PAGAS_PREPAGO (convenio, convenente)
SELECT CBR_TARF_REC.NR_DOC_SIS_OGM, Conv_Pre_Pagos.D_CLIENTE_NOM
FROM DB2TFA.CBR_TARF_REC JOIN Conv_Pre_Pagos ON CBR_TARF_REC.NR_DOC_SIS_OGM = Conv_Pre_Pagos.COD_IDEN]
(Background on this error at: https://sqlalche.me/e/14/f405)"
Where does it see an "insert following insert"?
Try this:
INSERT INTO DB2I023A.ANL_TARF_PAGAS_PREPAGO (convenio, convenente)
WITH Conv_Pre_Pagos AS
(
SELECT CONVENIO.COD_IDEN, CONVENIO.D_CLIENTE_NOM
FROM db2rpc.CONVENIO
WHERE CONVENIO.COD_ESPC = 52
)
SELECT CBR_TARF_REC.NR_DOC_SIS_OGM, Conv_Pre_Pagos.D_CLIENTE_NOM
FROM DB2TFA.CBR_TARF_REC
JOIN Conv_Pre_Pagos ON CBR_TARF_REC.NR_DOC_SIS_OGM = Conv_Pre_Pagos.COD_IDEN

Using IF And Executing A Query If It's True

I have a query that i want it to be executed in a condition, for instance the api key .
Human :
If Api key is in the Api database, do the following query, say 'You Are Not Allowed' .
What i tried :
Select IF ( api.key = 'myapikey' , TrueQuery , 'You are not allowed') from api
My problem is in the query, i'm getting a lot of errors, the query contains " SELECT ... FROM ... WHERE ... GROUP BY ... LEFT JOIN " .
What's the way to accomplish it ?
Following example returns null :
SELECT CASE WHEN (SELECT api.app FROM api WHERE api.app = 'Test' )
THEN (SELECT items.rom_id FROM items)
END
Try this, without using IF, but using EXISTS.
select *
from ( TrueQuery ) t
where exists(select 1 from api where api.key = 'myapikey')

How to use `CONCAT` in a `UPDATE`

I´m trying to use CONCAT in a mysql UPDATE.
"INSERT INTO table (
objekt_nr,
objekt_status)
VALUES(
:objekt_nr,
'salj,$fakt')
ON DUPLICATE KEY UPDATE
objekt_status = VALUES(CONCAT(objekt_status, 'salj,$fakt'))";
$query_params = array(
':objekt_nr' => $_POST['objekt_nr']);
I have tried several:
objekt_status = VALUES(CONCAT(objekt_status, objekt_status))";
objekt_status = VALUES(CONCAT(objekt_status, 'addMe'))";
objekt_status = VALUES(CONCAT(objekt_status, 'salj,$fakt'))";
objekt_status = VALUES((CONCAT(objekt_status, 'salj,$fakt')))";
Error Code for:
objekt_status = VALUES(CONCAT(objekt_status, 'salj,$fakt'))";
...syntax to use near '(objekt_status, 'salj,fakt,'))'
How should the code look like?
You have an semicolon where there should be a comma (after VALUES(objekt_nr);), and it appears the apostrophe is in the wrong place on the last line at $fakt. VALUES is only required for the INSERT, manual here
This query should be correct:
"INSERT INTO table (
objekt_nr,
objekt_status)
VALUES(
:objekt_nr,
'salj,$fakt')
ON DUPLICATE KEY UPDATE
objekt_nr = objekt_nr,
objekt_status = CONCAT(objekt_status, 'salj,$fakt')";
Also please ensure your variables are escaped, or use a prepared statement.
Try removing values as well as semicolon from the query
"INSERT INTO table (
objekt_nr,
objekt_status)
VALUES(
:objekt_nr,
'salj,$fakt')
ON DUPLICATE KEY UPDATE
objekt_nr = objekt_nr,
objekt_status = CONCAT(objekt_status, 'salj,'$fakt)";
Actually in my case i needed the "Values" for every line but the CONCAT line.
objekt_created_when = VALUES(objekt_created_when),
objekt_status = CONCAT(objekt_status, 'salj,$fakt') ";
If i did remove VALUES from all rows, values in db, got empty!

SQL INSERT INTO multiple tables

How can you make one query of this two?? I will insert data into two tables.
$query = "
INSERT INTO dc_mail_users (
i_id_pk, c_user, c_passwd_md5, i_user_active_id_fk, i_user_type_id_fk
) VALUES (
%1%, %2%, %3%, %4%, %5%
)";
$query2 = "
INSERT INTO dc_mail_user_data (
i_id_ut, c_user_sex, c_user_name, c_user_surname, c_user_url
) VALUES (
%1%, %2%, %3%, %4%, %5%
)";
You cannot insert into 2 tables with one query.
You would need to use a stored procedure where you can put that inserts.
What's the purpose of this? Are you trying to insert data into two different tables from one HTML form? I don't know about stored procedures but I use a transaction in similar case like this:
$d = dbSingle::dbLink();
//set autocommit to false
mysqli_autocommit($d->getDbc(), FALSE);
$query = " INSERT INTO dc_mail_users (
i_id_pk, c_user, c_passwd_md5, i_user_active_id_fk, i_user_type_id_fk
) VALUES (
%1%, %2%, %3%, %4%, %5%
)";
$r = $d->sqlQ($query);
//get the last inserted id for the second query
$last_insert_id = $d->getInsertId();
$query2 = "
INSERT INTO dc_mail_user_data (
i_id_ut, c_user_sex, c_user_name, c_user_surname, c_user_url
) VALUES (
%{$last_insert_id}%, %2%, %3%, %4%, %5% //not sure about the syntax, sorry
)";
$r2 = $d->sqlQ($query2);
//rollback if either one of the queries failed
if (!$r || (isset($r2) && !$r2)) {
mysqli_rollback($d->getDbc());
}
else {
//commit if everything worked
mysqli_commit($d->getDbc());
//autocommit on
mysqli_autocommit($d->getDbc(), TRUE);
}
This assumes i_id_ut in the table dc_mail_user_data is the FK and the i_id_pk is an auto increment field. I have a class called dbSingle that contains the query functions and database connection. Hope it's clear enough to be used with regular mysqli functions.
You can do with trigger or stored procedures but not with simple insert query.
$query = "
INSERT INTO dc_mail_users
(i_id_pk, c_user, c_passwd_md5, i_user_active_id_fk, i_user_type_id_fk)
VALUES (%1%, %2%, %3%, %4%, %5%)
";
$query2 = "
INSERT INTO dc_mail_user_data
(c_user_sex, c_user_name, c_user_surname, c_user_url)
VALUES (%1%, %2%, %3%, %4%)";
// start query 1
$dbh = new DB_Mysql_Extended;
$dbh->prepare($query)->execute($this->i_id_pk, $this->c_user, $this->c_passwd_md5, $this->i_user_active_id_fk, $this->i_user_type_id_fk);
// start query 2
$dbh2 = new DB_Mysql_Extended;
$dbh2->prepare($query2)->execute($this->c_user_sex, $this->c_user_name, $this->c_user_surname, $this->c_user_url);

ON DUPLICATE KEY not working correctly

I am running the following query, but getting an error. I think it is coming from the ON DUPLICATE KEY part, but I'm not 100% sure what the correct syntax is to use there.
<?php
$form_id = $form->data['form_id'];
$cfid = $form->data['cf_id'];
$datesent = $form->data['datesent'];
$query = mysql_query("
INSERT INTO `email_history` (
`cf_id` ,
`$form_id`
)
VALUES (
'$cfid', '$datesent'
)
ON DUPLICATE KEY
UPDATE INTO
`email_history` (
`$form_id`
)
VALUES (
'$datesent'
);
") or die(mysql_error());
?>
EDIT
Using the following I am getting this error :
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 10
<?php
$form_id = $form->data['form_id'];
$cfid = $form->data['cf_id'];
$datesent = $form->data['datesent'];
$query = mysql_query("
INSERT INTO `email_history` (
`cf_id` ,
`$form_id`
)
VALUES (
'$cfid', '$datesent'
)
ON DUPLICATE KEY
UPDATE `$form_id` = `$datesent`
);
") or die(mysql_error());
?>
The correct syntax of ON DUPLICATE KEY is something a long the lines described below.
Please note that it's just an example snippet, though it should be clear why your provided snippet fails to execute.
INSERT INTO tbl (
col1, col2, ... , colN
) VALUES (
#val1, #val2, ..., #valN
) ON DUPLICATE KEY UPDATE
col3 = VALUES(col3), col4 = #val4
Documentation
MySQL 5.0 Reference Manual :: 12.2.5.3 INSERT ... ON DUPLICATE KEY UPDATE Syntax
How would that look in my code?
$form_id = $form->data['form_id'];
$cfid = $form->data['cf_id'];
$datesent = $form->data['datesent'];
$query = mysql_query (<<<EOT
INSERT INTO `email_history` (`cf_id`, `$form_id`)
VALUES ('$cfid', '$datesent')
ON DUPLICATE KEY UPDATE `$form_id` = VALUES(`$form_id`)
EOT
) or die (mysql_error ());
What does VALUES($form_id) do?
It will yield the value of what was originally passed as the value for column named $form_id.
What is the use of <<<EOT?
In the previous snippet we are using a HEREDOC to have a string span multiple lines in an easy manner. You can read more about it in the documentation for heredocs.