如何使用Java检查SQLite数据库文件是否存在?

问题描述 投票:0回答:7

我从用户那里获取数据库文件的名称,我想检查该文件是否已经存在。如果它确实存在,我想向用户显示错误消息,但我不知道如何检查该文件是否存在。

public static void databaseConnect(String dbName) throws Exception
{
  if (/*same name exists*/) // How do I check this?
  {
    System.out.print("This database name already exists");
  }
  else
  {
    Class.forName("SQLite.JDBCDriver").newInstance();           
    conn = DriverManager.getConnection("jdbc:sqlite:/"+ dbName);
    stat = conn.createStatement(); 
  } 
}
java sqlite database-connection
7个回答
14
投票
public static void databaseConnect(String dbName) throws Exception {

   File file = new File (dbName);

  if(file.exists()) //here's how to check
     {
         System.out.print("This database name already exists");
     }
     else{

           Class.forName("SQLite.JDBCDriver").newInstance();            
           conn = DriverManager.getConnection("jdbc:sqlite:/"+ dbName);
           stat = conn.createStatement(); 

     }

3
投票

假设您的

dbName
参数指示 SQLite 文件的路径(尽管有“-wal”和“-shm”配套文件),您可以使用 Java
java.io.File
类和
exists()
谓词
:

final File f = new File(dbName);
if (f.exists())
{
  if (f.isDirectory())
  {
    // Warn about the designated name being a directory.
  }
  else
  {
    // Warn about the designated name already existing as a file.
  }
}

其他检查也可能是必要的,例如进程是否有权创建文件,尽管最终 SQLite 会做得更好确保满足其所有要求


1
投票

我发现最好的方法是检查文件的大小。如果执行:

return new File(DB_NAME).exists()
应等于 True。

你应该得到一个真正的背部,因为它会创造它。相反,请检查以确保文件大小大于 0。至少在这种情况下,您知道文件中有数据,而无需附加和查询结果。

改为:

return new File(DB_NAME).length() > 0


1
投票

你可以做这样的事情,我猜你认为当你执行“new File(name)”时,你正在目录中创建一个新文件,对于我红色的内容,但事实并非如此。

    public static void databaseConnect(String dbName) throws Exception {

      // new File(filename) does not create a new 
      // file in your file system
      File file = new File (dbName);

      if (file.exists()) 
      { // the file already existed and the program will enter this block
        System.out.print("Do something");
      }
      else 
      { //the file did not exist and you can send your error msg
        System.out.print("Do something else"); 
      } 
    }

我几次误解了你的问题,我的错,但我认为就是这样。


0
投票

有这样的事吗?

public boolean databaseExist()
{
    File dbFile = new File(DB_PATH + DB_NAME);
    return dbFile.exists();
}

0
投票

晚了,有帮助吗:

public boolean didPackageCreate() {

    File dbfile = this.getDatabasePath("app.db");
    if(dbfile.exists()){
        // db exist
    }else{
       // db doesn't exist
    }
}

0
投票

直到今天,我相信这是实现这一目标的最简单方法。 couchbase lite 数据库是在本地创建的,因此如果它已经存在,请将其添加到配置中并创建一个数据库实例以开始使用它。

这里是文档:https://docs.couchbase.com/couchbase-lite/current/java/database.html#lbl-find-db-loc

import com.couchbase.lite.*;


 private static Database openOrCreateDatabase(String dbName) throws CouchbaseLiteException {
        DatabaseConfiguration config = new DatabaseConfiguration();
        File file=new File(config.getDirectory());
        if (!Database.exists(dbName,file)){
            System.out.println("Database created: "+dbName);
            return new Database(dbName, config);
        }else{
            config.setDirectory("../../"+dbName+".cblite2"); //path to your cblite folder
            return new Database(dbName, config);
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.