尝试获取Android SQLite数据库中表的名称时出错

时间:2011-11-11 10:57:21

标签: java android sqlite listview

我试图通过创建表名的字符串数组然后将它们传递给listView来列出我在数据库中的所有表,然后将它们传递给listView,我有一个按钮来调用创建listview的活动。该活动从我的数据库类调用getDbNames。我在Eclipse中的LogCat中得到了这个错误:

标签 - CursorWindow
文本 - 字段槽1的错误请求,-1。 numRows = 3,numColumns = 1

我将包括我的整个数据库类,看看你是否可以提供帮助。

package the.paddox.pool;

import java.util.ArrayList;

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

public class Database {

public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "newplayersname";
public static final String KEY_PERCENTAGE = "percentage";

private static final String DATABASE_NAME = "paddoxa";
private static final String DATABASE_TABLE = "players";
private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

public static class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                KEY_NAME + " TEXT NOT NULL);"
                );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }

}

public Database(Context c){
    ourContext = c;
}

public Database open() throws SQLException{
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close(){
    ourHelper.close();
}

public long createEntry(String name) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME, name);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);
}

public String[] getData() {
    // TODO Auto-generated method stub
    String[] columns = new String[] { KEY_NAME};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result ="";
    String[] mString = {""};
    ArrayList<String> playersData = new ArrayList<String>();

    int iName = c.getColumnIndex(KEY_NAME);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        result = c.getString(iName);
        playersData.add(result);
    }
    mString = (String[]) playersData.toArray(new String[playersData.size()]);
    return mString;
}

public String[] getDBNames() {
    String[] result = null;
    Cursor c = ourDatabase.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
    c.moveToFirst();
    result = new String[c.getCount()];
    int i = 0;
    while (c.moveToNext()) {
        result[i] = c.getString(c.getColumnIndex(DATABASE_TABLE));
        i++;
    }
    return result;
}


}

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

我认为,您收到此错误是因为在您尝试获取iName时,您在该声明之前没有调用c.moveToFirst();

相反,您可以像以下一样使用它:

for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
    result = c.getString(c.getColumnIndex(KEY_NAME));
    playersData.add(result);
}

答案 1 :(得分:0)

请在 getDBNames()中查看您的查询

"SELECT name FROM sqlite_master WHERE type='table'"
,因为它列出了name

列的行

你的

c.getColumnIndex(DATABASE_TABLE) // DATABASE_TABLE = "players" in your code.
实际上无法正常工作并返回-1。

有关详情,请查看here

希望这有助于您进一步开展工作。