Android SQLite Database Not Creating
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 st
Solution 1:
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..
Solution 2:
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.
Post a Comment for "Android SQLite Database Not Creating"