java - 无法从 sqlite 表获取值

标签 java android sqlite android-sqlite

在我的应用程序中,我想将数据保存在数据库中。

这是我的 SQLiteHelper 代码

public class UserSqliteHelper extends SQLiteOpenHelper {
    private final String LOGCAT = "JBF/SQLite";
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "jbfjsonEntityDB";

    private static final String TABLE_NAME = "jbfjsonEntity";

    private static final String KEY_JSON = "json";
    private static final String KEY_URL_PATH = "url_path";
    private static final String KEY_TIME = "added_on";

    public UserSqliteHelper(Context context) {
        super(context, "dictionarysqlitehelper.db", null, 1);
        Log.d(LOGCAT, "Created");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "("
                + KEY_JSON + " TEXT, "
                + KEY_TIME + " TIMESTAMP NOT NULL DEFAULT current_timestamp, "
                + KEY_URL_PATH  + " TEXT )";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String query = "DROP TABLE IF EXISTS " + TABLE_NAME ;
        db.execSQL(query); onCreate(db);
    }

    public void addJsonEntity(JsonEntity jsonEntity) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_JSON, jsonEntity.getJson());
        values.put(KEY_URL_PATH, jsonEntity.getUrl_path());

        // Inserting Row
        db.insert(TABLE_NAME, null, values);
        db.close();
    }

    public JSONObject getJsonByUrl(String url) {
        String json = "";
        SQLiteDatabase db = this.getReadableDatabase();

        try {
//            Cursor c = db.query(TABLE_NAME, null, KEY_URL_PATH + "=?", new String[]{url}, null, null, null);
            String selectQuery = "SELECT * FROM " + TABLE_NAME + " where " + KEY_URL_PATH + "='"+url+"'";
            Cursor c = db.rawQuery(selectQuery, null);

            if (c == null) {
                return null;
            } else {
                c.moveToFirst();
                json =c.getString(c.getColumnIndex(KEY_JSON));
                if (json != null) {
                    return new JSONObject(json);
                } else {
                    return null;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}

当我从我的 Activity 中调用时

UserSqliteHelper sqliteHelper = new UserSqliteHelper(SplashActivity.this);
sqliteHelper.getWritableDatabase();
sqliteHelper.addJsonEntity(new JsonEntity(STRING_CONFIGS_URL,response.toString()));
System.out.println("json  ==== "+sqliteHelper.getJsonByUrl(GET_USER_INFO_URL));

我总是遇到这个错误

android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

谁能告诉我我在这里做错了什么。为什么我无法获取数据库值?

最佳答案

查询与任何数据都不匹配。 moveToFirst() 失败,光标未指向有效行。您应该检查 moveToFirst() 是否成功 - 它返回一个 boolean 值。

它与任何数据都不匹配的原因是您通过不同的键存储和检索数据:STRING_CONFIGS_URLGET_USER_INFO_URL

关于java - 无法从 sqlite 表获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27803036/

相关文章:

java - IntelliJ IDEA : Tomcat 8. 5.13 和 9.0.0.M19 — 工件部署期间出错。有关详细信息,请参阅服务器日志

java - Java内存模型如何确保所有线程看到的变量值一致?

android - 在单个 Activity 中的 Fragment 之间切换

iphone - sqlite3_open - 无法打开数据库?

java - Java.util.Map接口(interface)的父类是哪个?

java 随机数生成器 - 彩票

android - 在 apk 中包含 jar,android studio

android - 如何在 renderscript 中使用半精度?

java - ActiveAndroid 多对多关系

c# - 开发 Windows Phone 8、SQL Server Express 或 SQL Server Compact 或 SQLite 时最好使用什么?