I'm having some issues in fetching data from SQLite database












-2














I'm creating a normal android application in which I'm trying to fetch data using Sqlite database but the values is not being retrieved please tell me where I'm going wrong?



activity_main.xml



   <Button
android:id="@+id/button_viewAll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View Details"
/>


DatabaseHelper Class



   public static final String DATABASE_NAME = "Gym.db";
public static final String TABLE_NAME = "Exercise";
public static final String COL_1 = "exercise_id";
public static final String COL_2 = "exercise_name";
public static final String COL_3 = "body_part";
public static final String COL_4 = "description";

public DatabaseHelper( Context context) {
super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL("create table " + TABLE_NAME +" (exercise_id INTEGER PRIMARY KEY AUTOINCREMENT,exercise_name TEXT,body_part TEXT,description TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME, null);
return res;
}
}


MainActivity.java



 public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
Button texercise_id, texercise_name, tbody_part, tdescription;

Button btnviewAll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);

texercise_id = (Button)findViewById(R.id.button_viewAll);
texercise_name = (Button)findViewById(R.id.button_viewAll);
tbody_part = (Button)findViewById(R.id.button_viewAll);
tdescription = (Button)findViewById(R.id.button_viewAll);
btnviewAll = (Button)findViewById(R.id.button_viewAll);
viewAll();
}
public void viewAll() {
btnviewAll.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {

Cursor res = myDb.getAllData();
if(res.getCount() == 1) {
// show message
showMessage("Error","Nothing found");
return;
}

StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("exercise_id :"+ res.getString(0)+"n");
buffer.append("exercise_name :"+ res.getString(1)+"n");
buffer.append("body_part :"+ res.getString(2)+"n");
buffer.append("description :"+ res.getString(3)+"nn");
}

// Show all data
showMessage("Data Found",buffer.toString());
}
}
);
}
public void showMessage(String title,String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}


}


I've tried this before also but this same issue I'm facing with retrieving data from SQLite Database.










share|improve this question
























  • How much data your db contains? Can you also add your method for inserting values into db?
    – Manmohan
    Dec 26 '18 at 17:39










  • I didn't understand please explain.
    – Huzaifa Yusuf
    Dec 26 '18 at 17:44










  • The question from @Manmohan was how many rows of data do you currently have in the "Exercise" Table?? It seems odd that your code would have if(res.getCount() == 1) indicating that the Database is empty! :: Since when is a return of one row of data constitute an empty database table?
    – Barns
    Dec 26 '18 at 17:50












  • I have 4 rows in my database and they are: 1. exercise_id 2. exercise_name 3. body_part 4. description
    – Huzaifa Yusuf
    Dec 26 '18 at 17:53












  • Those are not rows those are columns!
    – Barns
    Dec 26 '18 at 17:57
















-2














I'm creating a normal android application in which I'm trying to fetch data using Sqlite database but the values is not being retrieved please tell me where I'm going wrong?



activity_main.xml



   <Button
android:id="@+id/button_viewAll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View Details"
/>


DatabaseHelper Class



   public static final String DATABASE_NAME = "Gym.db";
public static final String TABLE_NAME = "Exercise";
public static final String COL_1 = "exercise_id";
public static final String COL_2 = "exercise_name";
public static final String COL_3 = "body_part";
public static final String COL_4 = "description";

public DatabaseHelper( Context context) {
super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL("create table " + TABLE_NAME +" (exercise_id INTEGER PRIMARY KEY AUTOINCREMENT,exercise_name TEXT,body_part TEXT,description TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME, null);
return res;
}
}


MainActivity.java



 public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
Button texercise_id, texercise_name, tbody_part, tdescription;

Button btnviewAll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);

texercise_id = (Button)findViewById(R.id.button_viewAll);
texercise_name = (Button)findViewById(R.id.button_viewAll);
tbody_part = (Button)findViewById(R.id.button_viewAll);
tdescription = (Button)findViewById(R.id.button_viewAll);
btnviewAll = (Button)findViewById(R.id.button_viewAll);
viewAll();
}
public void viewAll() {
btnviewAll.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {

Cursor res = myDb.getAllData();
if(res.getCount() == 1) {
// show message
showMessage("Error","Nothing found");
return;
}

StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("exercise_id :"+ res.getString(0)+"n");
buffer.append("exercise_name :"+ res.getString(1)+"n");
buffer.append("body_part :"+ res.getString(2)+"n");
buffer.append("description :"+ res.getString(3)+"nn");
}

// Show all data
showMessage("Data Found",buffer.toString());
}
}
);
}
public void showMessage(String title,String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}


}


