Is it possible to get the result generated by eclipse link? - mysql

I have this entity with Generationtype table working..
#Id
#Basic(optional = false)
#Column(name = "app_users_pk")
#TableGenerator( name = "appseqstore", table = "app_seq_store", pkColumnName = "app_seq_name", pkColumnValue = "app_users_pk", valueColumnName = "app_seq_value", initialValue = 1, allocationSize = 1 )
#GeneratedValue( strategy = GenerationType.TABLE,generator = "appseqstore")
private Long appUsersPk;
when I execute a create command this is what eclipselink do.. (on my glassfish log)
UPDATE app_seq_store SET app_seq_value = app_seq_value + ? WHERE app_seq_name = ?
bind => [1, app_users_pk]
SELECT app_seq_value FROM app_seq_store WHERE app_seq_name = ?
bind => [app_users_pk]
INSERT INTO app_users (app_users_pk, username) VALUES (?, ?)
bind => [33, try lang]
SELECT app_users_pk, username FROM app_users
When I check the mysql log I found that eclipselink make 3 queries
1st is to update the table by incrementing by 1
2nd get the incremented value
and 3rd insert using the the incremented value
(on mysql query log)
SET autocommit=0
UPDATE app_seq_store SET app_seq_value = app_seq_value + 1 WHERE app_seq_name = 'app_users_pk'
SELECT app_seq_value FROM app_seq_store WHERE app_seq_name = 'app_users_pk'
INSERT INTO app_users (app_users_pk, username) VALUES (33, 'try lang')
commit
SET autocommit=1
now I see that eclipselink issued an "select" query..
is it possible to get the result from that query? because i also want that key to be used in other related table, or is their any other way to achieve this purpose?

Related

SQL UPDATE Not Updating

After uploading a csv file, I am trying to insert its contents into my database table. I have this query:
$connect = mysql_connect("localhost","root","");
mysql_select_db("dbtest",$connect);
//get the file
$handle = fopen($filename,"r");
do {
if (isset($data[0])) {
$data0 = mysql_real_escape_string($data[0]); //rcode
$data1 = mysql_real_escape_string($data[1]); //pcode
$data2 = mysql_real_escape_string($data[2]); //mcode
$data3 = mysql_real_escape_string($data[3]); //bcode
$data4 = mysql_real_escape_string($data[4]); //ecode
$data5 = mysql_real_escape_string($data[5]); //filetype
$data6 = mysql_real_escape_string($data[6]); //rec_count
$data7 = mysql_real_escape_string($data[7]); //gen_count
$data8 = mysql_real_escape_string($data[8]); //qc_count
$data9 = mysql_real_escape_string($data[9]); //be_count
$data10 = mysql_real_escape_string($data[10]); //trn_count
$query = "INSERT INTO tbltest(rcode,pcode,mcode,bcode,ecode,filetype,rec_count,
gen_count,qc_count,be_count,trn_count) VALUES ('$data0','$data1','$data2',
'$data3', '$data4', '$data5', '$data6', '$data7', '$data8', '$data9', '$data10')
ON DUPLICATE KEY UPDATE rec_count=values(rec_count),gen_count=values(gen_count),
qc_count=values(qc_count), be_count=values(be_count), trn_count=values(trn_count)";
mysql_query ($query,$connect) ;
}
} while ($data = fgetcsv($handle,1000,"|"));
And it's working neatly but then as the database was re-structured, I just then need to update the database table as rcode to filetype has values already and I just need to insert values of rec_count to trn_count. So my first query INSERT INTO ... ON DUPLICATE KEY UPDATE has been change to UPDATE only. And so I did this:
$query = "UPDATE tbltest SET (rec_count='$data6', gen_count = '$data7',
qc_count = '$data8', be_count = '$data9', trn_count= '$data10') WHERE
(rcode = '$data0', pcode = '$data1', mcode = '$data2', bcode = '$data3',
ecode = '$data4', filetype = '$data5')";
My problem now is that, my UPDATE seems to be not working as it doesn't update the database table. Bu when I did this;
$query = "UPDATE tbltest SET rcode = '5'";
The database is being updated. When I tried echo $query;, the echo responds the correct data (from the csv). I just cannot figure why it doesn't insert these data into the database. Kindly help. Thanks
Your SQL syntax is incorrect. The statement should read something like
UPDATE tbltest
SET rec_count='...', gen_count = '...', ...
WHERE rcode = '...' AND pcode = '...' AND ...
See MySQL UPDATE syntax.

Its code cannot update or insert data in to mysql db

