0

在我的数据库中,我有表 AndroidGameMode,但是当我尝试将一些数据插入表中时,我收到此错误,表示我的列之一不存在

    package com.example.jogodogalo.jogador1;

import java.util.ArrayList;

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

public class DataBaseHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_NAME = "JogoDoGaloDB";

    private static final String TABLE_RESULTS = "AndroidGameMode";

    private static final String KEY_ID = "PlayerID";
    private static final String KEY_PLAYER = "PlayerName";
    private static final String KEY_WINS = "PlayerWins";
    private static final String KEY_TIES = "PlayerTies";
    private static final String KEY_LOSSES = "PlayerLosses";
    private static final String KEY_POINTS = "PlayerPoints";

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_RESULTS_TABLE = "CREATE TABLE " + TABLE_RESULTS +"("
                + KEY_ID + " INTEGER PRIMARY KEY,"
                + KEY_PLAYER + " TEXT,"
                + KEY_WINS + " INTEGER,"
                + KEY_TIES + " INTEGER," 
                + KEY_LOSSES + " INTEGER," 
                + KEY_POINTS + "INTEGER" + ")";
        db.execSQL(CREATE_RESULTS_TABLE);
    }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            //"Mata" a outra tabela se existir
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_RESULTS);

            //cria as tabelas de novo
            onCreate(db);
        }
        //podes ir po logcat e marcar aquele erro ? marcar erro?

        public void addResults(Results results){
            SQLiteDatabase db = this.getWritableDatabase();

            ContentValues values = new ContentValues();
            values.put(KEY_PLAYER, results.getName());
            values.put(KEY_WINS, results.getWins());
            values.put(KEY_TIES, results.getTies());
            values.put(KEY_LOSSES, results.getLosses());
            values.put(KEY_POINTS, results.getPoints());
            //Pede ajuda daquele gajo ok? Dizes que inseres mas nao aparece nada e mostras as funçoes... dizes que nao e preciso nem editar nem apagar ok
            db.insert(TABLE_RESULTS, null,values);
            db.close();
        }

        public float getSumPoints() {
            float result = 0;
            SQLiteDatabase db = this.getWritableDatabase();

            Cursor c = db.query(TABLE_RESULTS, 
              new String[] { "sum(" + KEY_POINTS + ")" },
              null, null, null, null, null);
            if (c.moveToFirst()) {
                result = c.getLong(0);
            }
            c.close();
            return result;
        }

        public Results getResults(int id){
            SQLiteDatabase db = this.getReadableDatabase();

            Cursor cursor = db.query(TABLE_RESULTS, new String[]{KEY_ID, KEY_PLAYER,
                    KEY_WINS, KEY_TIES, KEY_LOSSES, KEY_POINTS}, KEY_ID + "=?", new String[] {String.valueOf(id)}, 
                null, null, null, null);

            if (cursor != null) cursor.moveToFirst();

            Results Result = new Results(Integer.parseInt(cursor.getString(0)), 
                        cursor.getString(1), 
                        Integer.parseInt(cursor.getString(2)),
                        Integer.parseInt(cursor.getString(3)),
                        Integer.parseInt(cursor.getString(4)), 
                        Integer.parseInt(cursor.getString(5)));         

                return Result;
        }



        public ArrayList<Results> getAllResults(){
            ArrayList<Results> ResultsList = new ArrayList<Results>();

            SQLiteDatabase db = this.getWritableDatabase();

            Cursor cursor = db.query(TABLE_RESULTS, null, null, null, null, null, null); 

            if(cursor.moveToFirst()){
                do{
                    Results Result = new Results();
                    Result.setID(Integer.parseInt(cursor.getString(0)));
                    Result.setName(cursor.getString(1));
                    Result.setWins(Integer.parseInt(cursor.getString(2)));
                    Result.setTies(Integer.parseInt(cursor.getString(3)));
                    Result.setLosses(Integer.parseInt(cursor.getString(4)));
                    Result.setPoints(Integer.parseInt(cursor.getString(5)));

                    ResultsList.add(Result);
                }while(cursor.moveToNext());
            }
            return ResultsList;
        }

}

