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.
Related
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
I am trying to sense all the rows of some sql server 2008 tables if any changes occur using java technology.
I have investigated some approaches like use of timestamps column , Change Tracking mechanism , Change Data Capture.
But all the above approaches need some customization in database as follows:
1.Timestamps column should present in each table.
2.Change tracking require primary key in each table.
3.Change Data Capture require some creation of system tables and other customizations.
I need some approach which do not require such heavy customizations in the database because database is crucial and does not allow to alter the config settings.
Can anyone help or suggest something in this regard?
The below changes can accomplish data audit
Create an identity column for all txn tables.
Fetch this identity data to the front end along with the transaction data.
Create history tables for all txn tables and move the original data prior to every transaction using a version ID.
After modification in the UI, pass the data back to database and compare the data with the existing information using SQL MERGE statement to perform the update/Insert/Delete
Compare the latest version available in the history table with the data in the current table using the following logic
New data Inserted - IF an Identity key exist in current table and NOT available in the latest version data of history table
WHERE C.IdentityColumn NOT IN (Select identitycolumn from History H)
Data deleted - IF an identity key exist in the latest version data of history tables AND NOT exist in the current table.
WHERE H.IdentityColumn NOT IN (Select identitycolumn from Current C)
Data updated - IF identity key exist in both current table and latest version of history table and any one column data is modified
WHERE (C.IdentityColumn = H.IdentityColumn)
AND
(
C.Col1 <> H.Col1
OR
C.Col2 <> H.Col2
OR
C.ColN <> H.ColN
)
C - Current table
H - History table
using the above logic the modified data can be tracked in a separate audit table which can have columns like Record ID, Field Name, Old Value, New Value, Modification, Modified By, Modified Date/Time
i would like to insert into a local table (in a local database), all the rows from a distant table. here's what i'm looking for :
insert into LocalTable (Column1,Column2,...,ColumnN) values (select * from DistantTable);
does anybody knows how could i do this (if there is a way)??
i'm aweare that there is a way using a java program, by copying the DistantTable rows in a file, then extracting those rows using a StringTokenizer then putting them to LocalTable. but it would be really good if i can perform this using only SQL queries.
You can create a database link in the local database, pointing at the remote database, and then type:
INSERT INTO LocalTable SELECT * FROM RemoteTable#DBLink;
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.