Reputation: 3225
I have searched here for a while and nothing helped me. I guess it is simple, I'm doing something stupid. So, I have this DataBaseHelper do create a table and put two values on it:
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);
}
}
and in my activity im trying:
@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);
}
}
and it never enters in the if. Moreover, when I try something like cursor.getCount() I get a android.content.res.Resource$NotFoundException: String resource ID #0x0
I guess it is a simple mistake, but... Any help?
Upvotes: 2
Views: 6578
Reputation: 42016
What I understood you want to set the Text when cursor having values so you can simply do this...
cursor = db.rawQuery("SELECT * FROM configs", null);
if (cursor!=null && cursor.getCount()>0) {
cursor.moveToFirst();
debug = new TextView(this);
debug.setText(cursor.getString(cursor.getColumnIndex("property")));
layout.addView(debug);
}
And about
when I try something like cursor.getCount() I get a android.content.res.Resource$NotFoundException: String resource ID #0x0
It is because when you using setText() of TextView, So if you passing parameter as Integer then it will look in R.class file so if you want to set text as integer you can do something like...
debug.setText(""+cursor.getCount());
or
debug.setText(String.valueOf(cursor.getCount()));
Upvotes: 6
Reputation: 2671
Use this
cv.put(property, "registered");
cv.put(value, "true");
and
cv.put(property, "test");
cv.put(value, "test");
instead of
cv.put(PROPERTY, "registered");
cv.put(VALUE, "true");
and
cv.put(PROPERTY, "test");
cv.put(VALUE, "test");
Upvotes: -1
Reputation: 578
If you are going to query use getReadableDatabase not getWritableDatabase
and always cursor.close();
Upvotes: 2