TransWikia.com

Unable to ATTACH a second DB with SQLite Database and JDBC

Stack Overflow Asked by Tacitus86 on December 7, 2020

I have tried every imaginable permutation of this and for some reason I cannot get a second database to connect via SQLite JDBC. Any idea why it’s not working?

I know the main database connects but the attach fails because after this when I try to issue a select query on the second database I get "SQL error or missing database (no such table: dest.AnalogInput$$d)". Yes, I’m sure the path is correct as well as the table name in the second DB.

I don’t get any exception back from the attach. I get false back from the execute() but from the API, that’s normal if there are no results. It’s just the query when I try to use the attached database, it fails saying it can’t find the table. Is there even a way through jdbc to verify that a db is attached or not explicitly?

enter image description here

    startDB(sourceDb);
    getDatabaseMetaData();

    try
    {
        LOG.info("Attach query = " + SQLiteInterface.getInstance().attach(destinationDb, "dest"));
        boolean result = sendCommand(cpds.getConnection(), SQLiteInterface.getInstance().attach(destinationDb, "dest"));
        LOG.info("Attach result = " + result);
    }
    catch (SQLException e1)
    {
        LOG.error("Failed to send attach query for merging databases: " + SQLiteInterface.getInstance().attach(destinationDb, "dest"));
    }

public String attach(String database, String alias)
{
    String query = "ATTACH DATABASE '" + database + "' as " + alias + ";";
    return query;
}

public void startDB(String dbPath)
    {
        cpds = new ComboPooledDataSource();
        cpds.setJdbcUrl("jdbc:sqlite:" + dbPath);
        try
        {
            cpds.setDriverClass("sqlite-jdbc-3.20.1.jar");
        }
        catch (PropertyVetoException e)
        {
            LOG.error("Error while setting driver for sqlite", e);
        }
        cpds.setMinPoolSize(1);
        cpds.setAcquireIncrement(5);
        cpds.setMaxPoolSize(20);

    }

public boolean sendCommand(Connection conn, String query)
{
    // Good practice to create a new statement and result set instead of reusing
    Statement stmt = null;
    boolean results = false;

    try
    {
        // Make sure the query request isn't empty, if it is, there is no point in sending it to the DB
        if (!query.isEmpty())
        {
            // Initialize the new statement
            stmt = conn.createStatement();
            results = stmt.execute(query);
        }
    }
    catch (Exception e)
    {
        LOG.error("Failed to issue database command: ", e);
    }
    finally
    {
        if (stmt != null)
        {
            try
            {
                stmt.close();
            }
            catch (SQLException e)
            {
                LOG.error("Failed to close JDBC statement.");
            }
        }
    }
    return results;
}

The output I keep getting is:

SourceDb = C:UsersTacitusDesktopdatabasearchivedb2Full.mergedb
Attach query = ATTACH DATABASE 'C:UsersTacitusDesktopdatabasearchiveHMI.FB20.dat_0_2' as dest;
Attach result = false
(I don't show the code in this question for the output below because I don't think it is relevant - If necessary I can add it)
Query = INSERT OR IGNORE into dest.AnalogInput$$d select * from AnalogInput$$d where 
SampleInfo_source_timestamp >= 1600824664131000000 AND SampleInfo_soruce_timestamp <= 
1600844478825000000;
[SQLITE_ERROR] SQL error or missing database (no such table: dest.AnalogInput$$d)

One Answer

Okay for anyone that runs into this: The issue was that I was calling a new connection throughout whenever I needed a connection instead of maintaining a single connection. This was the reason the attach was being lost.

Answered by Tacitus86 on December 7, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP