DataBase不工作--sqlite

时间:2014-07-24 13:58:02

标签: java android database sqlite

我试图开发一个非常简单的数据库,但它没有工作。

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

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

这是我的DB代码:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;

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 mega" +
      "(w_4,w_6,w_8, w_10,t_4,t_6,t_8,t_10)"
      );
      db = this.getWritableDatabase();
      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 contacts");
      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);
       }

}

日志:

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

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


}

我真的不需要更多的东西了,但我遇到了麻烦。

如果有人请帮助我,我会非常感激!

提前致谢!

1 个答案:

答案 0 :(得分:2)

db.execSQL(
"create mega" +
"(w_4,w_6,w_8, w_10,t_4,t_6,t_8,t_10)"
);

语法错误。它应该是

db.execSQL("CREATE TABLE " + mega + "(w_4 TEXT, w_6 TEXT,...)");

缺少数据类型(TEXT),它应该是CREATE TABLE + TABLE_NAME,而不是CREATE TABLE_NAME

另外,请移除db = this.getWritableDatabase()中的onCreate