未显示数据库中的数据

时间:2018-04-19 14:32:30

标签: java android database sqlite

不太清楚如何解释这一点,但会尽力而为。

我在Android项目中创建了一个数据库:

公共类FootySortItDatabase扩展了SQLiteOpenHelper {

private static final String DATABASE_NAME = "FootySortItDatabase";
private static final int DATABASE_VERSION = 1;

public FootySortItDatabase(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
        final String SQL_CREATE_PLAYER_TABLE = "CREATE TABLE " +
                PlayerDetails.PlayerTableEntry.TABLE_NAME + "(" +
                PlayerDetails.PlayerTableEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                PlayerDetails.PlayerTableEntry.COLUMN_PLAYER_NAME + " TEXT NOT NULL, " +
                PlayerDetails.PlayerTableEntry.COLUMN_PLAYER_NUMBER + " TEXT NOT NULL, " +
                PlayerDetails.PlayerTableEntry.COLUMN_IS_PLAYING + " INTEGER NOT NULL " +
                ");";

        db.execSQL(SQL_CREATE_PLAYER_TABLE);
}

我已经设定了以下合同:

public static final class PlayerTableEntry implements BaseColumns{
    public static final String TABLE_NAME = "playerTable";
    public static final String COLUMN_PLAYER_NAME = "playerName";
    public static final String COLUMN_PLAYER_NUMBER = "playerNumber";
    public static final String COLUMN_IS_PLAYING = "isPlaying";
}

我调用一个传递数据库和一系列细节的方法:

 private SQLiteDatabase playerDatabase;

    private Cursor getAllPlayers(){
        playerDatabase = ComposeMessage.addPlayerToTheDatabase(playerDatabase, playerDataSet);
        return playerDatabase.query(PlayerDetails.PlayerTableEntry.TABLE_NAME,
                null,null,null,
                null,null,null);
    }

public static SQLiteDatabase addPlayerToTheDatabase(SQLiteDatabase playerDatabase, ArrayList<PlayerDetails> listGame) {

    for (PlayerDetails i : listGame) {
        ContentValues cv = new ContentValues();

        cv.put(PlayerDetails.PlayerTableEntry.COLUMN_PLAYER_NAME, i.name);
        cv.put(PlayerDetails.PlayerTableEntry.COLUMN_PLAYER_NUMBER, i.number);

        playerDatabase.insert(PlayerDetails.PlayerTableEntry.TABLE_NAME, null, cv);
    }
    return playerDatabase;
}

然后,我将光标传递给我的RecyclerView:

public PlayerListRecyclerViewAdapter(Context context, Cursor cursor, ArrayList<PlayerDetails> aPlayerList,
                                     int count) {
    playerDataSet = aPlayerList;
    this.pCursor = cursor;
    pContext = context;

但我的pCursor = 0.这意味着它们在表中没有行吗?

通过终端,我使用adb导航到数据库,在数据库上使用SQLite3来搜索表的数据,但它不会返回任何内容:

sqlite> .tables
android_metadata  playerTable
sqlite> select * from playerTable;
sqlite>

当我调试时,我可以在插入部分看到,i.name和i.number有数据,&#34;它看起来是插入它&#34;但是为什么光标为0,当我搜索数据时,为什么它没有返回任何东西? 不知道怎么过来......

1 个答案:

答案 0 :(得分:1)

表格中有3列。它们都不能为空。当您写入数据库时​​,您将写入2列而不是3.因此,插入将失败 要创建数据库,我建议Schematic并使用Stetho进行调试。