SQLite delete a specific row java swing - java

I'm trying to delete a specific row from a table, and I can't find the way to do it.
I was following https://groups.google.com/forum/#!topic/android-developers/rrmbsKyKRCE but it didn't work.
my query is:
String query = "delete from " + type + " where name=? and score =? (SELECT score FROM " + type + " ORDER BY score DESC LIMIT 1)";
pst.setString(1, tempName);
pst.setInt(2, tempScore);
The string "type" is the table name (no bugs with the table name) and in the table I have to rows: name and score. and pst is the prepared statement.
The error I get: "[SQLITE_ERROR] SQL error or missing database (near "(": syntax error)".
I've noticed that the link above is for android, but I couldn't find something for java swing...
What am I doing wrong?

It seems you're looking for an IN clause
String query = "delete from " + type + " where name=? and score IN (SELECT score FROM " + type + " ORDER BY score DESC LIMIT 1)";

Related

org.postgresql.util.PSQLException: ERROR: column "id" does not exist - Java Web Service

I am developing a java web service that is deployed in wildly. It is connected to a postgresql database.
In this database, I have a table called xx_activity. In it there is a column called "id", which is also the primary key.
Here is the query used to create the table:
CREATE TABLE xx_activity
(
id serial NOT NULL,
baseitemid integer
);
to connect to this table, I use the following java code:
conn = postgresVoyateDBConnection();
query = conn.prepareStatement("select id, baseitemid" +
"from xx_activity " +
"where \"id\" = ? ");
query.setInt(1, id);
ResultSet rs = query.executeQuery();
However, when I call the method that includes this code, I get an error:
org.postgresql.util.PSQLException: ERROR: column "id" does not exist
Position: 8
This is confusing because I certainly have this column. i added escape characters as per this answer, but it did not solve the issue.
Also note that queries without the where clause, like:
conn = postgresVoyateDBConnection();
query = conn.prepareStatement("select id, baseitemid " +
"from xx_activity");
ResultSet rs = query.executeQuery();
work perfectly.
I have also tried without using escape characters but it gives the same error. I also checked in pgadmin and there is no trailing space in the column name, neither are there any upper case letters involved (in which case, the other select query shouldn't have worked?).
How can this be fixed?
Fixed this, the issue was a missing space. After the first line of the query, there needs to be a space as belows:
query = conn.prepareStatement("select id, baseitemid " +
"from xx_activity " +
"where \"id\" = ? ");
EDIT: escape charactors not needed for id; so final answer should be:
query = conn.prepareStatement("select id, baseitemid " +
"from xx_activity " +
"where id = ? ");

How to multiply 2 columns in database MySQL using JAVA GUI?

I want to make a sales database that it will insert and insert the data. My id is AUTO INCREMENT.
My Code is Here:
Inserting Data
String sql = "INSERT INTO `mycredentials`.`sales` (name, price,quantity)
VALUES ('"+jFrame_pName.getText()+"', '" + jFrame_pPrice.getText() + "','"+jFrame_quantity.getText()+"')";
ps.executeUpdate(sql);
Updating Data:
String sql2 = "UPDATE `mycredentials`.`sales` SET quantity = '"+jFrame_quantity.getText()+"', total = (quantity*price) WHERE id ='?'";
ps.executeUpdate(sql2);
And Viewing Data:
String sql1 = "SELECT name AS 'Product Name', price AS 'Product Price', quantity as 'Quantity', total as 'Total' FROM `mycredentials`.`sales`";
ResultSet rs = ps.executeQuery(sql1);
table_1.setModel(DbUtils.resultSetToTableModel(rs));
But the displays makes the total NULL. What is my error? Thank you so much.
Have you tried to maybe specify again the quantity like this?
String sql2 = "UPDATE `mycredentials`.`sales` SET quantity = '"
+ jFrame_quantity.getText() + "', total = ("
+ jFrame_quantity.getText() + "*price) WHERE id ='?'";
ps.executeUpdate(sql2);

Pass email as a parameter for SQL query, PostreSQL, JAVA

I'm trying to pass the email as a parameter for the SELECT SQL query in my JAVA back-end.
As i understood, for some reason it pass only "email_name" from the "email_name#email.com". (Getting this error):
Threw a SQLException creating the list of blogs.
ERROR: column "email_name" does not exist
Position: 174
There is an existed rows, which contains "email_name#email.com".
(Why "ERROR: column"? according to query it should look for a value, no?)
Here is My query:
String active_user = "email_name#email.com"; //email_name#email.com - example, active_user receive some path variable and on this particular moment(before query execution) contains exactly "email_name#email.com".
ResultSet rs = st.executeQuery("SELECT \n" +
" goods.item_title, \n" +
" goods.item_descr, \n" +
" goods.item_email,\n" +
" goods.item_images,\n" +
" goods.item_phone, \n" +
" goods.item_price \n" +
"FROM \n" +
" public.goods\n" +
"WHERE goods.owner = "+active_user+"\n" +
"ORDER BY\n" +
" goods.item_id ASC;");
So the question is - how to pass full email to query?
Try using String active_user = "'email_name#email.com'";. with single quotes. Since postgre recognized as column when you use double quotes.
You should use PreparedStatement. this is a example
Very unsafe approach, you should use PreparedStatement to avoid SQL injection. Here is existing answer

Getting the 10 latest entries in SQL

I'm trying to get the the ten latest entries in my sqlite database. I need to fetch only one entry at a time instead of those ten entries at once.
Until now, i was doing this:
"SELECT " + sql + " FROM " + TABLE_NAME
+ " WHERE _id = (SELECT max(_id)-2 FROM " + TABLE_NAME + ")";
For every entry i need, i was just doing ...max(_id)-x but this does not work, if some entries are deleted at some time. The id's have some gaps (like 1, 2, 4, 8, ...).
So after some research i was trying something like this:
"SELECT * FROM (SELECT " + sql + ", rank() over (order by max(_id) desc) rk
FROM " + TABLE_NAME + " GROUP BY " + sql + ") t WHERE rk = 2"
this approach however crashes with the following message
android.database.sqlite.SQLiteException: near "(": syntax error (code 1): , while compiling: select * from (select price, rank() over (order by max(_id) desc) rk from refuels group by price) t where rk = 2
How can I get this done? Thanks in advance
I found a simple and easy solution:
"SELECT " + sql + " FROM " + TABLE_NAME + " ORDER BY _id DESC LIMIT 1 offset n"
where the n is the number of the entry -1. Meaning, I want row number 6, n has to be 5.

sql syntax error MySQLSyntaxErrorException

String sql = "INSERT INTO order " + "(customerid, pant, shirt, date) "
+ "VALUES ('" + jTextField1.getText() + "','" + jTextField2.getText()
+ "','" + jTextField3.getText() + "','" + jTextField4.getText() + "')";
When tried this, I got the following error:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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
'order (customerid, pant, shirt, date) VALUES ('10','2','3','26')' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method).
You need to escape reserved words like order with backticks
INSERT INTO `order` (customerid, ...
Besides that I recommend using Prepared Statements.
Table name "order" is reserve word so please change table name and try it.

Categories