$retval = mysql_query("
IF EXISTS(SELECT * FROM config WHERE distance1 = '{$distance1}')
UPDATE config SET distance2 = $distance2, distance3 = $distance3, totaldistance = $totaldistance, passengers = $passengers, chairwell = $chairwell, babychairs = $babychairs, companions = $ppc, luggage = $ppl, pet = $ppp, insurance = $in, stopinway = $siw where distance1 = $distance1
ELSE
INSERT into config(distance1, distance2, distance3, totaldistance, passengers, chairwell, babychairs, companions, luggage, pet, insurancestopinway) values('$distance1', '$distance2', '$distance3', '$totaldistance', '$passengers', '$chairwell', '$babychairs', '$ppc', '$ppl', '$ppp', '$in', '$siw')
");
I think that it's better to use INSERT INTO ... ON DUPLICATE KEY UPDATE sintax.
But you have to set column distance1 as Unique, before.
So this will be your new query:
INSERT into config (
distance1, distance2, distance3,
totaldistance, passengers, chairwell,
babychairs, companions, luggage,
pet, insurancestopinway)
values (
'$distance1', '$distance2', '$distance3',
'$totaldistance', '$passengers', '$chairwell',
'$babychairs', '$ppc', '$ppl',
'$ppp', '$in',
'$siw' #ATTENTION: you have 8 columns but 9 values, this will be generate an error
)
ON DUPLICATE KEY UPDATE
distance2 = '$distance2',
distance3 = '$distance3'
#...
#and so on
");

Insert on first request; update on all subsequent requests

I'm inserting a row into a MySQL database table. On the first insertion I want a new row to be added, but after that I just want that row to be updated. Here's how I'm doing it. An Ajax request calls the following php file:
<?php
include "base.php";
$bookID = $_POST['bookID'];
$shelfID = $_POST['shelfID'];
$userID = $_SESSION['user_id'];
$query = mysql_query("SELECT shelfID FROM shelves WHERE userID = '$userID' AND shelfID = '$shelfID' AND bookID = '$bookID'");
if (mysql_num_rows($query) == 0) {
$insert = "INSERT INTO shelves (bookID,shelfID,userID) VALUES ('$bookID','$shelfID','$userID')";
mysql_query($insert) or die(mysql_error());
} elseif (mysql_num_rows($query) == 1) { //ie row already exists
$update = "UPDATE shelves SET shelfID = '$shelfID' WHERE userID = '$userID' AND bookID = '$bookID'";
mysql_query($update) or die(mysql_error());
}
?>
As it stands it adds a new row every time.
You should consider using PDO for data access. There is a discussion on what you need to do here: PDO Insert on Duplicate Key Update
I'd flag this as duplicate, but that question is specifically discussing PDO.
You can use the INSERT ... ON DUPLICATE KEY UPDATE syntax. As long as you have a unique index on the data set (i.e. userid + shelfid + bookid) you are inserting, it will do an update instead.
http://dev.mysql.com/doc/refman/5.5/en/insert-on-duplicate.html

update mysql database with jdbc

I have an error updating my database because of variables. This is my code:
UPDATE `payment` SET `paid`=1 AND `amoun`=$amountpaid WHERE `paid`=0 AND `userid`=$uid
$amountpaid is the amount of the bill that the user paid and $uid is user id. It seems like using $ in front of variable names is forbidden. How can I use variables in SQL?
Where are your variables coming from? You probably want something like this if you're using JDBC:
int setPaid = 1;
int amountPaid = x; // put $amountpaid here
int wherePaid = 0;
int userId = y; // put $uid here
String updateQuery = "UPDATE payment SET paid = ?, amoun = ?"
+ " WHERE paid = ? AND userid = ?";
PreparedStatement ps = con.prepareStatement(updateQuery);
ps.setInt(1, setPaid);
ps.setInt(2, amountPaid);
ps.setInt(3, wherePaid);
ps.setInt(4, userId);
ps.executeUpdate();
I got the solution by using String.
I converted the ArrayList to a String and then sent the data as string. The data got updated but I don't know what will happen next if I want to view the data in the client tier...

Update with Zend_DB on multiple rows

I am using Zend Framework. I have tree tables. Users and Groups and one table linking them.
I want to increment a field from users of a given group. To increment one User I do:
$table = 'users';
$update = array(
'ACLVersion' => new Zend_Db_Expr('ACLVersion + 1')
);
$where[] = $db->quoteInto('id = ?', $user);
$db->update($table, $update, $where);
I tried to use multiple wheres.
I have no clue how to join the tables in a where with Zend.
To use a JOIN with Zend_Db_Table, you have to disable the integrity check.
See example #27 in the ZF Reference Guide for Zend_Db_Table:
$table = new Bugs();
// retrieve with from part set, important when joining
$select = $table->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
$select->setIntegrityCheck(false)
->where('bug_status = ?', 'NEW')
->join('accounts', 'accounts.account_name = bugs.reported_by')
->where('accounts.account_name = ?', 'Bob');
$rows = $table->fetchAll($select);
Note that disabling the integrity check will also disable some of the automagic of the resulting recordset:
The resulting row or rowset will be
returned as a 'locked' row (meaning
the save(), delete() and any
field-setting methods will throw an
exception).
Load $num with a array of id's from a given group
The following code will do the job
$table = 'users';
$update = array(
'ACLVersion' => new Zend_Db_Expr('ACLVersion + 1')
);
$where = $db->quoteInto('id IN (?)', $num);
$db->update($table, $update, $where);