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("");
}
}