Saransh Agarwal Saransh Agarwal - 6 months ago 10
Android Question

Database reads same data multiple times

This is the database file

public class MyDBHandler extends SQLiteOpenHelper {
public static final int DATABASE_VERSION=1;
public static final String DATABASE_NAME="mydetails.db";
public static final String TABLE_MYDETAIL="mydetails";
public static final String COLUMN_NAME="_name";
public static final String COLUMN_ROLLNUMBER="_rollNumber";
public static final String COLUMN_MARKS="_marks";
public static final String COLUMN_ID="_id";

public MyDBHandler(Context context,String name, SQLiteDatabase.CursorFactory cursorFactory,int version){
super(context,DATABASE_NAME,cursorFactory,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query ="CREATE TABLE "+TABLE_MYDETAIL+"("+COLUMN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+COLUMN_ROLLNUMBER+" INTEGER, "+ COLUMN_NAME+" TEXT, "+COLUMN_MARKS +" INTEGER"+");";
Log.d("query",query);
db.execSQL(query);

}
public void addMyDetail(MyDetails myDetails){
ContentValues values= new ContentValues();
values.put(COLUMN_ROLLNUMBER,myDetails.get_rollNumber());
values.put(COLUMN_NAME,myDetails.get_name());
values.put(COLUMN_MARKS,myDetails.get_marks());
SQLiteDatabase db = getReadableDatabase();
long id= db.insert(TABLE_MYDETAIL, null, values);
Log.d("id", "addMyDetail: "+id);
db.close();
}
public void deleteMyDetail(String myName){
SQLiteDatabase db = getReadableDatabase();
db.execSQL("DELETE FROM " + TABLE_MYDETAIL + " WHERE " + COLUMN_NAME + "=\"" + myName + "\";");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS"+TABLE_MYDETAIL);
onCreate(db);
}
public String databaseToString(){
String dbString="";
SQLiteDatabase db =getWritableDatabase();
String query="SELECT * FROM " + TABLE_MYDETAIL + " WHERE 1" ;
Cursor c =db.rawQuery(query, null);
c.moveToFirst();
while(!c.isAfterLast()){
if(c.getString(c.getColumnIndex("_marks"))!=null){
dbString+= c.getString(c.getColumnIndex(COLUMN_ROLLNUMBER));
dbString+=c.getString(c.getColumnIndex(COLUMN_NAME));
dbString+=c.getString(c.getColumnIndex(COLUMN_MARKS));
dbString += "\n";
}
c.moveToNext();
}
c.close();
db.close();
return dbString;
}
}


This is my MainActivity

package com.example.saranshagarwal.studentdetailsapp;


public class MainActivity extends AppCompatActivity {
EditText name,rollnumber,marks;
MyDBHandler myDBHandler;
TextView tv2;
final Context context=this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.editText2);
rollnumber = (EditText) findViewById(R.id.editText5);
marks = (EditText) findViewById(R.id.editText4);
tv2=(TextView)findViewById(R.id.textView5);
myDBHandler=new MyDBHandler(this,null,null,1);

}
public void addButtonClicked(View view){
MyDetails myDetails=new MyDetails();
myDetails.set_rollNumber(rollnumber.getText().toString());

myDetails.set_name(marks.getText().toString());
myDetails.set_marks(marks.getText().toString());
myDBHandler.addMyDetail(myDetails);
String dbString= myDBHandler.databaseToString();
tv2.setText(dbString);
name.setText("");
rollnumber.setText("");
marks.setText("");
}
public void deleteButtonClicked(View view) {
String inputText = name.getText().toString();
myDBHandler.deleteMyDetail(inputText);

}
public void onViewClicked(View view){
Dialog dialog= new Dialog(context);
dialog.setContentView(R.layout.popup_on_view);
TextView tv1=(TextView)findViewById(R.id.popup_text);
String dbString= myDBHandler.databaseToString();
tv1.setText(dbString);

dialog.show();
}
}


Now when I click on the add Button the text is displayed of the name twice and marks once . What is the error.Please help.

I am new to programming please help.

afterEdit

Answer

You are adding marks in place of name in your code

Change your code to this

        myDetails.set_rollNumber(rollnumber.getText().toString());
        myDetails.set_name(name.getText().toString());
        myDetails.set_marks(marks.getText().toString());

Maybe the reason is because of the way you are accessing the database. Make a global variable like this-

private SQLiteDatabase db;

Initialize this in your constructor MyDBHandler() like this

db=context.openOrCreateDatabase(DATABASE_NAME,DATABASE_VERSION,null);

Then use it in each of subsequent methods.

Also you will need to explicitly close the database which you can do by creating a method in your MyDBHandler class.

public void close() throws SQLException {
        db.close();
    }

And call this once your work with db is done. Remove the db.close() statements from other methods.

Also you need to implement this in your activity for each method like this

public void addButtonClicked(View view){

    MyDetails myDetails=new MyDetails();
    myDetails.set_rollNumber(rollnumber.getText().toString());

    myDetails.set_name(marks.getText().toString());
    myDetails.set_marks(marks.getText().toString());

    myDBHandler=new MyDBHandler(this,null,null,1);
    myDBHandler.addMyDetail(myDetails);

    String dbString= myDBHandler.databaseToString();
    myDBHandler.close();
    tv2.setText(dbString);
    name.setText("");
    rollnumber.setText("");
    marks.setText("");
}

Hope this helps :)