简体   繁体   中英

Copy from Oracle to SQLite in Java

I was asked to implement a code to copy some info from a Oracle DB to an SQLite DB. I got the ResultSet from Oracle and start inserting in SQLite, but it seems to me that is really slow. Is there a better way or can you suggest a better way to achieve this?

My code looks like follows:

    OracleDB oracleDB = new OracleDB();
    SQLiteDB sqliteDB = new SQLiteDB();


    oracleConnection = oracleDB.connect();
    sqliteConnection = sqliteDB.connect();
    {
        Statement s = null;
        PreparedStatement ps = null;
        try {
            s = sqliteConnection.createStatement();
            s.executeUpdate("CREATE TABLE IF NOT EXISTS test_table (field1, field2)");

            ps = oracleConnection.prepareStatement("SELECT * FROM temp_desarrollo");
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                ps = sqliteConnection.prepareStatement("INSERT INTO test_table VALUES (?, ?)");
                //SET PARAMETERS
                ps.setString(1, rs.getString(1));
                ps.setString(2, rs.getString(2));
                ps.executeUpdate();
            }

        } catch (SQLException ex) {
            Logger.getLogger(OracleToSQLite.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    oracleDB.disconnect(oracleConnection);
    sqliteDB.disconnect(sqliteConnection);

I think what you've got is probably OK for a "one off", or occasional table copies.

You might also want to consider creating a linked table, and doing a "select into":

https://forums.oracle.com/forums/thread.jspa?threadID=846516

PS: As Vincent Malgrat suggested - move the "prepare" outside the loop, too :)

通常,批处理在插入数据库(PreparedStatement.addBatch())时可提高性能。

Have a look here http://www.zentus.com/sqlitejdbc/usage.html

I have never used this, but when inserting many rows into Oracle, the first things I would do are:

  1. Set autocommit = false
  2. Use array / batch inserts

So I googled for sqlite batch insert and the page above turned up indicating it is possible. I would be interested to hear if it makes things faster for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM