当前位置: 首页 > 知识库问答 >
问题:

Sqlite java和sqlite4java

通博实
2023-03-14

我试图建立一个应用程序作为一个学习经验。我得到了一个空指针异常,但它是在第二次调用代码时发生的。

// Sanity checks
sanity = new SanityChecks();
logger.info("Checking sanity...");
try {
    sanity.doBasicChecks();
    logger.info("It all looks sane.");
}
catch( final Exception ex){
    logger.error("Something is wrong, we are not sane.  Aborting...", ex);
    stop();
}
package nz.co.great_ape.tempusFugit;

import com.almworks.sqlite4java.SQLite;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * Checks to see if all is ok for standard usage of tempusFugit.
 *
 * Checks for valid paths, existing database etc, if not assumes that
 * this is the first time tempusFugit has been run for this user and
 * tries to set up the basic files and data.
 *
 * @author Rob Brown-Bayliss
 *         Created on 2/11/14
 */
public class SanityChecks {


    public static final Logger logger = LoggerFactory.getLogger(AppConsts.LOGGER_NAME);

    public SanityChecks() {
        // todo
        // todo
        SQLite.setLibraryPath(AppConsts.HOME_DIR);   // TODO:  Why is this required?  can we not place the native libs somewhere else or in the jar?
        // todo
        // todo
    }

    /**
     * Performs basic checks to see if it is ok to run the app.  If not it will attempt to
     * set up for first time use.
     *
     * @return true if all is ok
     */
    public boolean doBasicChecks() {
        logger.info("Starting basic checks...");
        if (!HomeDirExists()) {
            return false;
        }
        if (!DatabaseExists()) {
            logger.info("Trying to create a new database.");
            DatabaseUpgrade dug = new DatabaseUpgrade();
            if (!dug.upGrade()) {
                return false;
            }
            logger.info("Created a new database.");
            // At this point a usable database should exist and it should be current
        }
        if (!DatabaseVersionCorrect()) {
//          //  If the database is old we will upgrade it to the current version.
//          DatabaseUpgrade dug = new DatabaseUpgrade();
//          if (!dug.upGrade()) {
//              return false;
//          }
            logger.info("is this it?.");
        }
        //  At this point all basic checks have passed and we are good to go...
        logger.info("Basic checks are complete.  All is good in the universe...");
        return true;
    }

    /**
     * Checks if the app home directory exists, if not it tries to create it.
     *
     * @return true if exists or was able to create the directory
     */
    private boolean HomeDirExists() {
        if (!Files.isDirectory(Paths.get(AppConsts.HOME_DIR))) {
            try {
                Files.createDirectory(Paths.get(AppConsts.HOME_DIR));
            }
            catch (IOException io) {
                logger.error("The directory " + AppConsts.HOME_DIR + " does not exist and could not be created.  This is not the best but we can survive.", io);
                return false;
            }
        }
        logger.info("The directory " + AppConsts.HOME_DIR + " exists.  This is good.");
        return true;
    }

    /**
     * Checks if the SQLite database exists, if not it tries to create it.
     * or was able to create the database
     *
     * @return true if the database exists
     */
    private boolean DatabaseExists() {
        if (Files.notExists(Paths.get(AppConsts.TF_DATABASE))) {
            logger.error("The database " + AppConsts.TF_DATABASE + " does not exist.  This is bad.");
            return false;
        }
        logger.info("The database " + AppConsts.TF_DATABASE + " exists.  This is good.");
        return true;
    }

    /**
     * Checks if the SQLite database is correct for this version.
     *
     * @return true if correct version
     */
    private boolean DatabaseVersionCorrect() {
        Integer expectedVersion = AppConsts.TF_DATABASE_VERSION;
        logger.info("Checking the database version is correct.  Looking for version "+ expectedVersion + "." );
        DatabaseUpgrade dug = new DatabaseUpgrade();
        logger.info("Checking the database version is correct.  Looking for version "+ expectedVersion + "." );
        Integer currentVersion = dug.getVersion();
        if (currentVersion < expectedVersion) {
            logger.info("Expected version " + expectedVersion + ", but database is version " + currentVersion + ".  This is bad.");
            return false;
        }
        logger.info("The database version is correct.  This is good.");
        return true;
    }

}
package nz.co.great_ape.tempusFugit;

import com.almworks.sqlite4java.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * Setup the database for current version tempusFugit.  This will upgrade an
 * existing database or create a new empty database if required.
 *
 * @author Rob Brown-Bayliss
 *         Created on 4/11/14
 */
public class
        DatabaseUpgrade {

    public static final Logger logger = LoggerFactory.getLogger(AppConsts.LOGGER_NAME);

    private SQLiteConnection dbConn = null;
    private SQLiteQueue      sQueue = null;
    private int currentVersion;

    public DatabaseUpgrade() {

    }

    /**
     * Attempts to upgrade the existing database to the current version, or if
     * there is no existing database then create one suitable for the current app version.
     *
     * @return true if there is a current and usable database
     */
    public boolean upGrade() {
        logger.info("    Started an upgrade on the database, if the database does not exist it will be created at the current version, ");
        logger.info("    if it exists but is an older version it will be upgraded to the current version.");
        if (openDatabase()) {
            currentVersion = getVersion();
            if (currentVersion == AppConsts.FAIL) {
                logger.info("Database version is unknown.  The file will be deleted and a new database created.  We can survive this.");
                // TODO:  Ask user if we should delete the old one or make a backup?
                closeDatabase();
                deleteDatabase();
                openDatabase();
            }
            if (currentVersion != AppConsts.TF_DATABASE_VERSION) {
                logger.info("Current Database version is " + currentVersion);
                // TODO:  Backup current database.
                if (currentVersion < 1) {
                    if (!makeVersionOne()) {
                        logger.error("Unable to upgrade the database.  This is VERY bad.");
                        return false;
                    }
                }
                currentVersion = 1;
            }
            closeDatabase();  // good practice
        }
        logger.info("The database is the current version.  This is good.");
        return true;
    }

    /**
     * Turns a blank SQLite database into a tempusFugit version 1 database by
     * adding the required tables and data.
     */
    private boolean makeVersionOne() {
        logger.info("Attempting to upgrade to version 1.");
        String CT_SQL = "CREATE TABLE IF NOT EXISTS dbVersion (id INTEGER PRIMARY KEY AUTOINCREMENT, version INTEGER NOT NULL UNIQUE, ";
        CT_SQL = CT_SQL + "upgraded INTEGER(4) NOT NULL DEFAULT (strftime('%s','now'))); ";
        String IN_SQL = "INSERT INTO dbVersion(version) values (1); ";
        try {
            execSQL("BEGIN TRANSACTION; ");
            execSQL(CT_SQL);  // create the table
            execSQL(IN_SQL);  // insert the record
            execSQL("COMMIT; ");
        }
        catch (Exception ex) {
            logger.error("Attempted upgrade of " + AppConsts.TF_DATABASE + " to version 1 has failed.  This is VERY bad.", ex);
            return false;
        }
        logger.info("The database has been upgraded to version 1.  This is good.");
        return true;
    }

    private Integer execSQL(String SQL) {
        return sQueue.execute(new SQLiteJob<Integer>() {
            protected Integer job(SQLiteConnection con) throws SQLiteException {
                SQLiteStatement st = null;
                try {
                    st = con.prepare(SQL);
                    st.step();
                    return AppConsts.SUCCESS;
                }
                catch (Exception ex) {
                    logger.error("Tried to execute SQL: " + SQL, ex);
                    return AppConsts.FAIL;
                }
                finally {
                    if (st != null) {
                        st.dispose();
                    }
                }
            }
        }).complete();
    }

    /**
     * Gets the current database version
     *
     * @return version as an int
     */
    public int getVersion() {
        return sQueue.execute(new SQLiteJob<Integer>() {
            protected Integer job(SQLiteConnection con) throws SQLiteException {
                SQLiteStatement st = null;
                try {
                    st = con.prepare("SELECT version, upgraded FROM dbVersion ORDER BY upgraded DESC LIMIT 1;");
                    st.step();
                    return st.columnInt(0);
                }
                catch (Exception ex) {
                    logger.error("The database version of " + AppConsts.TF_DATABASE + " is unknown.  This is bad.", ex);
                    return AppConsts.FAIL;
                }
                finally {
                    if (st != null) {
                        st.dispose();
                    }
                }
            }
        }).complete();
    }

    /**
     * Opens an existing SQLite database or creates a new blank database
     * if none exists.
     *
     * @return false if there is a problem.  // TODO: Is it possible to have a problem?
     */
    private boolean openDatabase() {
        try {
            dbConn = new SQLiteConnection(new File(AppConsts.TF_DATABASE));
            dbConn.open(true);
            sQueue = new SQLiteQueue(new File(AppConsts.TF_DATABASE));
            sQueue.start();
            return true;
        }
        catch (Exception ex) {
            logger.error("The database " + AppConsts.TF_DATABASE + " could not be opened or created.  This is VERY bad.", ex);
            return false;
        }
    }

    /**
     * Closes an open database.
     *
     * @return false if there is a problem.  // TODO: Is it possible to have a problem?
     */
    private boolean closeDatabase() {
        try {
            if (dbConn != null) {
                dbConn.dispose();
            }
            return true;
        }
        catch (Exception ex) {
            logger.error("The database " + AppConsts.TF_DATABASE + " could not be closed.", ex);
            return false;
        }
    }

    /**
     * Deletes an existing database.
     *
     * @return false if there is a problem.  // TODO: Is it possible to have a problem?
     */
    private boolean deleteDatabase() {
        try {
            Files.delete(Paths.get(AppConsts.TF_DATABASE));
            logger.info("The database " + AppConsts.TF_DATABASE + " has been deleted.");
            return true;
        }
        catch (Exception ex) {
            logger.error("The database " + AppConsts.TF_DATABASE + " could not be deleted.", ex);
            return false;
        }
    }
}


Plus the constants:

package nz.co.great_ape.tempusFugit;

/**
 * Constants used by tempusFugit
 *
 * @author Rob Brown-Bayliss
 *         Created on 31/10/14
 */
public class AppConsts {

    // Application
    public static final String APP_NAME       = "Tempus Fugit";
    public static final String VERSION_NUMBER = "0.0.1";

    // Debug Mode On-Off
    public static final boolean DEBUG_MODE = true;

    // Data files and paths
    public static final String FS        = System.getProperty("file.separator");
    public static final String HOME_DIR  = System.getProperty("user.home") + FS + ".tempusFugit";  // This is the tempusFugit home, not the users home.
    public static final String USER_NAME = System.getProperty("user.name");

    public static final String LOGGER_NAME = "nz.co.great_ape.tempusFugit";
    public static final String LOG_FILE    = HOME_DIR + FS + "tempusFugit.log";

    // Database
    public static final String TF_DATABASE = HOME_DIR + FS + "tfData.sql";
    public static final int TF_DATABASE_VERSION = 1;  //  This is the current version of the database

    // Error codes
    public static final int UNKNOWN = -1;
    public static final int FAIL    = 0;
    public static final int SUCCESS = 1;

}

这里有人能帮忙吗?

这是堆栈跟踪

/usr/lib/jvm/java-8-oracle/bin/java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:49764,suspend=y,server=n -javaagent:/home/rob/Projects/IntelliJ/plugins/Groovy/lib/agent/gragent.jar -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-8-oracle/jre/lib/jsse.jar:/usr/lib/jvm/java-8-oracle/jre/lib/management-agent.jar:/usr/lib/jvm/java-8-oracle/jre/lib/deploy.jar:/usr/lib/jvm/java-8-oracle/jre/lib/javaws.jar:/usr/lib/jvm/java-8-oracle/jre/lib/plugin.jar:/usr/lib/jvm/java-8-oracle/jre/lib/resources.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfr.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfxswt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jce.jar:/usr/lib/jvm/java-8-oracle/jre/lib/charsets.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jfxrt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/cldrdata.jar:/home/rob/Projects/tempusFugit/build/classes/main:/home/rob/Projects/tempusFugit/build/resources/main:/home/rob/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-api/1.7.7/2b8019b6249bb05d81d3a3094e468753e2b21311/slf4j-api-1.7.7.jar:/home/rob/.gradle/caches/modules-2/files-2.1/com.almworks.sqlite4java/sqlite4java/1.0.392/d6234e08ff4e1607ff5321da2579571f05ff778d/sqlite4java-1.0.392.jar:/home/rob/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.1.2/b316e9737eea25e9ddd6d88eaeee76878045c6b2/logback-classic-1.1.2.jar:/home/rob/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-core/1.1.2/2d23694879c2c12f125dac5076bdfd5d771cc4cb/logback-core-1.1.2.jar:/home/rob/Projects/IntelliJ/lib/idea_rt.jar nz.co.great_ape.tempusFugit.MainApp