那是我的 databaseHandler 类

这是我的ResultClass

package com.example.jogodogalo.jogador1;

public class Results {

    int _id;
    String _name;
    int _wins;
    int _ties;
    int _losses;
    int _points;

    public Results(){

    }

    public Results (int id, String name, int wins, int ties, int losses, int points){
        this._id = id;
        this._name = name;
        this._wins = wins;
        this._losses = losses;
        this._ties = ties;
        this._points=points;
    }

    public Results (String name, int wins, int ties, int losses, int points){
        this._name = name;
        this._wins = wins;
        this._ties = ties;
        this._losses = losses;
        this._points = points;
    }

    public Results(int points){
        this._points = points;
    }

    public int getID(){
        return this._id;
    }

    public void setID(int id){
        this._id = id;
    }

    public String getName(){
        return this._name;
    }

    public void setName(String name){
        this._name = name;
    }

    public int getWins(){
        return this._wins;
    }

    public void setWins(int wins){
        this._wins = wins;
    }

    public int getTies(){
        return this._ties;
    }

    public void setTies(int ties){
        this._ties = ties;
    }

    public int getLosses(){
        return this._losses;
    }

    public void setLosses(int losses){
        this._losses = losses;
    }

    public int getPoints(){
        return this._points;
    }
    //ta mal, quando se faz um set tu nao retornas nada
    public void setPoints(int points){
        this._points = points;
    }

    @Override
    public String toString() {
        return this.getName();
    }

}

我的插入代码

 public void onClick(DialogInterface dialog, int whichButton) {             

                    String name = mPlayerName.getText().toString();

                    String win = mPlayerCount.getText().toString();
                    int wins_jogador1 = Integer.parseInt(win);

                    String ties = mTieCount.getText().toString();
                    int ties_jogador1 = Integer.parseInt(ties);

                    String losses = mAndroidCount.getText().toString();
                    int losses_jogador1 = Integer.parseInt(losses);

                    String pontuacao = mPointsCount.getText().toString();
                    int pontuaçao_jogador1 = Integer.parseInt(pontuacao);

                    if(pontuaçao_jogador1 == 0){
                        Toast myToast = Toast.makeText(getApplicationContext(), "Não tem qualquer pontuação. \nNão é possível guardar.",
                                Toast.LENGTH_LONG);
                    myToast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 0, 100);
                    myToast.setDuration(3000);
                    myToast.show();

                    }else if(pontuaçao_jogador1 != 0){

                    DataBaseHandler db  = new DataBaseHandler(getApplicationContext());
                    Results theResultados = new Results(name, wins_jogador1, ties_jogador1, losses_jogador1, pontuaçao_jogador1);

                    db.addResults(theResultados);
                    //que estranho :S ele teima com o playerPoints nao sei porque diz que a cioluna nao existe

                    Intent intent = new Intent(JogoDoGalo.this, MainMenu.class);
                    JogoDoGalo.this.startActivity(intent);
4

1 回答 1

2

您的 onCreate 代码中缺少空格:

+ KEY_POINTS + "INTEGER" + ")";

将其更改为:

 + KEY_POINTS + " INTEGER" + ")"; 

更新

还尝试使用ALTER如下命令拆分列创建命令:

@Override
 public void onCreate(SQLiteDatabase db) {
     String CREATE_RESULTS_TABLE = "CREATE TABLE " + TABLE_RESULTS+" ("
             + KEY_ID + " INTEGER PRIMARY KEY,"
             + KEY_PLAYER + " TEXT,"
             + KEY_WINS + " INTEGER,"
             + KEY_TIES + " INTEGER,"
              + KEY_LOSSES + " INTEGER"+")";
     db.execSQL(CREATE_RESULTS_TABLE);

     String ALTER_RESULTS_TABLE = "ALTER TABLE " + TABLE_RESULTS
             +" ADD "+ KEY_POINTS + " INTEGER";
     db.execSQL(ALTER_RESULTS_TABLE);
 } 
于 2012-09-11T10:58:39.860 回答