android - SQLite 问题 : Program Crash When Try a Query

标签 android database-connection

我在使用 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;

    }
}

最佳答案

我还没有机会检验我的理论,但看起来“DATABASE_CREATE”定义了“id”,而您正在查询中检索“_id”。您在顶部定义“KEY_ROWID”,但不要在您的数据库创建查询中使用该常量。

但是,如果这是您的主要问题,我不知道为什么“如果我尝试直接在 DatabaseHelper create() 方法中进行查询,它会运行,但在这个函数之外它不会”。

希望对您有所帮助。

关于android - SQLite 问题 : Program Crash When Try a Query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2857215/

相关文章:

android - OkBuck 不支持零依赖的 Android Jetifier

java - 如何找出从出发地到目的地经纬度的持续时间?

java - 使用网络托管连接到数据库后连接速度缓慢

php - Android 使用 php 服务器连接 mysql 数据库

android - 是否可以访问 ViewPager 正在查看的当前 fragment ?

android - 更改警报对话框中编辑文本的光标颜色

android - Android构建使用Travis的多个productFlavor buildTypes

java - 数据库连接关闭后无法关闭迭代器

Ruby -> PostgreSQL 连接 pg_hba.conf 设置为 "ident sameuser"而不是 "trust"

database - 如何从Ubuntu的控制台进入mysql?