Simplecursoradapter Does Not Load External Sqlite Database: "_id" Error
Solution 1:
It doesn't appear from your code that you've created the table yet, so no columns will be found.
You do this within the onCreate
method by creating a query to create the table. In your code you appear to be doing a select rather than create.
privatestaticfinalStringTABLE_CREATE="create table "
+ TABLE_NAME
+ "("
+ COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_TYPE + " text not null default '', "
+ COLUMN_ORDERNUM + " integer not null default 0, "
+ COLUMN_INSTALLATION + " integer not null default 0, "
+ COLUMN_SUBSTRUCTURE + " text not null default ''"
+ ");";
@OverridepublicvoidonCreate(SQLiteDatabase database) {
database.execSQL(TABLE_CREATE);
}
To store this on the external storage, you'll need to override getDatabasePath(...). A similar solution is here https://stackoverflow.com/a/8385537/935779
@OverridepublicFilegetDatabasePath(String name) {
// reference where you would like the file to be here.File result = newFile(getExternalFilesDir(null), name);
return result;
}
I believe you'll want to override this with your Application
class since it's a member of ContextWrapper
.
The method getDatabaseFile(...)
is used inside of openOrCreateDatabase(...) to determine the location.
Alternatively you could just override openOrCreateDatabase(...)
and set the file location there.
Solution 2:
I don't think you can change or even specify the location of the database, only the name. Leave off the path and don't try to put it in External Storage - let Android determine the path.
Solution 3:
Ok, this took me almost week and a lot of stress but here is the solution. I started to go through a lot of tutorials and got it working in this one:
http://www.mysamplecode.com/2012/11/android-database-content-provider.html
I extracted the database from the virtual device and manually added more data. Then copied the database to the desired folder on my device folder (Its just to make sure the database consistency/columns are exactly the same). Then changed MyDatabaseHelper class as follows:
publicclassMyDatabaseHelperextendsSQLiteOpenHelper {
privatestaticfinalStringDATABASE_PATH= Environment.getExternalStorageDirectory().getAbsoluteFile()+ "/MYFOLDER/";
privatestaticfinalStringDATABASE_NAME="TheWorld.db";
privatestaticfinalintDATABASE_VERSION=1;
MyDatabaseHelper(Context context) {
super(context, DATABASE_PATH+DATABASE_NAME, null, DATABASE_VERSION);
}
@OverridepublicvoidonCreate(SQLiteDatabase db) {
CountriesDb.onCreate(db);
}
@OverridepublicvoidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
CountriesDb.onUpgrade(db, oldVersion, newVersion);
}
}
Don't forget to add permissions to your manifest:
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Done! If you read through the posts above the answer is based on Kirks advice so reading his recommended link helps. I still have more tests to do just in case my database structure was wrong before.
Post a Comment for "Simplecursoradapter Does Not Load External Sqlite Database: "_id" Error"