如何使用 Java 检查 SQLite 数据库文件是否存在?
我从用户那里获取数据库文件的名称,我想检查该文件是否已经存在。如果它确实存在,我想向用户显示错误消息,但我不知道如何检查该文件是否存在。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
假设您的 dbName 参数指示 SQLite 文件的路径(尽管有“-wal”和“-shm”配套文件),您可以使用 Java
java.io.File
类及其 其exists()
谓词:也可以进行其他检查,例如进程是否有权创建文件,尽管最终 SQLite 会做得更好 确保满足其所有要求。
Assuming that your
dbName
parameter indicates the path to the SQLite file ("-wal" and "-shm" companion files notwithstanding), you can use the Javajava.io.File
class and itsexists()
predicate: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.
我发现最好的方法是检查文件的大小。如果执行:
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
你可以做这样的事情,我猜你认为当你执行“new File(name)”时,你正在目录中创建一个新文件,对于我红色的内容,但事实并非如此。
我几次误解了你的问题,我的错,但我认为就是这样。
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.
I misinterpreted your question a few times, my bad, but I think that's it.
像这样的东西吗?
Something like this?
晚了,可能有帮助:
late, may it help :
直到今天,我相信这是实现这一目标的最简单方法。 couchbase lite 数据库是在本地创建的,因此如果它已经存在,请将其添加到配置中并创建一个数据库实例以开始使用它。
这是文档: https:// /docs.couchbase.com/couchbase-lite/current/java/database.html#lbl-find-db-loc
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