共有1个答案

阎功
2023-03-14

问题是你打电话给

DatabaseUpgrade dug = new DatabaseUpgrade();
logger.info(...);
Integer currentVersion = dug.getVersion(); 

但是dabaseupgrade中的dbconnsqueue仍然为空。因为您没有调用私有的opendatabase()方法,该方法初始化您的varaibles。因此,当您调用getversion()时,您的squee.execute(...)会出现故障,因为您无法调用空对象上的方法。

 类似资料:
  • 问题内容: 关于它们有很多传说。我想知道真相。以下两个示例之间有什么区别? 问题答案: 不确定从何处获得传说,但: 提交按钮 与: IE6将在标记之间提交此按钮的所有文本,其他浏览器将仅提交值。使用可使您在按钮的设计上享有更大的布局自由度。从各种意图和目的看,它乍一看似乎很棒,但是各种浏览器怪癖使它有时很难使用。 在您的示例中,IE6将发送到服务器,而其他大多数浏览器将不发送任何内容。要使其跨浏览

  • 什么区别以及如何正确重写代码?

  • 我试图理解为什么下面两个代码块会产生不同的结果。 代码块1按预期工作,并返回从数据库中查找的提供程序的数组。另一方面,代码块2返回函数数组。在理解promissione.all()和async/await时,我觉得缺少了一些简单的东西。 代码块的差异如下: > 块1:创建许诺函数数组,然后使用map运算符将其包装在异步函数中。 块2:许诺函数的数组被创建为异步函数。因此,不调用map运算符。 如果

  • 问题内容: 我才刚刚开始研究SQL。 我有一个SQL Server 2008r2数据库,它将返回两个字段DocDate和InvValue。我需要将InvValues汇总为今天的MTD和YTD,所以看起来像 我已经做了大量的Google搜寻,并且可以使用SUM&DATEPART进行一项或多项,但是我坚持尝试两者兼而有之。 有人可以给我一些伪代码,以帮助我进一步谷歌。 谢谢@戈登·利诺夫(Gordon

  • 这个示例代码来自一本Java书籍,从墙上的99瓶啤酒到没有啤酒打印出这首歌。问题是,当墙上是1瓶啤酒时,它仍然写着瓶子。我试图通过在末尾添加部分来解决这个问题。但是,它仍然显示1瓶啤酒在墙上,我瓶啤酒在墙上。 我不知道该改变什么来解决这个问题。是否创建另一个while部分? 如果你能给他们一个提示,这样我就可以自己解决它,那也很酷!因为我知道我的实际歌曲输出是在第一个if部分,但我不知道我应该在哪

  • 原文地址:https://cesiumjs.org/tutorials/cesium-and-webpack/ Cesium 和 Webpack Webpack是非常强大非常流行的JavaScript 模块打包工具。它可以让开发人员以一种简单直观的 require 方式去加载各种页面需要的文件,极大的方便了开源人员对代码和资源文件进行结构化设计。当编译的时候,它会跟踪代码依赖性,把所有的模型打包到