获取数据库路径 [英] Get database path
问题描述
林新Android和我想创建一种CRUD的使用SQLite。
Im new to android and I want to create kind of CRUD with SQLite.
我有一个公共类DataBaseHelper
其中有一个构造函数:
I have a public class DataBaseHelper
which have a constructor:
public DataBaseHelper(Context context)
{
this.context = context;
}
和吸气的DATEBASE:
and a getter for datebase:
public SQLiteDatabase getDataBase()
{
if (dataBase == null)
{
try
{
dataBase = context.openOrCreateDatabase(DataBaseHelper.dbName, Context.MODE_PRIVATE, null);
dataBase.execSQL("CREATE TABLE " + DataBaseHelper.accountTblName + " " +
"(ID integer PRIMARY KEY AUTOINCREMENT" +
",name TEXT" +
",address TEXT" +
",password TEXT)");
}
catch(Exception e)
{
Log.e(context.toString(), e.getMessage());
if (dataBase != null)
{
dataBase.close();
}
}
}
return dataBase;
}
如果我启动程序在第一次 - 似乎一切都还好。数据库为空,我创建它(与表)。但是,当我重新启动我的应用程序的数据库是空再次表不能创建。
If I start a program for the first time - everything seems to be okay. database is null and I create it (with a table). But when I restart my app database is null again and table cannot be created.
我决定写一个CHECKDB功能,它试图打开一个数据库。但我在哪里可以得到它的路径?我不希望它成为硬codeD。
I've decided to write a checkDB function, which tries to open a database. But where can I get a path for it? I dont want it to be "hardcoded".
,也可以是我做的一些反模式?
Or may be I'm doing some kind of anti-pattern?
推荐答案
您可以通过获取路径context.getDatabasePath(DataBaseHelper.dbName)
这篇关于获取数据库路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!