lundi 21 décembre 2015

can't find SQLite _id column

I'm trying to learn how to use android studio.. I am currently creating a very simple app that simply allows a user to enter and track users and scores. I am unable to build this, due to this error message:

12-21 16:04:07.037 11681-11681/com.example.cherryjp.scoresexample E/SQLiteLog: (1) no such column: _id

I cannot figure out why this is. Can anybody help? Here is my code for the SQLite adapter:

package com.example.cherryjp.scoresexample;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.lang.Override;
import java.lang.String;
import java.lang.StringBuilder;

public class ScoreDataAdapter {
    // Becomes the filename of the database
    private static final String DATABASE_NAME = "scores.db";
    // Only one table in this database
    private static final String TABLE_NAME = "scores";
    // We increment this every time we change the database schema which will
    // kick off an automatic upgrade
    private static final int DATABASE_VERSION = 1;
    // TODO: Implement a SQLite database

    public static final String KEY_ID = "_id";
    public static final String KEY_NAME = "_name";
    public static final String KEY_SCORE = "_score";
    private static final String DROP_STATEMENT = "DROP TABLE IF EXISTS " + TABLE_NAME;
    private static final String CREATE_STATEMENT;
    static {
        StringBuilder sb = new StringBuilder();
        sb.append("CREATE TABLE " + TABLE_NAME + " (");
        sb.append(KEY_ID + " integer primary key autoincrement, ");
        sb.append(KEY_NAME + "text, ");
        sb.append(KEY_SCORE + "integer");
        sb.append(")");
        CREATE_STATEMENT = sb.toString();
    }

    private SQLiteOpenHelper mOpenHelper;
    private SQLiteDatabase mDatabase;

    public ScoreDataAdapter(Context context) {
        //Create SQLiteOpenHelper
        mOpenHelper = new ScoreDbHelper(context);
    }

    public void open() {
        //open database
        mDatabase = mOpenHelper.getWritableDatabase();

    }

    public void close() {
        //close db
        mDatabase.close();
    }

    public long addScore(Score score) {
        ContentValues row = getContentValuesfromScore(score);
        long newId = mDatabase.insert(TABLE_NAME, null, row);
        score.setId(newId);
        return 1L;
    }

    private ContentValues getContentValuesfromScore(Score score) {
        ContentValues row = new ContentValues();
        row.put(KEY_NAME, score.getName());
        row.put(KEY_SCORE, score.getScore());
        return null;
    }


    public Cursor getScoresCursor() {
        String[] projection = new String[] {KEY_ID, KEY_NAME, KEY_SCORE };
        return mDatabase.query(TABLE_NAME, projection, null, null, null, null, KEY_SCORE + "DESC");

    }


    private static class ScoreDbHelper extends SQLiteOpenHelper {

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

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(CREATE_STATEMENT);

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.d(ScoresListActivity.SLS, "Upgrading the database from revision " + oldVersion + " to " + newVersion);
            db.execSQL(DROP_STATEMENT);
            onCreate(db);
        }
    }
}

Thanks in advance, John

Aucun commentaire:

Enregistrer un commentaire