1
What am I doing wrong in this class of Sqlite ? 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 and the class of Sqlite :
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("");
}
}
COLUMN_DISCIPLINA
has what value?– Isac
It has the value that the user puts in Mainactivity. Well maybe it is better to post mainActivity
– Emanuel Sobreiro
It is not necessary. Just know what value he is receiving
– Isac
I didn’t realize, I’m a little new on android
– Emanuel Sobreiro
Enter here the error that is giving, as well as the line, so I know what is the problem you have at the moment
– Isac
07-09 15:46:14.072 24048-24048/com.example.sqllitetest E/Androidruntime: FATAL EXCEPTION: main Process: com.example.sqllitetest, PID: 24048 java.lang.Illegalstateexception: Couldn’t read Row 0, col -1 from Cursorwindow. Make sure the Cursor is initialized correctly before accessing data from it.
– Emanuel Sobreiro
This is the error, I’m not having error in the program but this exception
– Emanuel Sobreiro
c.getString(c.getColumnIndex("disciplina"))
tries to get the value for the fielddisciplina
that doesn’t exist. yours is called_disciplina
. Do it firstc.getString(c.getColumnIndex(COLUMN_DISCIPLINA ))
and the same goes for theprintData
– Isac
I did it no longer from error but the application gets all white and on loop without errors! Thank you for all your work and patience with me !
– Emanuel Sobreiro
No problem, it turns out it had several mistakes so it is always longer to be able to correct all. But this was one of them, the fields to be searched did not match the ones in the tables. But does it give an error? appears something in the log? I also advise you to try debugging and see if each method is picking up the desired values. And I also see that you do not have
c.moveToFirst()
in the edited code– Isac
Not in Logcat is only doing Waiting for a blocking GC Objectsallocated , infinitely
– Emanuel Sobreiro
I’ve been able to make it work but it gets a lot of fuss and it works because it was just a test for a larger app that needs SQL to work.
– Emanuel Sobreiro
If it already works this already in the right way. Now is to go modifying to stay the way you want. Rest of good programming :)
– Isac
Thank you very much vlw! Much better tutor than those with many arrogant points! I wish you a lot of luck here at Stackoverflow
– Emanuel Sobreiro