我是android新手并创建了我的第一个使用嵌入式sqlite数据库的应用程序。我填充了数据库,应用程序在Android工作室模拟器上工作正常,但当我尝试在我的移动设备上访问它时,应用程序是空的。
我已拉myapp.db
然后将其推送到设备:
~/Android/Sdk/platform-tools/adb -s emulator-5554 push /home/me/Desktop/myapp2/myapp.db /data/data/com.myappshop.myapp/databases/myapp.db
所以我可以使用adb shell看到这些文件:
root@generic_x86_64:/data/data/com.myappshop.myapp/databases # ls -al
-rw-rw---- u0_a72 u0_a72 16384 2016-05-20 20:04 dictionary.db
-rw------- u0_a72 u0_a72 8720 2016-05-20 20:04 dictionary.db-journal
-rw-rw-rw- root root 253952 2016-05-26 01:27 myapp.db
-rw------- u0_a72 u0_a72 8720 2016-05-23 00:53 myapp.db-journal
-rw-rw---- u0_a72 u0_a72 20480 2016-05-18 01:30 myapp1.db
-rw------- u0_a72 u0_a72 12824 2016-05-18 01:30 myapp1.db-journal
这是数据库类
public class DictionaryDatabase extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "myapp.db";
private static final String TABLE_DICTIONARY = "dictionary";
private static final String FIELD_WORD = "word";
private static final String FIELD_DEFINITION = "definition";
private static final int DATABASE_VERSION = 1;
DictionaryDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_DICTIONARY +
"(_id integer PRIMARY KEY," +
FIELD_WORD + " TEXT, " +
FIELD_DEFINITION + " TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//Handle database upgrade as needed
}
public void saveRecord(String word, String definition) {
long id = findWordID(word);
if (id>0) {
updateRecord(id, word,definition);
} else {
addRecord(word,definition);
}
}
public long addRecord(String word, String definition) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(FIELD_WORD, word);
values.put(FIELD_DEFINITION, definition);
return db.insert(TABLE_DICTIONARY, null, values);
}
public int updateRecord(long id, String word, String definition) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("_id", id);
values.put(FIELD_WORD, word);
values.put(FIELD_DEFINITION, definition);
return db.update(TABLE_DICTIONARY, values, "_id = ?",
new String[]{String.valueOf(id)});
}
public int deleteRecord(long id) {
SQLiteDatabase db = getWritableDatabase();
return db.delete(TABLE_DICTIONARY, "_id = ?", new
String[]{String.valueOf(id)});
}
public long findWordID(String word) {
long returnVal = -1;
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(
"SELECT _id FROM " + TABLE_DICTIONARY +
" WHERE " + FIELD_WORD + " = ?", new String[]{word});
Log.i("findWordID","getCount()="+cursor.getCount());
if (cursor.getCount() == 1) {
cursor.moveToFirst();
returnVal = cursor.getInt(0);
}
return returnVal;
}
public String getWord(long id) {
String returnVal = "";
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(
"SELECT word FROM " + TABLE_DICTIONARY +
" WHERE _id = ?", new String[]{String.valueOf(id)});
if (cursor.getCount() == 1) {
cursor.moveToFirst();
returnVal = cursor.getString(0);
}
return returnVal;
}
public String getDefinition(long id) {
String returnVal = "";
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(
"SELECT definition FROM " + TABLE_DICTIONARY +
" WHERE _id = ?", new String[]{String.valueOf(id)});
if (cursor.getCount() == 1) {
cursor.moveToFirst();
returnVal = cursor.getString(0);
}
return returnVal;
}
public Cursor getWordList() {
SQLiteDatabase db = getReadableDatabase();
String query = "SELECT _id, " + FIELD_WORD +
" FROM " + TABLE_DICTIONARY + " ORDER BY " + FIELD_WORD +
" ASC";
return db.rawQuery(query, null);
}
}
我还尝试重新编写上面的类以符合引用次数最多的tutorial,但是没有成功。 非常感谢您帮助解决此问题。
答案 0 :(得分:0)
我认为你的问题可能是因为你的"数据库适配器"类。我总是把我的" myDbHelper"这个类很小,并把它包含在我的" __我想要工作的任何DB的名字_DBAdapter"类。
这是我的SQLiteOpenHelper,这个类包含在我的__DBAdapter类中。
private static class myDbHelper extends SQLiteOpenHelper {
public myDbHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,
int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.w(LOG_TAG, "DATABASE_CREATE: version: " + FeedContract.DATABASE_VERSION);
// create all table(s) for the database.
db.execSQL(DATABASE_TABLE_<TABLE NAME>);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Log version upgrade.
Log.w(LOG_TAG + "DBHelper", "Upgrading from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data");
// **** Upgrade DB ****
// This is where you would manually migrate your data from old DB version to new
// For now, drop old DB table(s)
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_<TABLE NAME>);
// Create a the new database.
onCreate(db);
}
}
然后在我的___DBAdapter类中,我创建了SQL命令来创建我将在该数据库中使用的表。 (以及插入/更新/删除/查询命令)这是我在课程中教授的方法,并建议其他人在开始时遵循。它有很多方面的帮助:
允许您测试(通过集成测试)您的&#34; DBAdapter&#34;轻松上课
允许您更好地将代码划分为更小的部分(更易于管理)将SQLLiteOpenHelper与SQLite DB上的其余操作分开。
允许您使用与您相同的API&#34; DBAdapter&#34; SQL使用的类以及为内容提供者提供的内容(我通常&#39;(总有例外)认为,如果不使用内容提供程序包装它,就不应该创建SQL数据库)
使SQLiteOpenHelper类尽可能小有助于缩小代码范围并使其更易于处理。
答案 1 :(得分:0)
我认为问题在于您的手机没有根,而您实际上并没有将数据库推入手机。你提到你在文件浏览器中看不到路径/data/data/com.myappshop.myapp。
答案 2 :(得分:0)
在安装数据库之后创建数据库一定是问题所在。尝试重新安装应用程序