gpt4 book ai didi

android - 在添加 DEFAULT 值时出现错误

转载 作者:行者123 更新时间:2023-11-30 00:28:00 25 4
gpt4 key购买 nike

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

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) Failed to do file read, got: 0, amt: 100, last Errno: 2

最佳答案

试试这个方法:-

首先,在查询行中更改代码下方的所有切换行:

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 中执行的创建表的查询。

希望对您有所帮助。

关于android - 在添加 DEFAULT 值时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45012560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com