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

发布于 2024-11-30 06:56:00 字数 473 浏览 0 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

扶醉桌前 2024-12-07 06:56:00
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(); 

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

     }
玩心态 2024-12-07 06:56:00

假设您的 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 会做得更好 确保满足其所有要求

Assuming that your dbName parameter indicates the path to the SQLite file ("-wal" and "-shm" companion files notwithstanding), you can use the Java java.io.File class and 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.
  }
}

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.

冰葑 2024-12-07 06:56:00

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

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

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

相反,请执行以下操作:

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

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.

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.

Instead do:

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

述情 2024-12-07 06:56:00

你可以做这样的事情,我猜你认为当你执行“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"); 
      } 
    }

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

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.

帅哥哥的热头脑 2024-12-07 06:56:00

像这样的东西吗?

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

Something like this?

public boolean databaseExist()
{
    File dbFile = new File(DB_PATH + DB_NAME);
    return dbFile.exists();
}
你在看孤独的风景 2024-12-07 06:56:00

晚了,可能有帮助:

public boolean didPackageCreate() {

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

late, may it help :

public boolean didPackageCreate() {

    File dbfile = this.getDatabasePath("app.db");
    if(dbfile.exists()){
        // db exist
    }else{
       // db doesn't exist
    }
}
最单纯的乌龟 2024-12-07 06:56:00

直到今天,我相信这是实现这一目标的最简单方法。 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);
        }
    }

To this day I believe this is the simplest way to achieve this. The couchbase lite database is created locally, so if it already exists, you add it to the config and create a Database instance to start working with it.

Here is the documentation: 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);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文