0

I'm new to android and want to design database for my android application but i'm not able to do that. I have done everything to find the error but couldn't locate any error and still not able to create database. Please someone help me in this, thanks in advance.

The code is below:

Model Class:

package com.example.health;

public class Daily {
private int _id;
private String _tips;

public Daily(){
}

public Daily(int id, String tips){
    this._id=id;
    this._tips=tips;
}
public Daily(String tips){
    this._tips=tips;
}
public void setID(int id) {
    this._id = id;
}
public void setTips(String tips){
    this._tips=tips;
}
public int getID() {
    return this._id;
}
public String getTips(){
    return this._tips;
}
  }

Database Handler:

 package com.example.health;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;


public class MyDBHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "healthDB.db";
private static final String TABLE_DAILY = "daily";

public static final String COLUMN_ID = "_id";
public static final String COLUMN_TIPS = "tips";

public MyDBHandler(Context context, String name, 
        CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_DAILY_TABLE = "CREATE TABLE " + TABLE_DAILY + "(" + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_TIPS + " TEXT " + ")";
      db.execSQL(CREATE_DAILY_TABLE);

      }

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_DAILY);
      onCreate(db);
}
public void addDaily(Daily daily) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COLUMN_TIPS, daily.getTips());
    db.insert(TABLE_DAILY, null, values);
    db.close();
}
 public List<Daily> getAllDailys(){
        List <Daily> dailyList=new ArrayList<Daily>();

        // Select All Query
        String selectQuery="SELECT * FROM " + TABLE_DAILY;

        SQLiteDatabase db=this.getReadableDatabase();
        Cursor c=db.rawQuery(selectQuery, null);

        // looping through all rows and adding to the list
        if(c.moveToFirst()){
            do{
                Daily daily=new Daily();
                daily.setID(Integer.parseInt(c.getString(0)));
                daily.setTips(c.getString(1));


                // Adding user to the list
                dailyList.add(daily);
            }while(c.moveToNext());
        }
        c.close();
        return dailyList;
}}
Zohaib
  • 41
  • 12

2 Answers2

0

Note that the database is created only after you perform some operation on the database itself. i.e. By just creating the SQLiteOpenHelper class will NOT create the database in the device until you perform some Read/Write operation on the Database.

[EDIT 1] so untill you do something like:

MyDBHandler db = new MyDBHandler(....);
db.addDaily(daily);

the database will not be created.

[EDIT 2] you may also want to change your MyDBHandler class' constructor:

public MyDBHandler(Context contex) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

[NOTE] I am assuming you are not getting any error/exceptions.

SMR
  • 6,628
  • 2
  • 35
  • 56
  • can you tell me one more thing that how can we fetch all data from database in to List View in layout. For Eg: I want to fetch all data from "daily" table in to list view in layout. – Zohaib Jun 18 '14 at 12:15
  • that is subject to another question, google first and then ask. I am sure you can find it. – SMR Jun 18 '14 at 12:27
  • how can i add more than one entry at same time and delete all data at same time. one more thing that i want to ask is your above code it's added same data every time i run the application. Please reply. Thanks. – Zohaib Jun 19 '14 at 10:39
  • @Zohaib _"is your above code it's added same data every time i run the application."_ well... it depends on where you put that code.. if you put it inside `onCreate()` then YES but you can put it anywhere you like e.g. on `onClickListener()` of a button or a view. – SMR Jun 19 '14 at 11:35
  • to add multiple items you can simply call the `db.addDaily(someObject)` multiple times or can create a method inside the `SQLiteOpenHelper` class to accept an `Array` or `ArrayList` of objects and add them. – SMR Jun 19 '14 at 11:38
  • can you just write in brief for multiple entry as you wrote for single entry above.I have already try this. and if possible then for delete query also. Thanks – Zohaib Jun 19 '14 at 11:56
  • secondly I understand why it's inserting the sane data as i put the code in onCreate() thanks for the info. – Zohaib Jun 19 '14 at 11:58
  • `db.addDaily(obj1); db.addDaily(obj2);` whats the big deal... dont get into the habbit of spoon feeding... do your research before asking such stupid questions. – SMR Jun 19 '14 at 12:00
  • As i had told you that I'm totally new to android and all these concepts are totally new for me. It's less than 10 days that i start learning android and asking simple questions are not stupidity. But still thanks for your co-operation... :) – Zohaib Jun 19 '14 at 12:14
  • need help !! now everything is clear to me. I sort out the multiple entry of rows but when i put insert query outside the onCreate() data is not inserted but inside the onCreate() same data is inserted very time i run the application . Thanks – Zohaib Jun 20 '14 at 08:19
0

Your DbClass

package com.example.demo;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;


public class MyDBHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "healthDB.db";
private static final String TABLE_DAILY = "daily";

public static final String COLUMN_ID = "_id";
public static final String COLUMN_TIPS = "tips";

static MyDBHandler mDbHelper;

private MyDBHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public static synchronized MyDBHandler getInstance(Context context) {
    if (mDbHelper == null) {
        mDbHelper = new MyDBHandler(context);
    }
    return mDbHelper;
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_DAILY_TABLE = "CREATE TABLE " + TABLE_DAILY + "(" + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_TIPS + " TEXT " + ")";
      db.execSQL(CREATE_DAILY_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_DAILY);
      onCreate(db);
}
public void addDaily(Daily daily) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COLUMN_TIPS, daily.getTips());
    db.insert(TABLE_DAILY, null, values);
    db.close();
}
 public List<Daily> getAllDailys(){
        List <Daily> dailyList=new ArrayList<Daily>();

        // Select All Query
        String selectQuery="SELECT * FROM " + TABLE_DAILY;

        SQLiteDatabase db=this.getReadableDatabase();
        Cursor c=db.rawQuery(selectQuery, null);

        // looping through all rows and adding to the list
        if(c.moveToFirst()){
            do{
                Daily daily=new Daily();
                daily.setID(Integer.parseInt(c.getString(0)));
                daily.setTips(c.getString(1));


                // Adding user to the list
                dailyList.add(daily);
            }while(c.moveToNext());
        }
        c.close();
        return dailyList;
}}

You can add data like this

MyDBHandler db = MyDBHandler.getInstance(getApplicationContext());
Daily daily = new Daily();
daily.setID(1);
daily.setTips("hello");
db.addDaily(daily);

I have tested it. It works fine..

Community
  • 1
  • 1
user
  • 245
  • 1
  • 4
  • 12
  • Thanks for helping http://stackoverflow.com/users/2968912/sourabh-jain now i got it, database will not be created until some READ/Write operation is performed. – Zohaib Jun 18 '14 at 10:12
  • can you tell me one more thing that how can we fetch all data from database in single Text View without any button click in layout. – Zohaib Jun 18 '14 at 10:26