gpt4 book ai didi

java - Android CursorWindow 仅返回 1 行、1 列

转载 作者:行者123 更新时间:2023-12-01 15:37:36 27 4
gpt4 key购买 nike

我正在尝试创建一个应用程序来从预先存在的数据库中提取 sqlite 数据,在应用程序运行之前将其复制到目录中。该数据库包含超过 2000 行和大约 20 列。

DBAdapter 类。

package com.t3hh4xx0r.gmusicsniper;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBAdapter {

public static final String KEY_ROWID = "Id";
public static final String KEY_TITLE = "Title";
private static final String DATABASE_NAME = Constants.gMusicSniperDir + Constants.musicDB;
private static final String DATABASE_TABLE = "MUSIC";

private static final int DATABASE_VERSION = 37;


private final Context context;

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBAdapter(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}
}

//---opens the database---
public DBAdapter open() throws SQLException {
db = SQLiteDatabase.openDatabase(DATABASE_NAME, null, SQLiteDatabase.OPEN_READONLY);
return this;
}

//---closes the database---
public void close() {
DBHelper.close();
}

//---retrieves a particular title---
public Cursor getTitle(int songFinalValue) throws SQLException {
Cursor mCursor =
db.rawQuery("SELECT Title FROM MUSIC WHERE Id = 5899;", null);
//db.query(DATABASE_TABLE, new String [] {KEY_TITLE}, KEY_ROWID + " = \'" + songFinalValue + "\'", null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}

}

“应该”检索数据的方法。在此测试用例中,songFinalValue 为 5899,尽管我已将该值硬编码到查询语句中,以确保问题不在于任何未初始化的变量。

songFinalValue 引用第一列 Id。我尝试返回的值是相应标题的第 13 列。

    private void getTrackName(String songFinal) {
int songFinalValue = Integer.parseInt(songFinal);
DBAdapter db = new DBAdapter(this);

//---get a title---
db.open();
Cursor c = db.getTitle(songFinalValue);
while (c.isAfterLast() == false && c != null) {
c.getString(13); // will fetch you the data
c.moveToNext();
//DisplayTitle(c);
String cS = String.valueOf(c);
Log.d("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", cS);
}
db.close();
}

运行时出现错误

E/CursorWindow(17998): Failed to read row 0, column 13 from a CursorWindow which has 1 rows, 1 columns.
E/su (18033): sudb - Database closed
W/System.err(17998): java.lang.IllegalStateException: Couldn't read row 0, col 13 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

我已经为此工作了一段时间,并访问了每个指南、教程,并解决了许多 SOF 问题,但我似乎无法弄清楚这个问题。

这是我第一次涉足 SQLite 数据库,也是我的第一个应用程序。我假设这个错误很容易被任何有经验的人解决,它通常最终会成为我的一些愚蠢的错误,尽管我一生都无法弄清楚它会是什么。

最佳答案

在评论中回复。

Take a look at code - db.rawQuery("SELECT Title FROM MUSIC WHERE Id = 5899;", null); This will return single column if given ID matches. – AVD Dec 27 '11 at 4:04

关于java - Android CursorWindow 仅返回 1 行、1 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8641004/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com