2011-11-11 66 views
1

我試圖通過創建表名的字符串數組,然後將它們傳遞給列表視圖列出我在我的數據庫列表視圖中的所有表,我有一個按鈕,它調用創建列表視圖的活動。該活動從我的數據庫類調用getDbNames。我在Eclipse中的LogCat中得到這個錯誤:錯誤試圖獲取我的Android SQLite數據庫中的表名稱

標記 - 光標窗口
文本 - 字段插槽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; 
} 


} 

任何幫助將不勝感激。

回答

0

我想,你會得到這個錯誤,因爲你在哪裏試圖獲得iName,在這個陳述之前你還沒有打電話給c.moveToFirst();

相反,你可以用它喜歡:

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

請檢查您的查詢

"SELECT name FROM sqlite_master WHERE type='table'"
getDBNames(),因爲它列表行的列 name

c.getColumnIndex(DATABASE_TABLE) // DATABASE_TABLE = "players" in your code.
實際上將無法正常工作,併爲返回-1。

詳情請參考here

希望這會幫助你進一步工作。

相關問題