Error using a Static final String to build an SQL query

Asked

Viewed 245 times

0

Good evening person, I’ve been searching around and I know that you can do 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 there in the Dbhelper class you call the NAME_TABLE - "CREATE TABLE " + NAME_TABLE + "....

This is done to not have typo error in the future, because when you need to do an Insert or update Oce only calls the name of the column or table you need... in short I made a string class and another Dbhelper

when listing my data on Activity it presents me with 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 how to call it right, instead of collecting NAME_TABLE placed getNameTable() and it seems he’s 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 giving me some hint or something related, I’m just not getting the name of another class and entering these concatenating assignments. Thank you....

2 answers

2


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

String sqlSelectTodosResult = "SELECT * FROM " + DataModel.TABLE_NAME;

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 table columns.

Browser other questions tagged

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