dimanche 1 mars 2015

Merging SQLite databases in Java throws "unique constraint failed"

I am trying to merge two SQLite databases with Java. The database scheme is generated as follows:



public static void createDatabaseTables(Connection c)
{
String sql;

// creates the table HASHES
sql = "CREATE TABLE HASHES("
+ "INPUTVALUE TEXT PRIMARY KEY,"
+ "HASHVALUE TEXT);";
executeUpdate(c, sql);
System.out.println("Table HASHES created successfully");
}


I am using the following code to merge two databases (the first one is empty and the second one contains some values (inputvalues and appropriate pearson hashes)).



public static void mergeDatabases(String path1, String path2)
{
// open a database connection
Connection c = openDatabaseConnection(path1);

// end the actual transaction (must be done to attach a new database)
executeUpdate(c,"end transaction");

// attach the second database to the first one
String sql = "ATTACH DATABASE '" + path2 + "' AS toMerge";
executeUpdate(c, sql);

// copy the calculated hashes from the second database to the first one
sql = "INSERT INTO HASHES SELECT * FROM toMerge.HASHES";
executeUpdate(c, sql);

// begin a transaction
executeUpdate(c, "begin transaction");

// close the connection
closeDatabaseConnection(c);

System.out.println("Databases merged.");
}


This leads me to the following exception for every row (because if I add the "OR IGNORE" clause at the INSERT, every row is ignored)



java.sql.SQLException: UNIQUE constraint failed: HASHES.INPUTVALUE


I am sure that the values of the column INPUTVALUE are unique (because they are generated with each ascii symbol in a loop). Nevertheless I get the exception.


What am I doing wrong?


Aucun commentaire:

Enregistrer un commentaire