将值插入数据库后出现非法状态异常

时间:2013-08-13 04:58:58

标签: android sqlite

Sqlite助手类:

public class SQLite_helper extends SQLiteOpenHelper  {

    public static final String MYDATABASE_TABLE = "Stripclubs_table_test";
    public static final String KEY_ID = "_id";
    public static final String KEY_CONTENT1 = "Name";
    public static final String KEY_CONTENT2 = "Town";
    public static final String KEY_CONTENT3 = "State";
    public static final String KEY_CONTENT4 = "Phone";
    public static final String KEY_CONTENT5 = "Website";
    public static final String KEY_CONTENT6 = "Description";
    public static final String KEY_CONTENT7 = "Stars";
    public static final String KEY_CONTENT8 = "Latitude";
    public static final String KEY_CONTENT9 = "Longitude";
    public static final String KEY_CONTENT10 = "zipcode";
    public static final String KEY_CONTENT11 = "id";



    private static final String SCRIPT_CREATE_DATABASE =
          "create table " + MYDATABASE_TABLE + " ("
          + KEY_ID + " integer primary key autoincrement, "
          + KEY_CONTENT1 + " text, "
          + KEY_CONTENT2 + " text, "
          + KEY_CONTENT3 + " text, "
          + KEY_CONTENT4 + " text, "
          + KEY_CONTENT5 + " text, "
          + KEY_CONTENT6 + " text, "
          + KEY_CONTENT7 + " text, "
          + KEY_CONTENT8 + " text, "
          + KEY_CONTENT9 + " text, "
          + KEY_CONTENT10 + " text, "
          + KEY_CONTENT11 + " text)";



    public SQLite_helper(Context context, String name, CursorFactory factory,
            int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(SCRIPT_CREATE_DATABASE); 
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

    }

Mainactivity:

public class MainActivity extends Activity {


    public static final String MYDATABASE_NAMEE = "MY_DATABASE_TEST";
    public static final int MYDATABASE_VERSION = 3;
    public static final String MYDATABASE_TABLE = "Stripclubs_table_test";
    public static final String KEY_ID = "_id";
    public static final String KEY_CONTENT1 = "Name";
    public static final String KEY_CONTENT2 = "Town";
    public static final String KEY_CONTENT3 = "State";
    public static final String KEY_CONTENT4 = "Phone";
    public static final String KEY_CONTENT5 = "Website";
    public static final String KEY_CONTENT6 = "Description";
    public static final String KEY_CONTENT7 = "Stars";
    public static final String KEY_CONTENT8 = "Latitude";
    public static final String KEY_CONTENT9 = "Longitude";
    public static final String KEY_CONTENT10 = "zipcode";
    public static final String KEY_CONTENT11 = "id";

    private SQLite_helper sqLiteHelper;
    private SQLiteDatabase sqLiteDatabase;
    Cursor cursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        sqLiteHelper = new SQLite_helper(this, MYDATABASE_NAMEE, null, MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getWritableDatabase();
        if (!sqLiteDatabase.isOpen()) {
            sqLiteDatabase = getApplicationContext().openOrCreateDatabase("/data/data/com.example.sqlitetest/databases/MY_DATABASE_TEST.db", SQLiteDatabase.OPEN_READWRITE, null);
             }
        cursor = sqLiteDatabase.query(MYDATABASE_TABLE, null,null, null, null, null, null);
        System.out.println("MAX_PLACES  "+cursor.getCount());
            for(int i=0;i<5;i++){

                    ContentValues cv = new ContentValues();
                    cv.put(KEY_CONTENT1, "Salu");//Star Dust Lounge
                    cv.put(KEY_CONTENT2, "place");//District Heights
                    cv.put(KEY_CONTENT3, "fgfjhghj");
                    cv.put(KEY_CONTENT4, "47567");//301-5344-159
                    cv.put(KEY_CONTENT5, "ghj");//http://testsitez.info/stripclubs/harley-riders-private-club/
                    cv.put(KEY_CONTENT6, "5xfdgh");//low key around the way spot.  dancers will negotiate $$ for vip action. 
                    cv.put(KEY_CONTENT7, "3");
                    cv.put(KEY_CONTENT8, "gh");//38.864
                    cv.put(KEY_CONTENT9, "fggh");//-76.9145
                    cv.put(KEY_CONTENT10, "gfh");
                    cv.put(KEY_CONTENT11, "hjghj");

                    sqLiteDatabase.insert(MYDATABASE_TABLE, null, cv);
                    cursor.requery();
                    List<String> lables_name = sqLiteHelper.getAllLabels("Name");
                    System.out.println(lables_name.size());

            }

            //new GetPlaces().execute("details");

    }
}

主要活动和助手类如上所示。我的问题是,插入第一行后,程序通过抛出IllegalStateException来关闭。这里有什么问题。

这是我的logcat输出

08-13 10:34:34.829: E/AndroidRuntime(5860): FATAL EXCEPTION: main
08-13 10:34:34.829: E/AndroidRuntime(5860): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqlitetest/com.example.sqlitetest.MainActivity}: java.lang.IllegalStateException: database not open
08-13 10:34:34.829: E/AndroidRuntime(5860):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-13 10:34:34.829: E/AndroidRuntime(5860):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-13 10:34:34.829: E/AndroidRuntime(5860):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-13 10:34:34.829: E/AndroidRuntime(5860):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-13 10:34:34.829: E/AndroidRuntime(5860):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 10:34:34.829: E/AndroidRuntime(5860):     at android.os.Looper.loop(Looper.java:123)
08-13 10:34:34.829: E/AndroidRuntime(5860):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-13 10:34:34.829: E/AndroidRuntime(5860):     at java.lang.reflect.Method.invokeNative(Native Method)
08-13 10:34:34.829: E/AndroidRuntime(5860):     at java.lang.reflect.Method.invoke(Method.java:507)
08-13 10:34:34.829: E/AndroidRuntime(5860):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-13 10:34:34.829: E/AndroidRuntime(5860):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-13 10:34:34.829: E/AndroidRuntime(5860):     at dalvik.system.NativeStart.main(Native Method)
08-13 10:34:34.829: E/AndroidRuntime(5860): Caused by: java.lang.IllegalStateException: database not open
08-13 10:34:34.829: E/AndroidRuntime(5860):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1526)
08-13 10:34:34.829: E/AndroidRuntime(5860):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
08-13 10:34:34.829: E/AndroidRuntime(5860):     at com.example.sqlitetest.MainActivity.onCreate(MainActivity.java:85)
08-13 10:34:34.829: E/AndroidRuntime(5860):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-13 10:34:34.829: E/AndroidRuntime(5860):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-13 10:34:34.829: E/AndroidRuntime(5860):     ... 11 more

com.example.sqlitetest.MainActivity.onCreate(MainActivity.java:85)抛出sqLiteDatabase.insert(MYDATABASE_TABLE,null,cv)行;在主要活动中。但第一行是插入。

0 个答案:

没有答案