harshita
harshita

Reputation: 497

getting the error on adding DEFAULT values

I am using this code to insert default values to my database and fetching that default value

public class DatabseHandlerTwo extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "db_table";
public static final int DATABASE_VERSION = 1;
public static final String TABLENAME = "table_one";
public static final String KEYID = "_id";
public static final String DETAILS = "_details";
private static DatabseHandlerTwo mInstance = null;
Context context;

String CREATE_TABLE = "CREATE TABLE "+ TABLENAME + "("+ KEYID + " INTEGER PRIMARY KEY, "+ DETAILS + " TEXT DEFAULT 'RAM' )";

public DatabseHandlerTwo(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context = context;
}


public static DatabseHandlerTwo getInstance(Context ctx) {


    if (mInstance == null) {
        mInstance = new DatabseHandlerTwo(ctx.getApplicationContext());
    }
    return mInstance;
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL(CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+TABLENAME);
}

public ArrayList<String> fetchDetails(){
    SQLiteDatabase db = this.getReadableDatabase();
    ArrayList<String> arrayList = new ArrayList<String>();
    Cursor cursor = db.rawQuery("SELECT " + DETAILS + " FROM " + TABLENAME,null);
    Log.e("value",cursor.toString());
    try {
        if (cursor.moveToFirst()){
            do {
                arrayList.add(cursor.getString(cursor.getColumnIndex(DETAILS)));
                Log.e("value",cursor.getString(0));
            }while (cursor.moveToNext());
        }
    }catch (SQLiteException e){
        e.printStackTrace();
    }

    return arrayList;
}

}

but i am getting this error:

E/SQLiteLog: (10) Failed to do file read, got: 0, amt: 100, last Errno: 2

Upvotes: 1

Views: 502

Answers (2)

Niraj Sanghani
Niraj Sanghani

Reputation: 1493

You did miss out this onCreate(sqLiteDatabase); in onUpgrade

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+TABLENAME);
    onCreate(sqLiteDatabase);
}

So when you upgrade, no new Table is being created after drop.

Also edit:

mInstance = new DatabaseHandler(ctx.getApplicationContext());

to:

mInstance = new DatabaseHandler(ctx);

Upvotes: 0

Nitin Patel
Nitin Patel

Reputation: 1651

Try this way:-

First of all switch line below code with changing in query line:

String CREATE_TABLE = "CREATE TABLE "+ TABLENAME + " ("+ KEYID + " INTEGER PRIMARY KEY, "+ DETAILS + " TEXT DEFAULT 'RAM')";

Also, change fetchDetails function like below:

public ArrayList<String> fetchDetails(){
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<String> arrayList = new ArrayList<String>();
Cursor cursor = db.rawQuery("SELECT " + DETAILS + " FROM " + TABLENAME,null);
//Log.e("value",cursor.toString());
try {
    if (cursor!=null && cursor.getCount() > 0 && cursor.moveToFirst()){
        do {
            arrayList.add(cursor.getString(cursor.getColumnIndex(DETAILS)));
            Log.e("value",cursor.getString(0));
        }while (cursor.moveToNext());
    }
}catch (SQLiteException e){
    e.printStackTrace();
}

    return arrayList;
}

And don't forget to uninstall previous app then run and install, because we change in query of creating table which executes in onCreate.

Hope this will help you.

Upvotes: 1

Related Questions