I've tried this before also but this same issue I'm facing with retrieving data from SQLite Database.










share|improve this question
























  • How much data your db contains? Can you also add your method for inserting values into db?
    – Manmohan
    Dec 26 '18 at 17:39










  • I didn't understand please explain.
    – Huzaifa Yusuf
    Dec 26 '18 at 17:44










  • The question from @Manmohan was how many rows of data do you currently have in the "Exercise" Table?? It seems odd that your code would have if(res.getCount() == 1) indicating that the Database is empty! :: Since when is a return of one row of data constitute an empty database table?
    – Barns
    Dec 26 '18 at 17:50












  • I have 4 rows in my database and they are: 1. exercise_id 2. exercise_name 3. body_part 4. description
    – Huzaifa Yusuf
    Dec 26 '18 at 17:53












  • Those are not rows those are columns!
    – Barns
    Dec 26 '18 at 17:57














-2












-2








-2







I'm creating a normal android application in which I'm trying to fetch data using Sqlite database but the values is not being retrieved please tell me where I'm going wrong?



activity_main.xml



   <Button
android:id="@+id/button_viewAll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View Details"
/>


DatabaseHelper Class



   public static final String DATABASE_NAME = "Gym.db";
public static final String TABLE_NAME = "Exercise";
public static final String COL_1 = "exercise_id";
public static final String COL_2 = "exercise_name";
public static final String COL_3 = "body_part";
public static final String COL_4 = "description";

public DatabaseHelper( Context context) {
super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL("create table " + TABLE_NAME +" (exercise_id INTEGER PRIMARY KEY AUTOINCREMENT,exercise_name TEXT,body_part TEXT,description TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME, null);
return res;
}
}


MainActivity.java



 public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
Button texercise_id, texercise_name, tbody_part, tdescription;

Button btnviewAll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);

texercise_id = (Button)findViewById(R.id.button_viewAll);
texercise_name = (Button)findViewById(R.id.button_viewAll);
tbody_part = (Button)findViewById(R.id.button_viewAll);
tdescription = (Button)findViewById(R.id.button_viewAll);
btnviewAll = (Button)findViewById(R.id.button_viewAll);
viewAll();
}
public void viewAll() {
btnviewAll.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {

Cursor res = myDb.getAllData();
if(res.getCount() == 1) {
// show message
showMessage("Error","Nothing found");
return;
}

StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("exercise_id :"+ res.getString(0)+"n");
buffer.append("exercise_name :"+ res.getString(1)+"n");
buffer.append("body_part :"+ res.getString(2)+"n");
buffer.append("description :"+ res.getString(3)+"nn");
}

// Show all data
showMessage("Data Found",buffer.toString());
}
}
);
}
public void showMessage(String title,String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}


}


I've tried this before also but this same issue I'm facing with retrieving data from SQLite Database.










share|improve this question















I'm creating a normal android application in which I'm trying to fetch data using Sqlite database but the values is not being retrieved please tell me where I'm going wrong?



activity_main.xml



   <Button
android:id="@+id/button_viewAll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View Details"
/>


DatabaseHelper Class



   public static final String DATABASE_NAME = "Gym.db";
public static final String TABLE_NAME = "Exercise";
public static final String COL_1 = "exercise_id";
public static final String COL_2 = "exercise_name";
public static final String COL_3 = "body_part";
public static final String COL_4 = "description";

public DatabaseHelper( Context context) {
super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL("create table " + TABLE_NAME +" (exercise_id INTEGER PRIMARY KEY AUTOINCREMENT,exercise_name TEXT,body_part TEXT,description TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME, null);
return res;
}
}


MainActivity.java



 public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
Button texercise_id, texercise_name, tbody_part, tdescription;

Button btnviewAll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);

