Reputation: 219
I developed DB for an application, and it's working...
What I need is DB with 8 Strings that I can update and get them from an Activity.
I need to start with "" for all 8 strings.
This is my code for the DB:
public class Mega_DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Mega.db";
public static final String MEGA_TABLE_NAME = "mega";
public static final String WEDNESDAY_4 = "w_4";
public static final String WEDNESDAY_6 = "w_6";
public static final String WEDNESDAY_8 = "w_8";
public static final String WEDNESDAY_10 = "w_10";
public static final String THURSDAY_4 = "t_4";
public static final String THURSDAY_6 = "t_6";
public static final String THURSDAY_8 = "t_8";
public static final String THURSDAY_10 = "t_10";
private HashMap hp;
public Mega_DBHelper(Context context)
{
super(context, DATABASE_NAME , null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + MEGA_TABLE_NAME + "(w_4 TEXT, w_6 TEXT, w_8 TEXT, w_10 TEXT, t_4 TEXT, t_6 TEXT, t_8 TEXT, t_10 TEXT )");
ContentValues contentValues = new ContentValues();
contentValues.put("w_4", "");
contentValues.put("w_6", "");
contentValues.put("w_8", "");
contentValues.put("w_10", "");
contentValues.put("t_4", "");
contentValues.put("t_6", "");
contentValues.put("t_8", "");
contentValues.put("t_10", "");
db.insert(MEGA_TABLE_NAME, null, contentValues);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+ MEGA_TABLE_NAME);
onCreate(db);
}
public boolean updateW_4 (String name)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("w_4", name);
db.update("mega", contentValues, null , null );
return true;
}
public boolean updateW_6 (String name)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("w_6", name);
db.update("mega", contentValues, null , null );
return true;
}
public boolean updateW_8 (String name)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("w_8", name);
db.update("mega", contentValues, null , null );
return true;
}
public boolean updateW_10 (String name)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("w_10", name);
db.update("mega", contentValues, null , null );
return true;
}
public boolean updateT_4 (String name)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("t_4", name);
db.update("mega", contentValues, null , null );
return true;
}
public boolean updateT_6 (String name)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("t_6", name);
db.update("mega", contentValues, null , null );
return true;
}
public boolean updateT_8 (String name)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("t_8", name);
db.update("mega", contentValues, null , null );
return true;
}
public boolean updateT_10 (String name)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("t_10", name);
db.update("mega", contentValues, null , null );
return true;
}
public String getW_4(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from mega", null );
return res.getString(0);
}
public String getW_6(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from mega", null );
return res.getString(1);
}
public String getW_8(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from mega", null );
return res.getString(2);
}
public String getW_10(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from mega", null );
return res.getString(3);
}
public String getT_4(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from mega", null );
return res.getString(4);
}
public String getT_6(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from mega", null );
return res.getString(5);
}
public String getT_8(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from mega", null );
return res.getString(6);
}
public String getT_10(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from mega", null );
return res.getString(7);
}
}
This is the the function of the activity that uses the Mega_DBHelper:
public void setUpView (){
wednesday = (Button)this.findViewById(R.id.wednesday);
thursday = (Button)this.findViewById(R.id.thursday);
day = (TextView)this.findViewById(R.id.day);
edit4 = (EditText)this.findViewById(R.id.edit4);
edit4.setImeOptions(EditorInfo.IME_ACTION_DONE);
edit6 = (EditText)this.findViewById(R.id.edit6);
edit6.setImeOptions(EditorInfo.IME_ACTION_DONE);
edit8 = (EditText)this.findViewById(R.id.edit8);
edit8.setImeOptions(EditorInfo.IME_ACTION_DONE);
edit10 = (EditText)this.findViewById(R.id.edit10);
edit10.setImeOptions(EditorInfo.IME_ACTION_DONE);
day.setText("רביעי");
wednesday.setVisibility(View.INVISIBLE);
megaDB = new Mega_DBHelper(this);
edit4.setText(megaDB.getW_4());
edit6.setText(megaDB.getW_6());
edit8.setText(megaDB.getW_8());
edit10.setText(megaDB.getW_10());
}
This is the logCat:
07-24 19:49:40.700: E/AndroidRuntime(8082): FATAL EXCEPTION: main
07-24 19:49:40.700: E/AndroidRuntime(8082): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidclient/com.example.androidclient.TableM}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
07-24 19:49:40.700: E/AndroidRuntime(8082): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
07-24 19:49:40.700: E/AndroidRuntime(8082): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
07-24 19:49:40.700: E/AndroidRuntime(8082): at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-24 19:49:40.700: E/AndroidRuntime(8082): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
07-24 19:49:40.700: E/AndroidRuntime(8082): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 19:49:40.700: E/AndroidRuntime(8082): at android.os.Looper.loop(Looper.java:137)
07-24 19:49:40.700: E/AndroidRuntime(8082): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-24 19:49:40.700: E/AndroidRuntime(8082): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 19:49:40.700: E/AndroidRuntime(8082): at java.lang.reflect.Method.invoke(Method.java:511)
07-24 19:49:40.700: E/AndroidRuntime(8082): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-24 19:49:40.700: E/AndroidRuntime(8082): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-24 19:49:40.700: E/AndroidRuntime(8082): at dalvik.system.NativeStart.main(Native Method)
07-24 19:49:40.700: E/AndroidRuntime(8082): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
07-24 19:49:40.700: E/AndroidRuntime(8082): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
07-24 19:49:40.700: E/AndroidRuntime(8082): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
07-24 19:49:40.700: E/AndroidRuntime(8082): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
07-24 19:49:40.700: E/AndroidRuntime(8082): at com.example.androidclient.Mega_DBHelper.getW_4(Mega_DBHelper.java:152)
07-24 19:49:40.700: E/AndroidRuntime(8082): at com.example.androidclient.TableM.setUpView(TableM.java:145)
07-24 19:49:40.700: E/AndroidRuntime(8082): at com.example.androidclient.TableM.onCreate(TableM.java:55)
07-24 19:49:40.700: E/AndroidRuntime(8082): at android.app.Activity.performCreate(Activity.java:5206)
07-24 19:49:40.700: E/AndroidRuntime(8082): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
07-24 19:49:40.700: E/AndroidRuntime(8082): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
07-24 19:49:40.700: E/AndroidRuntime(8082): ... 11 more
Can someone please help me?
Thanks in advance!
Answer by Huy Tran:
public String getW_4(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from mega", null );
if (res.moveToFirst()) {
return res.getString(0);
} else {
return "";
}
}
Upvotes: 0
Views: 85
Reputation: 198
A cursor might not contain any data, and when it is delivered to you, it is set to row -1. In order to determine if there is any data, you must ask it to move to the first valid position.
If there is no valid position, you will have to handle that case as well.
public String getW_4(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from mega", null );
if (res.moveToFirst()) {
return res.getString(0);
} else {
return "";
}
}
Upvotes: 2
Reputation: 368
Before getting something from the cursor, you first need to call moveToNext on it first. So for example of get_W4 :
public String getW_4(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from mega", null );
res.moveToNext();
return res.getString(0);
}
Upvotes: 2