我在使用Android SDK 1.6编程时遇到问题。我正在做与“记事本示例”相同的操作,但是当我尝试查询时程序崩溃。如果我尝试直接在DatabaseHelper create()方法中执行查询,它就会成功,但在此函数之外却不会。你有什么主意吗?
这是来源:
public class DbAdapter {
public static final String KEY_NAME = "name";
public static final String KEY_TOT_DAYS = "totdays";
public static final String KEY_ROWID = "_id";
private static final String TAG = "DbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "flowratedb";
private static final String DATABASE_TABLE = "girl_data";
private static final String DATABASE_TABLE_2 = "girl_cyle";
private static final int DATABASE_VERSION = 2;
/**
* Database creation sql statement
*/
private static final String DATABASE_CREATE =
"create table "+DATABASE_TABLE+" (id integer, name text not null, totdays int);";
private static final String DATABASE_CREATE_2 =
"create table "+DATABASE_TABLE_2+" (ref_id integer, day long not null);";
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
db.execSQL(DATABASE_CREATE_2);
db.delete(DATABASE_TABLE, null, null);
db.delete(DATABASE_TABLE_2, null, null);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);
db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE_2);
onCreate(db);
}
}
public DbAdapter(Context ctx) {
this.mCtx = ctx;
}
public DbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public long createGirl(int id,String name, int totdays) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ROWID, id);
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_TOT_DAYS, totdays);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
public long createGirl_fd_day(int refid, long fd) {
ContentValues initialValues = new ContentValues();
initialValues.put("ref_id", refid);
initialValues.put("calendar", fd);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
public boolean updateGirl(int rowId, String name, int totdays) {
ContentValues args = new ContentValues();
args.put(KEY_NAME, name);
args.put(KEY_TOT_DAYS, totdays);
return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
public boolean deleteGirlsData() {
if (mDb.delete(DATABASE_TABLE_2, null, null)>0)
if(mDb.delete(DATABASE_TABLE, null, null)>0)
return true;
return false;
}
public Bundle fetchAllGirls() {
Bundle extras = new Bundle();
Cursor cur = mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
KEY_TOT_DAYS}, null, null, null, null, null);
cur.moveToFirst();
int tot = cur.getCount();
extras.putInt("tot", tot);
int index;
for (int i=0;i<tot;i++){
index=cur.getInt(cur.getColumnIndex("_id"));
extras.putString("name"+index, cur.getString(cur.getColumnIndex("name")));
extras.putInt("totdays"+index, cur.getInt(cur.getColumnIndex("totdays")));
}
cur.close();
return extras;
}
public Cursor fetchGirl(int rowId) throws SQLException {
Cursor mCursor =
mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_NAME, KEY_TOT_DAYS}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor fetchGirlCD(int rowId) throws SQLException {
Cursor mCursor =
mDb.query(true, DATABASE_TABLE_2, new String[] {"ref_id",
"day"}, "ref_id=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}
我有同样的问题,我通过查看您对创建新数据库的评论来解决。
仅在创建数据库时才调用onCreate()方法。如果已经准备好创建数据库(它将在您第一次尝试代码时创建它:),则下次运行模拟器时将不会调用onCreate。因此,在下次启动模拟器时,在onCreate()方法中修改sql语句不会更改表,并且您仍然会拥有第一次启动模拟器并创建数据库时创建的表。在这种情况下,您的代码中将包含错误的sql语句。第二种情况是您使用了adb-sqlite3控制台(shell)程序并修改了表。数据库已经创建,所以不会调用onCreate()-无论您要用什么代码在onCreate()中描述该表,该表都将与通过shell修改该表相同...
我的问题是我使用Shell删除了表,并希望onCreate重新创建它们,但是数据库已经创建,并且不再有表。
因此,结论是,第一次创建数据库时,所有表都已创建,如果需要修改它们,请通过shell修改它们或删除数据库,以便在您运行程序时将onCreate称为。
天哪,这段文字听起来像是一个无限循环:)很抱歉使答案复杂化,我真的真的已经分层了...:(
祝你好运!
我还没有机会验证我的理论,但是看起来“ DATABASE_CREATE”定义了“ id”,并且您正在查询中检索“ _id”。您在顶部定义“ KEY_ROWID”,但不要在数据库创建查询中使用该常量。
但是,如果这是您的主要问题,我不知道为什么“如果我尝试直接在DatabaseHelper create()方法中执行查询,它就走了,但是从此功能中走了,却没有”。
希望有所帮助。
我有同样的问题。即使从代码中消除了所有的缺陷,查询仍然崩溃。
我更改了数据库名称,它开始工作。
[我怀疑自从我测试较早的“不良”代码迭代以来没有重启仿真器,因此数据库实例陷入了一次运行中。
通常,没有太多的错误处理。我可以在很多地方看到代码可能崩溃。
对于初学者,请用try-catch包围这些内容
也:
SELECT id,name from table
然后您可以使用getString(1);访问名称>最重要的是,请熟悉adb logcat
(下面的链接)。此工具将从您的设备中提取日志缓冲区并实时监控它是否崩溃。
如果您的应用程序崩溃,adb + logcat将向您显示堆栈跟踪,它将指向您的应用程序崩溃的确切行,以及原因!
https://developer.android.com/studio/command-line/adb.html#logcat