Error when viewing or adding SQL data

Asked

Viewed 112 times

1

Good afternoon guys, I was trying to make an sql to put the score obtained in a game I’m developing, but when I put to save the score, or even see the score it gives error, I will put an asterisk on the line where Android Studio points the error.

Criabanco

public class CriadorBanco extends SQLiteOpenHelper {

    static final String NOME_BANCO = "pont.db";
    static final String TABELA = "pontuacoes";
    static final String ID = "_id";
    static final String GAMER = "player";
    static final String DESAFIO = "desafio";
    static final String PONTUACAO = "pontos";
    static final int VERSAO = 1;

    public CriadorBanco (Context context){
        super(context, NOME_BANCO, null, VERSAO);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE" + TABELA + "("
                + ID + "integer primary key autoincrement,"
                + GAMER + "text,"
                + DESAFIO + "text,"
                + PONTUACAO + "text,"
                +")";
        *db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS" + TABELA);
        onCreate(db);
    }
}

Activity Score

public class Pontuacao extends AppCompatActivity {

    private ListView pontuacaodesafio1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pontuacao);

        ControladorBanco crud = new ControladorBanco(getBaseContext());
        *Cursor cursor = crud.carregaDados();

        String[] nomeCampos = new String[] {CriadorBanco.ID, CriadorBanco.GAMER, CriadorBanco.DESAFIO, CriadorBanco.PONTUACAO};
        int[] idViews = new int[] {R.id.idGamer, R.id.Gamer, R.id.NDesafio, R.id.Pontuacao};

        SimpleCursorAdapter adaptador = new SimpleCursorAdapter(getBaseContext(),
                R.layout.pontuacao_layout,cursor,nomeCampos,idViews, 0);
        pontuacaodesafio1 = (ListView)findViewById(R.id.pontuacaodesafio1);
        pontuacaodesafio1.setAdapter(adaptador);
    }
}

Controller

public class ControladorBanco {

    private SQLiteDatabase db;
    private CriadorBanco banco;

    public ControladorBanco (Context context){
        banco = new CriadorBanco(context);
    }

    public String insereDado(String player, String desafio, String pontos){
        ContentValues valores;
        long resultado;

        db = banco.getWritableDatabase();
        valores = new ContentValues();
        valores.put(CriadorBanco.GAMER, player);
        valores.put(CriadorBanco.DESAFIO, desafio);
        valores.put(CriadorBanco.PONTUACAO, pontos);

        resultado = db.insert(CriadorBanco.TABELA, null, valores);
        db.close();

        if (resultado ==-1)
            return "Tente Novamente";
        else
            return "Pontuação Salva";
    }

    public Cursor carregaDados(){
        Cursor cursor;
        String[] campos = {banco.ID, banco.GAMER, banco.DESAFIO, banco.PONTUACAO};
        *db = banco.getReadableDatabase();
        cursor = db.query(banco.TABELA, campos, null, null, null, null, null);

        if(cursor!=null){
            cursor.moveToFirst();
        }
        db.close();
        return cursor;
    }
}

The mistake:

E/AndroidRuntime: FATAL EXCEPTION: main
          Process: br.com.mscp.desafio, PID: 2435
          java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.mscp.desafio/br.com.mscp.desafio.Pontuacao}: android.database.sqlite.SQLiteException: near "TABLEpontuacoes": syntax error (code 1): , while compiling: CREATE TABLEpontuacoes(_idinteger primary key autoincrement,playertext,desafiotext,pontostext,)
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
              at android.app.ActivityThread.-wrap12(ActivityThread.java)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:154)
              at android.app.ActivityThread.main(ActivityThread.java:6119)
              at java.lang.reflect.Method.invoke(Native Method)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
           Caused by: android.database.sqlite.SQLiteException: near "TABLEpontuacoes": syntax error (code 1): , while compiling: CREATE TABLEpontuacoes(_idinteger primary key autoincrement,playertext,desafiotext,pontostext,)
              at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
              at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
              at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
              at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
              at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
              at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
              at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1677)
              at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
              at br.com.mscp.desafio.CriadorBanco.onCreate(CriadorBanco.java:29)
              at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
              at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
              at br.com.mscp.desafio.ControladorBanco.carregaDados(ControladorBanco.java:39)
              at br.com.mscp.desafio.Pontuacao.onCreate(Pontuacao.java:18)
              at android.app.Activity.performCreate(Activity.java:6679)
              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
              at android.app.ActivityThread.-wrap12(ActivityThread.java) 
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
              at android.os.Handler.dispatchMessage(Handler.java:102) 
              at android.os.Looper.loop(Looper.java:154) 
              at android.app.ActivityThread.main(ActivityThread.java:6119) 
              at java.lang.reflect.Method.invoke(Native Method) 
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Application terminated.

1 answer

0


The sql command that creates the table is poorly constructed.
Missing spaces between some elements.

To prevent this situation always include a space after and before quotation marks

@Override
public void onCreate(SQLiteDatabase db) {
    String sql = "CREATE TABLE " + TABELA + " ( "
            + ID + " integer primary key autoincrement, "
            + GAMER + " text, "
            + DESAFIO + " text, "
            + PONTUACAO + " text "
            + " )";
    db.execSQL(sql);
}
  • 1

    Thank you, it worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.