【问题标题】:Unable to ATTACH a second DB with SQLite Database and JDBC无法使用 SQLite 数据库和 JDBC 附加第二个数据库
【发布时间】:2021-01-10 03:19:58
【问题描述】:

我已经尝试了所有可以想象的排列方式,但由于某种原因,我无法通过 SQLite JDBC 连接第二个数据库。知道为什么它不起作用吗?

我知道主数据库已连接,但附加失败,因为在此之后,当我尝试在第二个数据库上发出选择查询时,我得到“SQL 错误或缺少数据库(没有这样的表:dest.AnalogInput$$d)”。是的,我确定路径正确,第二个数据库中的表名也正确。

我没有从附件中得到任何异常。我从 execute() 得到错误信息,但从 API 得到错误信息,如果没有结果,这是正常的。这只是我尝试使用附加数据库时的查询,它失败说它找不到表。甚至有没有办法通过 jdbc 来验证数据库是否已明确附加?

    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;
}

我不断得到的输出是:

SourceDb = C:\Users\Tacitus\Desktop\database\archive\db2Full.mergedb
Attach query = ATTACH DATABASE 'C:\Users\Tacitus\Desktop\database\archive\HMI.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)

【问题讨论】:

  • 没有。我在另一个程序中制作数据库,并总是将它们命名为点合并。 (抱歉在手机上,不让我输入)。
  • 一个有用的注释是我只是试图合并具有相同架构的 2 个数据库。

标签: java sqlite jdbc


【解决方案1】:

对于遇到此问题的任何人都可以:问题是,每当我需要连接而不是维护单个连接时,我一直在调用一个新连接。这就是附件丢失的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    相关资源
    最近更新 更多