texercise_id = (Button)findViewById(R.id.button_viewAll);
texercise_name = (Button)findViewById(R.id.button_viewAll);
tbody_part = (Button)findViewById(R.id.button_viewAll);
tdescription = (Button)findViewById(R.id.button_viewAll);
btnviewAll = (Button)findViewById(R.id.button_viewAll);
viewAll();
}
public void viewAll() {
btnviewAll.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {

Cursor res = myDb.getAllData();
if(res.getCount() == 1) {
// show message
showMessage("Error","Nothing found");
return;
}

StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("exercise_id :"+ res.getString(0)+"n");
buffer.append("exercise_name :"+ res.getString(1)+"n");
buffer.append("body_part :"+ res.getString(2)+"n");
buffer.append("description :"+ res.getString(3)+"nn");
}

// Show all data
showMessage("Data Found",buffer.toString());
}
}
);
}
public void showMessage(String title,String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}


}


I've tried this before also but this same issue I'm facing with retrieving data from SQLite Database.







android sqlite android-sqlite






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 27 '18 at 10:46









GianhTran

1,8811721




1,8811721










asked Dec 26 '18 at 17:12









Huzaifa Yusuf

102




102












  • How much data your db contains? Can you also add your method for inserting values into db?
    – Manmohan
    Dec 26 '18 at 17:39










  • I didn't understand please explain.
    – Huzaifa Yusuf
    Dec 26 '18 at 17:44










  • The question from @Manmohan was how many rows of data do you currently have in the "Exercise" Table?? It seems odd that your code would have if(res.getCount() == 1) indicating that the Database is empty! :: Since when is a return of one row of data constitute an empty database table?
    – Barns
    Dec 26 '18 at 17:50












  • I have 4 rows in my database and they are: 1. exercise_id 2. exercise_name 3. body_part 4. description
    – Huzaifa Yusuf
    Dec 26 '18 at 17:53












  • Those are not rows those are columns!
    – Barns
    Dec 26 '18 at 17:57


















  • How much data your db contains? Can you also add your method for inserting values into db?
    – Manmohan
    Dec 26 '18 at 17:39










  • I didn't understand please explain.
    – Huzaifa Yusuf
    Dec 26 '18 at 17:44










  • The question from @Manmohan was how many rows of data do you currently have in the "Exercise" Table?? It seems odd that your code would have if(res.getCount() == 1) indicating that the Database is empty! :: Since when is a return of one row of data constitute an empty database table?
    – Barns
    Dec 26 '18 at 17:50












  • I have 4 rows in my database and they are: 1. exercise_id 2. exercise_name 3. body_part 4. description
    – Huzaifa Yusuf
    Dec 26 '18 at 17:53












  • Those are not rows those are columns!
    – Barns
    Dec 26 '18 at 17:57
















How much data your db contains? Can you also add your method for inserting values into db?
– Manmohan
Dec 26 '18 at 17:39




How much data your db contains? Can you also add your method for inserting values into db?
– Manmohan
Dec 26 '18 at 17:39












I didn't understand please explain.
– Huzaifa Yusuf
Dec 26 '18 at 17:44




I didn't understand please explain.
– Huzaifa Yusuf
Dec 26 '18 at 17:44












The question from @Manmohan was how many rows of data do you currently have in the "Exercise" Table?? It seems odd that your code would have if(res.getCount() == 1) indicating that the Database is empty! :: Since when is a return of one row of data constitute an empty database table?
– Barns
Dec 26 '18 at 17:50






The question from @Manmohan was how many rows of data do you currently have in the "Exercise" Table?? It seems odd that your code would have if(res.getCount() == 1) indicating that the Database is empty! :: Since when is a return of one row of data constitute an empty database table?
– Barns
Dec 26 '18 at 17:50














I have 4 rows in my database and they are: 1. exercise_id 2. exercise_name 3. body_part 4. description
– Huzaifa Yusuf
Dec 26 '18 at 17:53






I have 4 rows in my database and they are: 1. exercise_id 2. exercise_name 3. body_part 4. description
– Huzaifa Yusuf
Dec 26 '18 at 17:53














Those are not rows those are columns!
– Barns
Dec 26 '18 at 17:57




Those are not rows those are columns!
– Barns
Dec 26 '18 at 17:57












1 Answer
1






active

oldest

votes


















0














From your code it appears that all is working fine. However, there does not appear to be any data added, nor a means to add data. Thus the dialog is showing no data BUT it is not showing the message that indicates no data.



1. Fix the dialog to indicate no data



To fix the Dialog showing no data you need to check to see if the Cursor has not rows rather than 1 row. So change the line :-



