我陷入了一种我不知道如何在 android 中使用 sqlite 文件的情况
- 放在哪里?和
- 我们如何整合它?
我知道我可以从文件夹中获取它,data/data/PACKAGE/databases/
但是当有一个新项目时,我找不到数据库文件夹。在哪里放置我的扩展名为.sqlite
. 任何答案将不胜感激。谢谢你。
您需要将 sqlite 文件放在项目的 assets 文件夹中,并且应该将此文件复制到 SD 卡上的所需位置。在这里希望这对你有所帮助。
public DataBaseHelper(Context context)
{
super(context, DB_NAME, null, 300500);// 1? its Database Version
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
this.mContext = context;
}
public void createDataBase() throws IOException
{
//If database not exists copy it from the assets
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist)
{
this.getReadableDatabase();
this.close();
try
{
//Copy the database from assests
copyDataBase();
Log.w(TAG, "createDatabase database created");
}
catch (IOException mIOException)
{
throw new Error("ErrorCopyingDataBase");
}
}
}
//Check that the database exists here: /data/data/your package/databases/Da Name
private boolean checkDataBase()
{
File dbFile = new File(DB_PATH + DB_NAME);
//Log.v("dbFile", dbFile + " "+ dbFile.exists());
return dbFile.exists();
}
//Copy the database from assets
private void copyDataBase() throws IOException
{
InputStream mInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
//Open the database, so we can query it
public boolean openDataBase() throws SQLException
{
String mPath = DB_PATH + DB_NAME;
//Log.v("mPath", mPath);
myDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
//mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
return myDataBase != null;
}
@Override
public synchronized void close()
{
if(myDataBase != null)
myDataBase.close();
super.close();
}
// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
// to you to create adapters for your views.
我认为这可能对您有所帮助:http: //zaman91.wordpress.com/2010/09/22/android-how-to-use-own-sqlite-database/
如果您使用 Eclipse,您可以下载此插件,只需复制粘贴 .jar 文件即可:) 只需通过此链接 ,您就可以在 DDMS 中看到您的数据库
通常Sqlite数据存储在
//data/data/<Your-Application-Package-Name>/databases/<your-database-name>
但是如果你看不到他们。您可以将数据库直接存储在 SD 卡中,如下所示:
static class SQLiteHelper extends SQLiteOpenHelper {
SQLiteHelper(final Context context) {
super(context, Environment.getExternalStorageDirectory()
+ File.separator + "/DataBase/" + File.separator
+ DATABASE_NAME, null, DATABASE_VERSION);
}
现在您可以在 /sdcard 的 DataBase 文件夹中看到创建的数据库如果您将数据库存储在内部存储器中,那么您也可以将您的数据库从内部存储器复制到 SD 卡:)
当有一个新项目时,我找不到数据库文件夹
原因是安全。如果将数据库放置在内部存储中会更准确,这样数据库只能从创建它的应用程序中看到。您的 .db 文件存储在
data/data/com.example.db/databases
所以如果你想在另一个项目(有不同的包)中打开它,就不会有你的文件。
在哪里放置我的文件
如果您想在多个应用程序之间实现和共享一个 db,您可以将文件保存到外部存储中,但现在这里是关于敏感数据安全性的问题。
但您还有另一种选择。您可以为更多应用程序共享一个数据库,但因此您必须在manifest.xml中为每个应用程序指定sharedUserId
android:sharedUserId="com.example"
然后,如果您想从另一个应用程序连接到 db,您需要使用托管应用程序包。
Context cntxt = this.createPackageContext("hosting.app.package.name",
Context.CONTEXT_INCLUDE_CODE);