Sqlite not allow WHERE clause - java

I am developing one app in which i have make sqlite Database.so there are one table with 3 Field
1._id
2.Appname
3.Rating
For this I need to set Data in Last Field(Rating) with condition where _id = 1 like this...
but from Post I found that Sqlite not allow where clauses so how can I do this?
INSERT INTO Packageinformation(appRating)VALUES (3) where _id=1;
can you please help me out this.

You have to use update, not insert.

Perhaps that's what you need:
UPDATE Packageinformation SET appRating = 3 WHERE _id = 1;

INSERT INTO inserts data into the database, a WHERE clause is only used for SELECT, which is reading rows.
INSERT INTO with a WHERE clause is nonsense.
If you want to update a row, use UPDATE (see documentation for details).
You could also use REPLACE INTO, but be careful because it might set all other fields to their default values.

Related

In Jooq, how to make the "delete" statement return the deleted records?

In my project, there are 2 tables, orders and order_payments.
The latter is a join table with 2 columns, order_id and payment_id.
I didn't use FK for efficiency sake.
When I delete some orders from orders table, I need to delete the related records from payments table based on the order_id field.
So, after the orders are deleted using the DELETE statement, I want the order_ids of the deleted orders to be returned, so that I can use them to retrieve the order_payments table and delete related records.
Could someone teach me how to do that?
I tried
private List<OrdersRecord> deleteOrders(OrderQuery orderQuery, DSLContext dsl) {
DeleteQuery<OrdersRecord> deleteQuery = dsl.deleteQuery(ORDERS);
deleteQuery.addConditions(orderQueryConditions(orderQuery));
deleteQuery.setReturning();
deleteQuery.execute();
return deleteQuery.getReturnedRecords();
}
but an empty List was returned.
Where I did wrong?
Thanks in advance!
I don't think this is possible in MySQL 8.0.21 yet:
https://bugs.mysql.com/bug.php?id=83139
If you know a way to do this with native MySQL (whatever version), or emulate it somehow, please document it here: https://github.com/jOOQ/jOOQ/issues/6865
You can use 'on delete cascade'. So you can resolve your problem. And you don't must getting ids from first table. You just must execute delete query from first table and records from second table will deleted too.
For example:
https://www.mysqltutorial.org/mysql-on-delete-cascade/

Huge Update or Inserts into DB2 using JAVA and JPA 1.0

I have a JAVA requirement where i have 1500 records that I have to update or insert into the database.
If a record exists with userId, then update it.
If a record does not exist with userId, then Insert it.
And, if there is an error in lets say, 10th record,,,I need to get
the error code for that record.
It looks like I have 2 options using JPA 1.0
A) Fire a select to check if record exists. If yes, then fire update. If not, fire insert.
B) Fire an insert always,,,but i get an uniqe record exception, only then fire an update query..
Are there any other more efficient ways ? how can this be done with as few queries and as quick as possible ?
ENV- JAVA, JPA 1.0, DB2
You did not specify which version of DB2 you use and on which system. Anyway, check if MERGE statement is available on your DB:
LUW from 9.5.0: http://www.ibm.com/support/knowledgecenter/SSEPGG_9.5.0/com.ibm.db2.luw.sql.ref.doc/doc/r0010873.html
Z/OS from 10.0.0: http://www.ibm.com/support/knowledgecenter/SSEPEK_10.0.0/sqlref/src/tpc/db2z_sql_merge.html
Another way is to do delete + insert on every record (poor performance).
Third option is to create dynamic one delete statement with listed ID/KEY in where clause from data you are going to update, fire delete and then insert all data.
Performance of every option will depend on table specification, indexes etc.
you can write query in mysql as below
//suppose a as pk
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1,b=b+1;
here update will run when record with pk as a=1 is already present
refer below link http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html

Oracle SQL Update nested within Select

Is it possible to nest an Update query within a Select query?
Or is there another method of updating rows given that the first word in the query must be 'Select' and ensuring that it is one query (ie. there is no semicolon between queries).
Furthermore, it must be accepted by JdbcTemplate query (which is why there must be no semicolons).
Thanks for the help.
=====
Update:
I'm trying to inject SQL to update table rows. The table structure doesn't matter but the table must already exist and the rows that it is trying to update must already exist as well.
Example query:
UPDATE users
SET username = 'Mike';
The only requirement is that the first word in the SQL query must be SELECT because that is the only basic sanity check that the server does. Is it possible to do something like this?
=====
Update:
One solution mentioned was to use PL/SQL to call an function that would mutate data. I'm currently looking to see if you can create this function and call it in one or more SELECT statement.

Using INSERT INTO & UPDATE at the same time - JAVA SQL

I have a question.
So I have a add functionality where the user can add cars to the database. How do I do a check whilst adding the car, so that if the car does exist-the data is overwritten, instead of an error messaging like 'Duplicate error' appearing?
So I have...
INSERT INTO Cars VALUES (1, "AUDI R8", 10);
How do I do it so that if a user inputs (1, "BMW X5, 15), it overwrites the current data?
How would I have an INSERT INTO and UPDATE STATEMENT at the same time? Also how do I make use of transactions here?
Many thanks
MySQL has a REPLACE statement for this kind of cases:
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted [...]REPLACE is a MySQL extension to the SQL standard. It either inserts, or deletes and inserts.
Check the reference for the REPLACE statement.
Well, the best solution for this problem are the TRIGGERS.
Triggers allow you to separate the application layer from the the database.
In your case you need to launch an error, so this is a trigger called "passive", that reacts automatically only if the condition is violated.
Take a look a this: https://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html or various related docs.
Maybe is not the best solution, but u can do a SELECT first, and after do an UPDATE or a simply INSERT, depends of the case.

Possible in JDBC / MySQL to retrieve updated rows?

preparedStatement.executeUpdate()
Returns the number of rows updated. To my research so far it's not possible to do an update-query in which you would retrieve the updated rows, but this seems like such a basic feature that I'm clearly missing something. How to accomplish this?
Per first comment on question this is simply not possible in MySQL. PostgreSQL supports UPDATE...RETURNING as this feature.
If you use executeQuery instead of executeUpdate, you get a resultset back.
Then, change your stored procedure to be a function, and return the changed rows in a select at the end of the function. AFAIK, you cannot return data from a procedure in MySQL (as opposed to e.g. Microsoft SQL server).
EDIT: The suggestion struck out above is not possible. The JDBC specification does not allow updates in query statements (see the answer for this one: http://bugs.mysql.com/bug.php?id=692).
BUT, if you know the WHERE clause of the rows you are about to update, you can always select them first, to get the primary keys, perform the update, and then perform a select on them afterwards. Then you get the changed rows.
when you fire preparedStatement.executeUpdate() you already have the row identifiers using which you can uniquely identify the rows you want updated- you need to use the same identifiers to do a query and fetch the updated rows. you can not accomplish update and retrieval in one shot using JDBC apis.

Categories

Resources