if(res.getCount() == 1) { 


to be



if(res.getCount() < 1) {


2. Add some data so data is displayed



To show some data you need to add some data. To add some data you could have a method, in the DatabaseHelper class that allows data to be added. For example :-



public long addData(String excercise_name, String body_part, String description) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL_2,excercise_name);
cv.put(COL_3,body_part);
cv.put(COL_4,body_part);
return db.insert(TABLE_NAME,null,cv);
}


You would then need to actually add some data, as an example of doing so, the following method could be added to your MainActivity :-



private void addSomeDataButOnlyIfNoneExists() {
if (DatabaseUtils.queryNumEntries(myDb.getWritableDatabase(),DatabaseHelper.TABLE_NAME) < 1) {
myDb.addData("Squat","Legs","Squat");
myDb.addData("Push up","Arms","Push up");
myDb.addData("Run","Legs and Arms","Rapidly put one foot in front of the other");
}
}



  • Note this will just add the above data once (the first line is checking to see if data already exists).


You could invoke the method using :-



protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
addSomeDataButOnlyIfNoneExists(); //<<<<<<<<<< ADDED

........ rest of your code


Result (with data)



using the above, running the app and clicking on the View Details button results in :-



enter image description here






share|improve this answer























  • Thank you so much it worked finally
    – Huzaifa Yusuf
    Dec 28 '18 at 12:16











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53935006%2fim-having-some-issues-in-fetching-data-from-sqlite-database%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














From your code it appears that all is working fine. However, there does not appear to be any data added, nor a means to add data. Thus the dialog is showing no data BUT it is not showing the message that indicates no data.



1. Fix the dialog to indicate no data



To fix the Dialog showing no data you need to check to see if the Cursor has not rows rather than 1 row. So change the line :-



if(res.getCount() == 1) { 


to be



if(res.getCount() < 1) {


2. Add some data so data is displayed



To show some data you need to add some data. To add some data you could have a method, in the DatabaseHelper class that allows data to be added. For example :-



public long addData(String excercise_name, String body_part, String description) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL_2,excercise_name);
cv.put(COL_3,body_part);
cv.put(COL_4,body_part);
return db.insert(TABLE_NAME,null,cv);
}


You would then need to actually add some data, as an example of doing so, the following method could be added to your MainActivity :-



private void addSomeDataButOnlyIfNoneExists() {
if (DatabaseUtils.queryNumEntries(myDb.getWritableDatabase(),DatabaseHelper.TABLE_NAME) < 1) {
myDb.addData("Squat","Legs","Squat");
myDb.addData("Push up","Arms","Push up");
myDb.addData("Run","Legs and Arms","Rapidly put one foot in front of the other");
}
}



  • Note this will just add the above data once (the first line is checking to see if data already exists).


You could invoke the method using :-



protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
addSomeDataButOnlyIfNoneExists(); //<<<<<<<<<< ADDED

........ rest of your code


Result (with data)



using the above, running the app and clicking on the View Details button results in :-



enter image description here






share|improve this answer























  • Thank you so much it worked finally
    – Huzaifa Yusuf
    Dec 28 '18 at 12:16
















0














From your code it appears that all is working fine. However, there does not appear to be any data added, nor a means to add data. Thus the dialog is showing no data BUT it is not showing the message that indicates no data.



1. Fix the dialog to indicate no data



To fix the Dialog showing no data you need to check to see if the Cursor has not rows rather than 1 row. So change the line :-



if(res.getCount() == 1) { 


to be



if(res.getCount() < 1) {


2. Add some data so data is displayed



To show some data you need to add some data. To add some data you could have a method, in the DatabaseHelper class that allows data to be added. For example :-



public long addData(String excercise_name, String body_part, String description) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL_2,excercise_name);
cv.put(COL_3,body_part);
cv.put(COL_4,body_part);
return db.insert(TABLE_NAME,null,cv);
}


You would then need to actually add some data, as an example of doing so, the following method could be added to your MainActivity :-



private void addSomeDataButOnlyIfNoneExists() {
if (DatabaseUtils.queryNumEntries(myDb.getWritableDatabase(),DatabaseHelper.TABLE_NAME) < 1) {
myDb.addData("Squat","Legs","Squat");
myDb.addData("Push up","Arms","Push up");
myDb.addData("Run","Legs and Arms","Rapidly put one foot in front of the other");
}
}



  • Note this will just add the above data once (the first line is checking to see if data already exists).


