我已经编写了一个使用SQLite数据库的应用程序。在我没有使用SQLite并只使用普通和常规字段存储数据的第一阶段,我的应用程序在我的手机中工作得很好。但是现在当我使用SQLite有效地存储和提取数据时,我的应用程序在我的手机中崩溃了。有趣的是,它在我的计算机上的AVD仿真器上运行良好。知道为什么会这样吗?
这是我的DB类:
public class WordsDB extends SQLiteOpenHelper {
static final String DATABASE_NAME = "DicDB";
static final String TermTable = "Terms";
static final String ID = "id";
static final String Selected = "selected";
static final String Term = "term";
static final String Br = "br_phon";
static final String NAm = "nam_phon";
static final String Type = "type";
static final String Noun = "noun";
static final String Verb = "verb";
static final String Adjective = "adjective";
static final String Adverb = "adverb";
static final String DefTable = "Definitions";
static final String Def = "definition";
static final String Ex = "example";
public static final int DATABASE_VERSION = 1;
private static final String TERM_TABLE_CREATE = "Create table " + TermTable +
"(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
Selected + " VARCHAR(5) ," +
Term + " VARCHAR(20) ," +
Br + " VARCHAR(20) ," +
NAm + " VARCHAR(20) ," +
Type + " VARCHAR(10) ," +
Noun + " VARCHAR(20) ," +
Verb + " VARCHAR(20) ," +
Adjective + " VARCHAR(20) ," +
Adverb + " VARCHAR(20)) ";
private static final String DEF_TABLE_CREATE = "Create table " + DefTable +
"(" + ID + " INTEGER PRIMARY KEY ," +
Def + " VARCHAR(30) ," +
Ex + " VARCHAR(160)) ";
public WordsDB(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(TERM_TABLE_CREATE);
db.execSQL(DEF_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TermTable);
db.execSQL("DROP TABLE IF EXISTS " + DefTable);
onCreate(db);
}
}
这是DataSource类:
public class WordsDS {
private WordsDB dbHelper;
private SQLiteDatabase db;
public WordsDS(Context context) {
dbHelper = new WordsDB(context);
}
public void open() throws SQLException {
db = dbHelper.getWritableDatabase();
}
public void close() {
db.close();
}
// Methods to inset, update and extract data from table
.......
}
我在我的主要活动中用WordsDS类创建一个对象,通过open方法我得到可写数据库等等,在我的其他类中,我使用WordsDS对象并通过它插入,更新和提取数据。
答案 0 :(得分:1)
首先,您必须卸载该应用才能进行全新安装。这意味着所有创建的数据库都将在手机上擦除。这将确保如果添加新列或发生数据库结构更改,则将创建数据库。您可能认为onUpgrade方法会为您执行此操作,但它不会!
至于在移动设备上调试您的应用。首先,您必须在计算机上安装手机的驱动程序。然后,您现在可以运行adb logcat。