SQLite中的升级似乎无法在我的应用程序中运行

时间:2014-02-23 08:59:06

标签: java android database eclipse sqlite

我正在关注移动应用程序的SQLite初学者教程,但我仍然遇到输出问题。数据将显示在ListView上,但每次我从Eclipse运行到我的手机时,每次从Eclipse运行应用程序时,都会添加数据而不是列表3,然后它会有一个列表为6的输出,9,12,15等。我想旧桌子不会被SQLite升级所淹没。

我的数据库中有以下代码:

    public class DatabaseHandler extends SQLiteOpenHelper
    {
        //all static variables; database version
        private static final int DATABASE_VERSION = 1;

        //database name
        private static final String DATABASE_NAME = "devicesqlite.db";

        //Tag for Logcat
        private static final String LOGCAT = null;

    //for the switch //same in ControllerTab
        private final static Integer[] ids = { R.id.switch1, R.id.switch2, R.id.switch3 };

        public DatabaseHandler(Context applicationContext) {
            //super(context, name, factory, version);
            super(applicationContext, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            //String CREATE_DEVICES_TABLE = " CREATE TABLE " + TABLE_DEVICES + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT" + ")";
            String createQuery = "CREATE TABLE devices ( deviceID INTEGER PRIMARY KEY, deviceName TEXT)";
            db.execSQL(createQuery);
            Log.d(LOGCAT, "TABLE devices CREATED");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            //drop older table if existed
            String dropQuery = ("DROP TABLE IF EXISTS devices");
            db.execSQL(dropQuery);
            onCreate(db); //create tables again
        }

        //adding new device
        public void addDevice(Device device) {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("deviceName", device.getName());
            db.insert("devices", null, values); //inserting row
            db.close();
        }

            //getting all devices
        public ArrayList<HashMap<String, String>> getAllDevices() {
            ////ArrayList<Device> deviceList = new ArrayList<Device>();
            ArrayList<HashMap<String, String>> deviceList;
            deviceList = new ArrayList<HashMap<String, String>>();

            //Select All query
            String selectQuery = "SELECT * FROM devices";
            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);

            //looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do
                {
                             HashMap<String, String> map = new HashMap<String, String>();
                    map.put("deviceID", cursor.getString(0));
                    map.put("deviceName", cursor.getString(1));
                    deviceList.add(map);

                }
                while (cursor.moveToNext());
            }

            return deviceList;
        }

    //updating single device
        //this method accepts Device class object as parameter
        public int updateDevice(HashMap<String, String> queryValues) {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("deviceName", queryValues.get("deviceName"));

            //updating row
            return db.update("devices", values, "deviceID" + " = ?", new String[] { queryValues.get("deviceID")});
        }

    public HashMap<String, String> getDeviceInfo(String id) {
            HashMap<String, String>

 wordList = new HashMap<String, String>();
        SQLiteDatabase db = this.getReadableDatabase();
        String selectQuery = "SELECT * FROM devices WHERE deviceID ='" + id + "'";
        //Cursor cursor = db.rawQuery(sql, selectionArgs)
        Cursor cursor = db.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {
                //wordList.put("deviceName", cursor.getString(1));
                wordList.put("deviceID", cursor.getString(1));
            } while (cursor.moveToNext());
        }
        return wordList;
    }

我在ControllerTab类中有这些输出:

DatabaseHandler db = new DatabaseHandler(this);


        // Inserting Devices
        Log.d("Insert: ", "Inserting .."); 
        db.addDevice(new Device(1, "DB Device 1"));        
        db.addDevice(new Device(2, "DB Device 2"));
        db.addDevice(new Device(3, "DB Device 3"));

ArrayList<HashMap<String, String>> deviceList = db.getAllDevices();
        if(deviceList.size()!=0) {
            ListView list = getListView();
            list.setOnItemClickListener(new OnItemClickListener() {
                  @Override 
                  public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                      deviceID = (TextView) view.findViewById(R.id.deviceId);
                      String valdeviceID = deviceID.getText().toString();                     
                      Intent  objIndent = new Intent(getApplicationContext(),EditDeviceName.class);
                      objIndent.putExtra("deviceID", valdeviceID); 
                      startActivity(objIndent); 
                  }
            }); 
            ListAdapter adapter = new SimpleAdapter( ControllerTab.this, deviceList, R.layout.activity_main_view, new String[] { "deviceId","deviceName"}, new int[] {R.id.deviceId, R.id.deviceName}); 
            setListAdapter(adapter);

非常感谢任何帮助或建议。谢谢。

1 个答案:

答案 0 :(得分:0)

仅当传递给onUpgrade()超类构造函数的版本号大于存储在数据库文件中的版本号时,才会调用

SQLiteOpenHelper。您的DATABASE_VERSION1,因此无法调用。另见:When is SQLiteOpenHelper onCreate() / onUpgrade() run?

如果您需要使用数据预填充数据库,请在数据库帮助程序onCreate()中执行此操作。它仅在第一次创建数据库时运行。