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;
publicclassMyDBHandlerextendsSQLiteOpenHelper {
privatestaticfinalintDATABASE_VERSION=1;
privatestaticfinalStringDATABASE_NAME="healthDB.db";
privatestaticfinalStringTABLE_DAILY="daily";
publicstaticfinalStringCOLUMN_ID="_id";
publicstaticfinalStringCOLUMN_TIPS="tips";
static MyDBHandler mDbHelper;
privateMyDBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
publicstaticsynchronized MyDBHandler getInstance(Context context) {
if (mDbHelper == null) {
mDbHelper = newMyDBHandler(context);
}
return mDbHelper;
}
@OverridepublicvoidonCreate(SQLiteDatabase db) {
StringCREATE_DAILY_TABLE="CREATE TABLE " + TABLE_DAILY + "(" + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_TIPS + " TEXT " + ")";
db.execSQL(CREATE_DAILY_TABLE);
}
@OverridepublicvoidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_DAILY);
onCreate(db);
}
publicvoidaddDaily(Daily daily) {
SQLiteDatabasedb=this.getWritableDatabase();
ContentValuesvalues=newContentValues();
values.put(COLUMN_TIPS, daily.getTips());
db.insert(TABLE_DAILY, null, values);
db.close();
}
public List<Daily> getAllDailys(){
List <Daily> dailyList=newArrayList<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 listif(c.moveToFirst()){
do{
Daily daily=newDaily();
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
MyDBHandlerdb= MyDBHandler.getInstance(getApplicationContext());
Dailydaily=newDaily();
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:
MyDBHandlerdb=newMyDBHandler(....);
db.addDaily(daily);
the database will not be created.
[EDIT 2] you may also want to change your MyDBHandler
class' constructor:
publicMyDBHandler(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"