merge 2 database with same structure

i have a server that backup the db and send me every time the db... btw bc i don't want every time send 50mb of db i did a small program with something like

delete from table where timestamp < ?

get the last timestamp of the db and save it for the next backup in that way i can send only the newest data (few kb instead 50mb)

now i want make another program for merge the different backups ... i'm "stucked" on the insert part...

    public static void merge(String toMerge) {
Connection c, c2 = null;
Statement stmt, stmt2 = null;

try {
    Class.forName("org.sqlite.JDBC");
    c = DriverManager.getConnection("jdbc:sqlite:merge.db");
    System.out.println("Opened merge.db database successfully");
    c.setAutoCommit(false);
    stmt = c.createStatement();
    c2 = DriverManager.getConnection("jdbc:sqlite:" + toMerge);
    System.out.println("Opened " + toMerge + " database successfully");
    stmt2 = c2.createStatement();

    ResultSet rs = stmt2.executeQuery("SELECT * FROM messages;");
    String sql;
    while (rs.next()) {
    sql = "INSERT INTO messages";
    stmt.executeUpdate(sql);
    }
    rs.close();
    stmt2.close();
    c2.close();

    stmt.close();
    c.commit();
    c.close();
} catch (Exception e) {
    System.err.println(e.getClass().getName() + ": " + e.getMessage());
    System.exit(0);
}
System.out.println("Operation done successfully");

}

i was thinking on open 2 db... 1 have a standard name "merge.db" (in that way i can merge 2+ db) and the other is the one to merge... for use this method i open 2 connection (1 for each db) and read every line from db2 and insert in the new db... but here i have a few problem

  1. the db have lots of cols so i need to use 200 getInt/String/Float
  2. i have NULL value
  3. i have a lot of rows

so if i use this method it require a lot of vars and time for make the insert query... (for now i'm writing this method but if there is a simpler way for do it is better... i was thinking make a class with all the vars and use it for create the sql insert.. in that way i can use the same function for different tables (just need to extend the class for other class))

edit in the table i have blob real int string values (29 cols in total)

Answers


If you had two tables in the same database, you could use a statement like this:

INSERT INTO messages1 SELECT * FROM messages2

When you have two different database files, you can ATTACH one to the other:

ATTACH DATABASE '...' AS toMerge

and then access it by prefixing the table name with the database name:

INSERT INTO messages SELECT * FROM toMerge.messages

Need Your Help

Using LC_ENCRYPTION_INFO for basic crack detection on 64bit systems?

ios security dyld

I was using LC_ENCRYPTION_INFO to check if binary is encrypted/compressed or not. I was using this to guess the possibility of the app being a pirated copy.