【问题标题】:Error trying to get the names of the tables in my Android SQLite Database尝试获取我的 Android SQLite 数据库中的表名时出错
【发布时间】:2011-12-26 22:29:05
【问题描述】:

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

标签 - 光标窗口
文本 - 对字段槽 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;
}


}

任何帮助将不胜感激。

【问题讨论】:

    标签: java android sqlite listview


    【解决方案1】:

    我认为,您收到此错误是因为您尝试获取 iName 的位置,但在该语句之前您没有调用 c.moveToFirst();

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

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

    【讨论】:

      【解决方案2】:

      请检查您的查询

      "SELECT name FROM sqlite_master WHERE type='table'"
      in getDBNames() 因为它列出了name 列的行

      你的

      c.getColumnIndex(DATABASE_TABLE) // DATABASE_TABLE = "players" 在您的代码中。
      实际上不起作用并返回 -1。

      详情请查看here

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-05
        • 2020-04-22
        • 2012-11-28
        • 2012-08-27
        • 2020-09-01
        • 2012-06-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多