【发布时间】:2022-03-17 21:42:23
【问题描述】:
我在这里搜索了一段时间,但没有任何帮助。我想这很简单,我正在做一些愚蠢的事情。所以,我让这个 DataBaseHelper 创建一个表并在其上放置两个值:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "db";
public static final String PROPERTY = "property";
public static final String VALUE = "value";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE configs (property VARCHAR(15), value VARCHAR(10));");
ContentValues cv=new ContentValues();
cv.put(PROPERTY, "registered");
cv.put(VALUE, "true");
db.insert("configs", PROPERTY, cv);
cv.put(PROPERTY, "test");
cv.put(VALUE, "test");
db.insert("configs", PROPERTY, cv);
}
}
在我的活动中我正在尝试:
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
layout = (LinearLayout) findViewById(R.id.layoutRegister);
db = (new DatabaseHelper(this)).getWritableDatabase();
cursor = db.rawQuery("SELECT * FROM configs",
null);
if (cursor.moveToFirst()) {
debug = new TextView(this);
debug.setText(cursor.getString(cursor.getColumnIndex("property")));
layout.addView(debug);
}
}
它从不进入 if。此外,当我尝试类似 cursor.getCount() 的东西时,我得到一个 android.content.res.Resource$NotFoundException: String resource ID #0x0
我猜这是一个简单的错误,但是...有什么帮助吗?
【问题讨论】:
-
我认为你遗漏了很多东西。例如,用于 CRUD 操作的类也是如此。请参阅此链接,这是一个很好的示例作为开始。 androidhive.info/2011/11/android-sqlite-database-tutorial , vogella.com/articles/AndroidSQLite/article.html
-
谢谢,我知道了。但这只是一个小测试/学习......对于这样一个简单的测试,我不需要那个(是吗?)。我当然会读它,也许它会启发我。