I wanted to copy a ".db" file from an SQLite database to memory using JAVA code. How can I do this ?
What you could do is :-
Open an in-memory database and attach the database you want to copy.
Create the tables and copy the data for each table (you can ascertain the table and other entity names from sqlite_master (name, type and table columns))
You could use CREATE TABLE x AS SELECT * FROM attached_database_identifier.x
noting limitations as per [SQL As Understood By SQLite - CREATE TABLE - CREATE TABLE ... AS SELECT Statements](https://www.sqlite.org/lang_createtable.html#rowid))
the above is for the table name x
Alternately you could create the tables based upon the SQL that is stored in sqlite_master in the sql column.
Create the other entities Indexes, Views and Triggers (again the sql column of sqlite_master will hold the SQL to create the entities).
Indexes would then be built accordingly.
Triggers would then work (you wouldn't want to create them before the data has been copied from the original tables as the triggers would have done their work).
Views extract the resultant data from the tables when used.
detach the attached database.
You now have the in-memory copy.
I want to update a column of a table. But the required Data is available in another oracle DB.Which is the best way to copy data from remote DB to my DB.
Should I go with Java program or can I achieve it in PL/SQL itself?
If I correctly understood the question, you need to set the value of a column with a value extracted from another table of another DB. The two DBs have different structure.
In this case you can do it with just SQL and a database link.
Here's how to create an Oracle database link: Oracle documentation for database links
Then you can write a query like following:
UPDATE local_table
SET local_column = (SELECT remote_column FROM remote_table#remote_db WHERE ...)
WHERE ...
How to insert data in different tables using MySql database?
like i want to insert cid=1,cname=sahil,bid=12345,amount=12000
customer
cid,cname
bankb
bid,cid,amount
MySQL Does not support multiple table data insert using single query,
however Oracle does it.
for MySQL you have to use multiple insert query
like
INSERT INTO NAMES VALUES(...)
INSERT INTO PHONES VALUES(...)
Source : sql - insert into multiple tables in one query
Its not efficient with simple query and so use Stored procedures. Refer the below links.
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/77ce4b34-581b-47c8-aad6-96910ecd8ab5/correct-way-to-insert-data-into-multiple-tables-stored-procedure?forum=sqlgetstarted
This is a common scenario, but i wanted to find out which way is the performance optimized way and best practice.
I have a table with 4 columns: id, name, and two other fields. Id is the PK and name is a unique key. I'm reading data from excel file, populate the values of each row in a Domain object and then saving it. When saving, i want to see whether a record already exists for the same name and if exists, i want to update it. Else save it as a new record.
I can do it with normal select query for the name and check for null, and based on that insert or update but i have thousands of rows to be read from excel files and a non-functional requirement requested is the performance.
So please advice me on which is the best way to handle this senario? i haven't started coding my persistence layer part yet, so i can switch to an ORM or plain jdbc according to your suggestion.
Edited:
If i use name as primary key, then i think i can use saveOrUpdate or merge from an ORM, to fullfill my need. Is it a good idea???
Thanks & regards,
Prasath.
I think the fastest way would be to carry out all the insert/updates in the database itself rather than connecting to it and using a large number of statements.
Note, this is Oracle specific, but other databases may have similar concepts.
I would use the following approach: First save the Excel data as a CSV file on the database server (/mydatadir/mydata.csv), then in Oracle I would be using an external table:
create or replace directory data_dir as '/mydatadir/';
create table external_table (
id number(18),
name varchar2(30),
otherfield1 varchar2(40),
otherfield2 varchar2(40))
organization external (
type oracle_loader
default directory data_dir
access parameters
( fields terminated by ',' )
location ('mydata.csv')
)
(Note, the external table wouldn't have to be set up every time)
Then you can use the following command to merge the data into your table:
merge into yourtable t
using external_table e
on t.name = e.name
when matched then
update set t.id = e.id,
t.otherfield1 = e.otherfield1,
t.otherfield2 = t.otherfield2
when not matched then
insert (t.id, t.name, t.otherfield1, t.otherfield2)
values (e.id, e.name, e.otherfield1, e.otherfield2)
This will upsert the rows in yourtable in one Oracle command, so all the work will be carried out by the database.
EDIT:
This merge command can be issued over plain JDBC (though I prefer using Spring's SimpleJdbcTemplate)
EDIT2:
In MySQL you can use the following construct to perform the merge:
insert into yourtable (id, name, otherfield1, otherfield2)
values (?, ?, ?, ?),
(?, ?, ?, ?),
(?, ?, ?, ?) --repeat for each row in the Excel sheet...
on duplicate Key update
set otherfield1 = values(otherfield1),
otherfield2 = values(otherfield2)
This can be issued as a plain JDBC statement and is going to be better than a separate update and insert, and you can call these in batches of (say) a hundred rows from the spreadsheet. This would mean 1 JDBC call for every 100 rows in your Excel sheet and should perform well. That'll allow you to do it without external tables (you'd need a UNIQUE index on the name column for this to work, I wouldn't change the primary key as this could cause you problems with foreign keys if you needed to change somebody's name).
MySQL also has the concept of external tables, which I think would be faster still than inserting the data as batches as per above. As long as the csv file is uploaded to the correct location, the import should work quickly.
May be it's reasonable to read all names in a Set and subtract the use combinations with Set of names read from the excel file.
Set dbSet=//fill it from SQl query;
Set newSet//fill it from the file;
newSet.removeAll(dbSet); //left non existing ones to be inserted.
originalNewSet (could be clone of initial)
originalNewSet.removeAll(insertingSet); //left records to be updated.
In my project are two matrices using Microsoft Access database, sourced from two different tables (for example TABLE_A and TABLE_B). I need to retrieve a row and a column from that database.
Selecting COL1 from one table means selecting a corresponding row from the other.
I would like to retrieve COL1 and the corresponding row at the same time.
How is this possible using SQL and a JDBC call?
Thank you!
In order to work with two separate tables of the same database you will need one Connection and a Statement and a ResultSet for each database table.
Since you do not clarify how you have stored your matrix data in the database i can not give you more details here. But, whatever you do, in order to access the contents of the first matrix from the database table you will have to replicate for the second. Just change the table name in your SQL select.
However it will not happen "at the same time", one query will follow the other.