Error when using a final static String to construct a SQL query

0

Good evening person, I've been searching around and I know what to do to get CRUD organized ... I know that the programmer creates a class with the strings, leaving for example public static final String NAME_TABLE = "nome"; and so on, then la in the DBHelper class you call NAME_TABLE - "CREATE TABLE " + NAME_TABLE + "....

This is done to not have a typo in the future, because when you need to do an insert or update you just call the name of the column or table you need ... in summary I did a class string and another DBHelper

When listing my data in the activity it gives me an error saying

Caused by: android.database.sqlite.SQLiteException: near "FROM": syntax error (code 1):, while compiling: SELECT FROM result

I do not know how I can proceed with this organization, because I do not know what to call it right, instead of collocating NAME_TABLE put getNameTable() and it seems that it is not recognizing

class strings

package DataModel;

public class DataModel {

    private static final String DB_NAME = "Resultados";
    private static final String TABLE_NAME = "resultado";
    private static final String ID = "id";
    private static final String TIME_CASA = "TimeCasa";
    private static final String TIME_FORA = "TimeFora";

    public static String getDbName() {
        return DB_NAME;
    }

    public static String getTableName() {
        return TABLE_NAME;
    }

    public static String getID() {
        return ID;
    }

    public static String getTimeCasa() {
        return TIME_CASA;
    }

    public static String getTimeFora() {
        return TIME_FORA;
    }
}

DBHelper

package Base;

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;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import DataModel.DataModel;

import static DataModel.DataModel.getGolsCasa;
import static DataModel.DataModel.getGolsFora;
import static DataModel.DataModel.getID;
import static DataModel.DataModel.getTableName;


public class DbHelper extends SQLiteOpenHelper{

    private static final String NAME_BASE = "Resultados";
    private static final int VERSION_BASE = 1;

    public DbHelper(Context context) {

        super(context, NAME_BASE, null, VERSION_BASE);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sqlCreateTableResultado = "CREATE TABLE"+ getTableName() +"("
                + getID()+ "INTEGER PRIMARY KEY AUTOINCREMENT,"
                + getGolsCasa()+ "TEXT,"
                + getGolsFora()+"TimeFora TEXT,"
                + "JogadoresCasa TEXT,"
                + "JogadoresFora TEXT,"
                + "GolsCasa INTEGER,"
                + "CartaoVermelho INTEGER,"
                + "CartaoAmarelo INTEGER,"
                + "GolsFora INTEGER" + ")";

        db.execSQL(sqlCreateTableResultado);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sqlDropTableResultado = "DROP TABLE " + getTableName();

        db.execSQL(sqlDropTableResultado);

        onCreate(db);

    }

public List<Esporte> selectTodosResult() {
        List<Esporte> listResult = new ArrayList<Esporte>();
        SQLiteDatabase db = getReadableDatabase();

        String sqlSelectTodosResult = "SELECT FROM " + getTableName();

        Cursor c = db.rawQuery(sqlSelectTodosResult, null);

        try {
            if (c != null && c.moveToFirst()) {
                c.moveToFirst();
                do {
                    Esporte inserirBanco = new Esporte();
                    inserirBanco.setId(c.getInt(0));
                    inserirBanco.setNomeTimeUm(c.getString(c.getColumnIndexOrThrow("TimeCasa")));
                    inserirBanco.setNomeTimeDois(c.getString(c.getColumnIndexOrThrow("TimeFora")));

                    listResult.add(inserirBanco);

                } while (c.moveToNext());
}
        }
    }catch (SQLException or){
        or.printStackTrace();
    }
    db.close();
    return listResult;
}

Someone could help me by passing some hints or something related, I just can not get the name of another class and inserting in these assignments concatenating. Thank you ....

    
asked by anonymous 20.06.2017 / 06:39

2 answers

2

The problem is not in the use of constants but in the query syntax, which is wrong. Between SELECT and FROM it is necessary to indicate the name of the columns that the select should contain or * if you want them to contain all.

String sqlSelectTodosResult = "SELECT * FROM " + DataModel.TABLE_NAME;
    
20.06.2017 / 10:45
0

The error is in your SQL string. You are not passing any parameters in the SELECT clause. It has to be:

SELECT <coluna1, coluna2, etc> FROM <tabela>

or

SELECT * FROM <tabela>

to return all the columns of the table.

    
20.06.2017 / 12:22