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

3gtaxfhh  于 11个月前  发布在  SQLite
关注(0)|答案(7)|浏览(200)

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

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

字符串

axkjgtzd

axkjgtzd1#

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(); 

     }

字符串

zbq4xfa0

zbq4xfa02#

假设dbName参数指示SQLite文件的路径(尽管有“-wal”和“-shm”伴随文件),您可以使用Java java.io.File类和its exists() predicate

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会做得更好,确保它的所有要求都能得到满足。

np8igboo

np8igboo3#

我发现最好的方法是检查文件的大小。如果你执行:
return new File(DB_NAME).exists()应该等于True。
你应该得到一个true,因为它会创建它。相反,检查以确保文件大小大于0。至少在这种情况下,你知道文件中有数据,而无需附加和查询结果。
相反,应:
return new File(DB_NAME).length() > 0

e0bqpujr

e0bqpujr4#

你可以这样做,我猜你认为你是在你的目录中创建一个新的文件,当你做“新文件(名称)",我已经红色,但这不是正在发生的事情。

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

字符串
我误解了你的问题几次,我的错,但我认为这是它。

nx7onnlm

nx7onnlm5#

像这样的?

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

字符串

t5zmwmid

t5zmwmid6#

晚了,可能会有帮助:

public boolean didPackageCreate() {

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

字符串

vddsk6oq

vddsk6oq7#

直到今天,我相信这是实现这一目标的最简单的方法。Couchbase Lite数据库是在本地创建的,所以如果它已经存在,您可以将其添加到配置中,并创建一个Database示例来开始使用它。
以下是文档: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);
        }
    }

字符串

相关问题