在添加DEFAULT值时出错

时间:2017-07-10 12:49:53

标签: android sqlite

我正在使用此代码将默认值插入我的数据库并获取该默认值

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;
}

}

但是我收到了这个错误:

  

E / SQLiteLog:(10)文件读取失败,得到:0,amt:100,最后错误:2

2 个答案:

答案 0 :(得分:1)

尝试这种方式: -

首先在代码下面切换行,并在查询行中进行更改:

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

另外,更改 fetchDetails 功能,如下所示:

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;
}

并且不要忘记卸载以前的应用程序然后运行并安装,因为我们更改了在onCreate中执行的创建表的查询。

希望这会对你有所帮助。

答案 1 :(得分:0)

你确实错过了onUpgrade中的onCreate(sqLiteDatabase);

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

因此,升级时,删除后不会创建新的表。

同时编辑:

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

为:

mInstance = new DatabaseHandler(ctx);