You could invoke the method using :-



protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
addSomeDataButOnlyIfNoneExists(); //<<<<<<<<<< ADDED

........ rest of your code


Result (with data)



using the above, running the app and clicking on the View Details button results in :-



enter image description here






share|improve this answer























  • Thank you so much it worked finally
    – Huzaifa Yusuf
    Dec 28 '18 at 12:16














0












0








0






From your code it appears that all is working fine. However, there does not appear to be any data added, nor a means to add data. Thus the dialog is showing no data BUT it is not showing the message that indicates no data.



1. Fix the dialog to indicate no data



To fix the Dialog showing no data you need to check to see if the Cursor has not rows rather than 1 row. So change the line :-



if(res.getCount() == 1) { 


to be



if(res.getCount() < 1) {


2. Add some data so data is displayed



To show some data you need to add some data. To add some data you could have a method, in the DatabaseHelper class that allows data to be added. For example :-



public long addData(String excercise_name, String body_part, String description) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL_2,excercise_name);
cv.put(COL_3,body_part);
cv.put(COL_4,body_part);
return db.insert(TABLE_NAME,null,cv);
}


You would then need to actually add some data, as an example of doing so, the following method could be added to your MainActivity :-



private void addSomeDataButOnlyIfNoneExists() {
if (DatabaseUtils.queryNumEntries(myDb.getWritableDatabase(),DatabaseHelper.TABLE_NAME) < 1) {
myDb.addData("Squat","Legs","Squat");
myDb.addData("Push up","Arms","Push up");
myDb.addData("Run","Legs and Arms","Rapidly put one foot in front of the other");
}
}



  • Note this will just add the above data once (the first line is checking to see if data already exists).


You could invoke the method using :-



protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
addSomeDataButOnlyIfNoneExists(); //<<<<<<<<<< ADDED

........ rest of your code


Result (with data)



using the above, running the app and clicking on the View Details button results in :-



enter image description here






share|improve this answer














From your code it appears that all is working fine. However, there does not appear to be any data added, nor a means to add data. Thus the dialog is showing no data BUT it is not showing the message that indicates no data.



1. Fix the dialog to indicate no data



To fix the Dialog showing no data you need to check to see if the Cursor has not rows rather than 1 row. So change the line :-



if(res.getCount() == 1) { 


to be



if(res.getCount() < 1) {


2. Add some data so data is displayed



To show some data you need to add some data. To add some data you could have a method, in the DatabaseHelper class that allows data to be added. For example :-



public long addData(String excercise_name, String body_part, String description) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL_2,excercise_name);
cv.put(COL_3,body_part);
cv.put(COL_4,body_part);
return db.insert(TABLE_NAME,null,cv);
}


You would then need to actually add some data, as an example of doing so, the following method could be added to your MainActivity :-



private void addSomeDataButOnlyIfNoneExists() {
if (DatabaseUtils.queryNumEntries(myDb.getWritableDatabase(),DatabaseHelper.TABLE_NAME) < 1) {
myDb.addData("Squat","Legs","Squat");
myDb.addData("Push up","Arms","Push up");
myDb.addData("Run","Legs and Arms","Rapidly put one foot in front of the other");
}
}



  • Note this will just add the above data once (the first line is checking to see if data already exists).


You could invoke the method using :-



protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
addSomeDataButOnlyIfNoneExists(); //<<<<<<<<<< ADDED

........ rest of your code


Result (with data)



using the above, running the app and clicking on the View Details button results in :-



enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 28 '18 at 22:58

























answered Dec 27 '18 at 19:52









MikeT

14.9k102441




14.9k102441












  • Thank you so much it worked finally
    – Huzaifa Yusuf
    Dec 28 '18 at 12:16


















  • Thank you so much it worked finally
    – Huzaifa Yusuf
    Dec 28 '18 at 12:16
















Thank you so much it worked finally
– Huzaifa Yusuf
Dec 28 '18 at 12:16




Thank you so much it worked finally
– Huzaifa Yusuf
Dec 28 '18 at 12:16


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53935006%2fim-having-some-issues-in-fetching-data-from-sqlite-database%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas