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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7100597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:37:43  来源:igfitidea点击:

How do I check whether a SQLite database file exists using Java?

javasqlitedatabase-connection

提问by SunyGirl

I get the name of database file from a user, and I want to check if that file already exists. If it does exist, I want to show an error message to user, but I don't know how to check if the file exists.

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

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

回答by Heisenbug

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

     }

回答by seh

Assuming that your dbNameparameter indicates the path to the SQLite file ("-wal" and "-shm" companion files notwithstanding), you can use the Java java.io.Fileclass and its exists()predicate:

假设您的dbName参数指示 SQLite 文件的路径(尽管有“-wal”和“-shm”伴随文件),您可以使用 Javajava.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.
  }
}

Other checks could be warranted too, such as whether the process has the privilege to create the file, though ultimately SQLite will do a better job ensuring that all its requirements can be fulfilled.

也可以进行其他检查,例如进程是否具有创建文件的权限,但最终 SQLite 会做得更好,确保其所有要求都能得到满足

回答by n0manarmy

I've found that the best way to do this is to instead check the size of the file. If you execute:

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

return new File(DB_NAME).exists()should equal True.

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

You should get a true back because it will create it. Instead check to make sure the file size is greater than 0. At least in this case you know there's data in the file without attaching and querying for results.

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

Instead do:

而是这样做:

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

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

回答by NoggetGump

You can do something like this, I'm guessing you think you are creating a new file in your directory when you do "new File(name)", for what I've red, but that is not what is happening.

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

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

I misinterpreted your question a few times, my bad, but I think that's it.

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

回答by Eriksberger

Something like this?

像这样的东西?

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

回答by faridlab

late, may it help :

晚了,可能有帮助:

public boolean didPackageCreate() {

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