Error in Sqlite class

Asked

Viewed 46 times

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?

  • It has the value that the user puts in Mainactivity. Well maybe it is better to post mainActivity

  • It is not necessary. Just know what value he is receiving

  • I didn’t realize, I’m a little new on android

  • Enter here the error that is giving, as well as the line, so I know what is the problem you have at the moment

  • 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.

  • This is the error, I’m not having error in the program but this exception

  • c.getString(c.getColumnIndex("disciplina")) tries to get the value for the field disciplina that doesn’t exist. yours is called _disciplina. Do it first c.getString(c.getColumnIndex(COLUMN_DISCIPLINA )) and the same goes for the printData

  • 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 !

  • 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

  • Not in Logcat is only doing Waiting for a blocking GC Objectsallocated , infinitely

  • 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.

  • 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 :)

  • Thank you very much vlw! Much better tutor than those with many arrogant points! I wish you a lot of luck here at Stackoverflow

Show 9 more comments

2 answers

2


Observing the error message we see that SQL is all together:

SELECT _dataFROMtestsWHERE 1

When it should be:

SELECT _data FROM tests WHERE 1

This consultation shall be based on:

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

That must become:

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

Notice the spaces placed between the words FROM and WHERE.

However, WHERE still needs to be corrected because it needs to be WHERE campo = 1. You now need to see which field has the value of whatever has 1.

  • Thank you very much solved the problem! But now I’m having this error: android.database.Cursorindexoutofboundsexception: Index -1 requested, with a size of 1

  • Foul c.moveToFirst() before the while to ensure you start searching at the first value

  • now give me this error : Couldn’t read Row 0, col -1 from Cursorwindow. Make sure the Cursor is initialized correctly before accessing data from it

  • What is the error ? Cursorindexoutofboundsexception ? in which line. And confirm that you corrected Where as well because I suspect your query is not returning any results. To find out if you have results you can use c.getCount() which tells you how many lines have been obtained

  • I entered "WHERE" + "COLUMN_ID" + "=" + "1"

  • Mple.sqllitetest E/Cursorwindow: Failed to read Row 0, column -1 from a Cursorwindow which has 5 Rows, 1 Columns. This is the error that gives me .

  • I will update the response to contemplate all these facts.

  • Okay thanks, I’ve already edited the question for what I put.

Show 3 more comments

0

You have to put a space in the from and Where, if n gives syntax error because when it will execute the command it tries to read everything together.

where you’re wrong:

Sqlitedatabase sqLiteDatabase = getWritableDatabase(); String query = "SELECT " + COLUMN_DATA + "FROM" + TABLE_TESTS + "WHERE 1";

Correct:

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

Browser other questions tagged

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