DB-sqlite android,无法正常工作

时间:2014-07-24 17:24:56

标签: java android database sqlite

我为一个应用程序开发了DB,它正在工作......

我需要的是带有8个字符串的数据库,我可以更新并从活动中获取它们。

我需要从""开始所有8个字符串。

这是我的数据库代码:

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

}

这是使用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());
}

这是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

有人可以帮帮我吗?

提前致谢!

回答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 "";
  }
}

2 个答案:

答案 0 :(得分:2)

游标可能不包含任何数据,当它传递给您时,它被设置为第-1行。为了确定是否有任何数据,您必须要求它移动到第一个有效位置。

如果没有有效的职位,你也必须处理这个案件。

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

答案 1 :(得分:2)

在从光标获取内容之前,首先需要先调用moveToNext。 例如get_W4:

public String getW_4(){
      SQLiteDatabase db = this.getReadableDatabase();
      Cursor res =  db.rawQuery( "select * from mega", null );
      res.moveToNext();
      return res.getString(0);
}