Error in SQLite class

1

What am I doing wrong in this SQLite class? I'm getting this error:

   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqllitetest/com.example.sqllitetest.MainActivity}: android.database.sqlite.SQLiteException: near "1": syntax error (code 1): , while compiling: SELECT _dataFROMtestsWHERE 1   

This is the SQLite class:

public class MyDBHandler extends SQLiteOpenHelper {

public static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "tests.db";
public static final String TABLE_TESTS = "tests";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_DATA = "_data";
public static final String COLUMN_DISCIPLINA = "_disciplina";


public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    String query = "CREATE TABLE " + TABLE_TESTS  + " ( "+
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
            COLUMN_DATA + " TEXT ," +
            COLUMN_DISCIPLINA + " TEXT "
            +");";
    sqLiteDatabase.execSQL(query);

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS" + TABLE_TESTS);
    onCreate(sqLiteDatabase);

}

public void addTest (Testes test){
    ContentValues values = new ContentValues();
    values.put(COLUMN_DATA,test.get_data());
    values.put(COLUMN_DISCIPLINA,test.get_disciplina());
    SQLiteDatabase sqLiteDatabase = getWritableDatabase();
    sqLiteDatabase.insert(TABLE_TESTS,null,values);
    sqLiteDatabase.close();



}

public void deleteTest ( String data, String disciplina){
    SQLiteDatabase sqLiteDatabase = getWritableDatabase();
    sqLiteDatabase.execSQL("DELETE FROM " + TABLE_TESTS + " WHERE" + COLUMN_DATA + "=\""  + data + "\"" + "OR" + COLUMN_DISCIPLINA + "=\"" + disciplina +"\";" );
}

public String printDisciplina (){
    String dbString = "";
    SQLiteDatabase sqLiteDatabase = getWritableDatabase();
    String query = "SELECT " + COLUMN_DISCIPLINA + " FROM " + TABLE_TESTS + " WHERE 1";


    Cursor c = sqLiteDatabase.rawQuery(query,null);

    while (!c.isAfterLast()){
        if(c.getString(c.getColumnIndex("disciplina"))!=null){
            dbString += c.getString(c.getColumnIndex("disciplina"));
            dbString += "\n";
        }


    }
    sqLiteDatabase.close();
    return dbString;
}
public String printData (){
    String dbString = "";
    SQLiteDatabase sqLiteDatabase = getWritableDatabase();
    String query = "SELECT " + COLUMN_DATA + "FROM" + TABLE_TESTS + "WHERE 1";


    Cursor c = sqLiteDatabase.rawQuery(query,null);

    while (!c.isAfterLast()){
        if(c.getString(c.getColumnIndex("data"))!=null){
            dbString += c.getString(c.getColumnIndex("data"));
            dbString += "\n";
        }


    }
    sqLiteDatabase.close();
    return dbString;
}

}

Main activity:

  public class MainActivity extends AppCompatActivity {

TextView textDisciplina;
TextView textData;
EditText disciplina;
EditText data;
Button addButton;
Button removeButton;
MyDBHandler dbHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textDisciplina = (TextView) findViewById(R.id.textDisciplina);
    textData = (TextView) findViewById(R.id.textData);
    disciplina = (EditText) findViewById(R.id.disciplina);
    data = (EditText) findViewById(R.id.data);
    addButton = (Button) findViewById(R.id.addButton);
    removeButton = (Button) findViewById(R.id.removeButton);
    dbHandler = new MyDBHandler(MainActivity.this,null,null,1);


    addButton.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v){
            Testes teste = new Testes(data.getText().toString(),disciplina.getText().toString());
            dbHandler.addTest(teste);
            printDataBase();
        }

    });

    removeButton.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v){
            String data_input = data.getText().toString();
            String disciplina_input = data.getText().toString();
            dbHandler.deleteTest(data_input,disciplina_input);
            printDataBase();
        }
    });

    printDataBase();
}



public void printDataBase(){
    String date = dbHandler.printData();
    String discipline = dbHandler.printDisciplina();
    textDisciplina.setText(discipline);
    textData.setText(date);
    data.setText("");
    disciplina.setText("");

}

}

    
asked by anonymous 09.07.2017 / 16:23

2 answers

2

Looking at the error message we see that SQL is all together:

SELECT _dataFROMtestsWHERE 1

When should it be:

SELECT _data FROM tests WHERE 1

This query comes from:

String query = "SELECT " + COLUMN_DATA + "FROM" + TABLE_TESTS + "WHERE 1";

What should become:

public String printData (){
    ...
    String query = "SELECT " + COLUMN_DATA + " FROM " + TABLE_TESTS + " WHERE 1";

Look closely at the spaces between the words FROM and WHERE .

However, the WHERE has to be corrected because it must be WHERE campo = 1 . You should now see which field has the value you want it to have 1.

    
09.07.2017 / 16:29
0

It has to be by a space in and from where, if n gives syntax error because when it goes to execute the command it tries to read everything together.

Where you're wrong:

SQLiteDatabase sqLiteDatabase = getWritableDatabase (); String WHERE 1 ;

Correct:

public String printData (){
    String dbString = "";
    SQLiteDatabase sqLiteDatabase = getWritableDatabase();
    String query = "SELECT " + COLUMN_DATA + " FROM " + TABLE_TESTS + " WHERE 1";
    
09.07.2017 / 16:31