Java SQLite中的升级在我的应用程序中似乎不起作用

Java SQLite中的升级在我的应用程序中似乎不起作用,java,android,database,eclipse,sqlite,Java,Android,Database,Eclipse,Sqlite,我正在学习一个针对移动应用程序的SQLite入门教程,但输出仍然有问题。数据将显示在列表视图上,但每次我从Eclipse运行到手机时,数据都会被添加,而不是有一个3个列表,每次我从Eclipse运行应用程序时,它都会有一个6、9、12、15等列表的输出。我猜SQLite升级不会删除旧表 我的数据库中有以下代码: public class DatabaseHandler extends SQLiteOpenHelper { //all static variable

我正在学习一个针对移动应用程序的SQLite入门教程,但输出仍然有问题。数据将显示在列表视图上,但每次我从Eclipse运行到手机时,数据都会被添加,而不是有一个3个列表,每次我从Eclipse运行应用程序时,它都会有一个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;
    }
公共类DatabaseHandler扩展了SQLiteOpenHelper
{
//所有静态变量;数据库版本
私有静态最终int数据库_VERSION=1;
//数据库名称
私有静态最终字符串数据库_NAME=“devicesqlite.db”;
//Logcat标签
私有静态最终字符串LOGCAT=null;
//对于开关//控制器选项卡中的相同
私有最终静态整数[]id={R.id.switch1,R.id.switch2,R.id.switch3};
公共数据库处理程序(上下文应用程序上下文){
//超级(上下文、名称、工厂、版本);
super(applicationContext,数据库名称,null,数据库版本);
}
@凌驾
public void onCreate(SQLiteDatabase db){
//字符串CREATE_DEVICES_TABLE=“CREATE TABLE”+TABLE_DEVICES+”(“+KEY_ID+”整数主键“+KEY_NAME+”文本“+”);
String createQuery=“创建表设备(deviceID整数主键,deviceName文本)”;
execSQL(createQuery);
Log.d(LOGCAT,“创建的表设备”);
}
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
//删除旧表(如果存在)
字符串dropQuery=(“如果存在设备,则删除表”);
execSQL(dropQuery);
onCreate(db);//再次创建表
}
//添加新设备
公用设备(设备){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues=新的ContentValues();
value.put(“deviceName”,device.getName());
db.insert(“设备”,null,值);//插入行
db.close();
}
//获取所有设备
公共阵列列表getAllDevices(){
////ArrayList deviceList=新的ArrayList();
魔术师;
deviceList=新的ArrayList();
//选择所有查询
String selectQuery=“选择*来自设备”;
SQLiteDatabase db=this.getWritableDatabase();
Cursor Cursor=db.rawQuery(selectQuery,null);
//循环遍历所有行并添加到列表
if(cursor.moveToFirst()){
做
{
HashMap=newHashMap();
put(“deviceID”,cursor.getString(0));
map.put(“deviceName”,cursor.getString(1));
添加(地图);
}
while(cursor.moveToNext());
}
回归魔鬼主义;
}
//更新单个设备
//此方法接受设备类对象作为参数
public int updateDevice(HashMap查询值){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues=新的ContentValues();
value.put(“deviceName”,queryValue.get(“deviceName”);
//更新行
返回db.update(“设备”,值,“设备ID”+“=?”,新字符串[]{queryValues.get(“设备ID”)});
}
公共HashMap getDeviceInfo(字符串id){
哈希图
wordList=newhashmap();
SQLiteDatabase db=this.getReadableDatabase();
String selectQuery=“从设备中选择*,其中设备id=”+id+”;
//Cursor Cursor=db.rawQuery(sql,selectionArgs)
Cursor Cursor=db.rawQuery(selectQuery,null);
if(cursor.moveToFirst()){
做{
//put(“deviceName”,cursor.getString(1));
put(“deviceID”,cursor.getString(1));
}while(cursor.moveToNext());
}
返回词表;
}
在我的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);
DatabaseHandler db=新的DatabaseHandler(此);
//插入设备
日志d(“插入:”,“插入..”);
db.addDevice(新设备(1,“db设备1”));
db.addDevice(新设备(2,“db设备2”));
db.addDevice(新设备(3,“db设备3”));
ArrayList deviceList=db.getAllDevices();
如果(deviceList.size()!=0){
ListView list=getListView();
list.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
deviceID=(TextView)view.findViewById(R.id.deviceID);
字符串valdeviceID=deviceID.getText().toString();
Intent objIndent=newintent(getApplicationContext(),EditDeviceName.class);
objIndent.putExtra(“设备ID”,valdeviceID);
星形触觉(明显);
}
}); 
ListAdapter=new SimpleAdapter(ControllerTab.this,deviceList,R.layout.activity_main_视图,新字符串[]{“deviceId”,“deviceName”},新int[]{R.id.deviceId,R.id.deviceName});
setListAdapter(适配器);
任何帮助或建议都将不胜感激。谢谢。

onUpgrade()
仅当传递给
SQLiteOpenHelper
超类构造函数的版本号大于存储在数据库文件中的版本号时才会调用。您的
数据库\u版本
1
,因此不会调用它。另请参阅: