I need to delete all the rows in an Apache Derby database in Java.
If I execute statement.executeUpdate("DELETE FROM TABLENAME WHERE CONDITION=") it works, but if I try statement.executeUpdate("DELETE FROM TABLENAME") it does not delete all the rows. I tried TRUNCATE and it does not work either.
Any ideas.
Use dummy condition to delete all the records :
DELETE FROM TABLENAME WHERE 1=1;
Related
I have a mariadb table with 2 cols: rowid int pk autogenerated and imagen blob.
By using "CallableStatement sentence = mariaConn.prepareCall(myinsert);" I'm able to add a new row with a blob into "imagen" BUT I can't get the autogenerated pk col "rowid".
By the other hand, using "Statement sentence = mariaConn.prepareStatement(myinsert);" I can get the autogenerated col "rowid" but I can't add a blob into "imagen" (only do if it is empty).
Is there a way to do both things at one call? (trying to avoid a Statement insert to get the pk and then a CallableStatement to update the blob).
Note: in Oracle is pretty simple using CallableStatement because Oracle's insert has a "returning" clause <= I'm trying to emulate it on mariadb.
Thanks in advance.
you do not need CallableStatement to insert blob, a simple prepared statement
insert into table(imagen) values(?)
works,and with that you can get autogenerated value if you use Statement.RETURN_GENERATED_KEYS during preparation, and Statement.getGeneratedKeys() after execution. You also can do
select last_insert_id()
any time, but this is less efficient.
There is no MariaDB 5.6 btw.
I want to update two sql tables at once in java. I'm using SQLiteManager. Could someone please suggest a way of doing that?
Assuming you have the driver and connection to the database, you should be able to run most sql commands from java. There is not a single sql statement that will update two tables at once but you can update each table in turn which will have the same effect.
See http://www.javaworkspace.com/connectdatabase/connectSQLite.do for some examples.
For a table update, use
statement.execute(sql);
where sql is a string of the form
sql = "UPDATE myTable SET myColumn = newValue WHERE someOtherColumn=value";
I was trying to delete a column in a SQLite database table using JDBC. As there is no such function in SQLite, to achieve this, I used this approach:
Create a new table (t1_backup) with all columns but not the one to be deleted,
Copy all data (except that of the column to be deleted) to the new table,
Drop the old table (t1),
Rename the new table (t1_backup --> t1).
The actual code is as follows:
Connection cn = DriverManager.getConnection("jdbc:sqlite:"+path);
Statement stmt = new Statement();
stmt = cn.createConnection();
stmt.execute("CREATE TABLE t1_backup(id);");
stmt.executeUpdate("INSERT INTO t1_backup SELECT id FROM t1;");
stmt.execute("DROP TABLE t1;");
stmt.execute("ALTER TABLE t1_backup RENAME TO t1;");
The first two statements were executed without a problem. But when the DROP TABLE statement was executed, it gave me java.sql.SQLException: [SQLITE_LOCKED] A table in the database is locked (database table is locked).
I have found a solution but I was not sure why it worked. The solution is to create three more Statement objects, connect them to the same database and execute each of the four SQL statements with a different Statement object:
stmt.execute("CREATE ...");
stmt2.executeUpdate("INSERT ...");
stmt3.execute("DROP ...");
stmt4.execute("ALTER ...");
Can this be explained?
The JDBC I am using is the one by Xerial.
Thank you.
I have a table with unique constraint on some field. I need to insert a large number of records in this table. To make it faster I'm using batch update with JDBC (driver version is 8.3-603).
Is there a way to do the following:
every batch execute I need to write into the table all the records from the batch that don't violate the unique index;
every batch execute I need to receive the records from the batch that were not inserted into DB, so I could save "wrong" records
?
The most efficient way of doing this would be something like this:
create a staging table with the same structure as the target table but without the unique constraint
batch insert all rows into that staging table. The most efficient way is to use copy or use the CopyManager (although I don't know if that is already supported in your ancient driver version.
Once that is done you copy the valid rows into the target table:
insert into target_table(id, col_1, col_2)
select id, col_1, col_2
from staging_table
where not exists (select *
from target_table
where target_table.id = staging_table.id);
Note that the above is not concurrency safe! If other processes do the same thing you might still get unique key violations. To prevent that you need to lock the target table.
If you want to remove the copied rows, you could do that using a writeable CTE:
with inserted as (
insert into target_table(id, col_1, col_2)
select id, col_1, col_2
from staging_table
where not exists (select *
from target_table
where target_table.id = staging_table.id)
returning staging_table.id;
)
delete from staging_table
where id in (select id from inserted);
A (non-unique) index on the staging_table.id should help for the performance.
Hi I am trying to insert values from excel sheet into SQL Database in java. SQL database has already some rows inserted by some other techniques. Now I need to insert new rows from excel sheet and should eliminate the duplicate values which are existed in the database as well as in the excel sheet. For that I write a query like this.
First I inserted the records from excelsheet into SQL database by using insert query
Statement.executeUpdate(("INSERT INTO dbo.Company(CName,DateTimeCreated) values
('"+Cname”' ,'"+ts+"');
Later I deleted the duplicate values using delete query.
String comprows="delete from dbo.Company where Id not in"
+ "(select min(Id) from dbo.Company "
+ "group by CName having count(*)>=1)";
statement3.executeUpdate(comprows);
where Id is autoincremented integer.
but it is not good to do insert and then delete.
How do I know the values are already exist? If it is exist how do I remove during insertion???
You can simply fire a SELECT for the CName first. If a record is found, update else insert a new record.
Edited to add code snippet:
ResultSet rs = Statement.query("SELECT Id from dbo.Company where CNAME = '" +Cname + "'");
if(rs.next()) {
// retrieve ID from rs
// fire an update for this ID
} else {
// insert a new record.
}
Alternatively, if you think that there are already duplicates on your table and you want to remove them as well..
ResultSet rs = Statement.query("SELECT Id from dbo.Company where CNAME = '"+Cname + "'");
List idList = new ArrayList();
while(rs.next()) {
// collect IDs from rs in a collection say idList
}
if(!isList.isempty()) {
//convert the list to a comma seperated string say idsStr
Statement.executeUpdate("DELETE FROM dbo.Company where id in ("+ idsStr + ")");
}
// insert a new record.
Statement.executeUpdate(("INSERT INTO dbo.Company(CName,DateTimeCreated) values('"+Cname”' ,'"+ts+"');
Of course good practice is to use PreparedStatement as it would improve performance.
PS: Excuse me for any syntax errors.
One option would be to create a temp table and dump your Excel data there. Then you can write an insert that joins the temp table with the dbo.Company table and only insert the records that aren't already there.
You could do a lookup on each record you want to insert but if you are dealing with large volumes that's not a super efficient way to do it since you will have to do a select and an insert for each record in you excel spreadsheet.
Merge statements are pretty effective in these types of situations as well. I don't think all databases support them (I know Oracle does for sure). A merge statement is basically a combo insert and update so you can do the look up to the final table and insert if not found and update if found. The nice thing about this is you get the efficiency of doing all of this as a set rather than one record at a time.
If you can control the DB schema, you might consider putting a unique contraint for whatever column(s) to avoid duplicating. When you do your inserts, it'll throw when it tries to add the dup data. Catch it before it tosses you all the way out.
It's usually good to enforce constraints like this on the DB itself; that means no one querying the database has to worry about invalid duplicates. Also, optimistically trying the insert first (without doing a separate select first) might be faster.