2

自从我在数据库中添加了一个新表后,我就一直收到这个错误,不知道为什么。我已经做了几次这种方法,没有遇到任何错误,但现在发生了这种情况,sqlite 就像女人一样,你第一次做某事,一切看起来都很好,但是当你再次做同样的事情时,他们就会开始为之疯狂一些理由。

无论如何,这里有一些相关的代码:

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selectorcreatemove);

        allFunctions();

    }

    private void allFunctions() {
        addMove = (Button) findViewById(R.id.bAddMove);
        etAddMove = (EditText) findViewById(R.id.etMoveName);

        MoveList = (ListView) findViewById(R.id.lvMoveItems);
        SQLHandlerview = new SQLHandler(this);

        SQLHandlerview = new SQLHandler(ListMovingNames.this);
        SQLHandlerview.open();

        cursor = SQLHandlerview.getMove();
        startManagingCursor(cursor);

        String[] from = new String[]{SQLHandler.KEY_MOVENAME};
        int[] to = new int[]{R.id.text};

        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
        MoveList.setAdapter(cursorAdapter);

        addMove.setOnClickListener(new OnClickListener() {

            @SuppressLint("NewApi")
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

        MoveList.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, final View view, int position,
                    long id) {
                // TODO Auto-generated method stub
                AlertDialog load = new AlertDialog.Builder(ListMovingNames.this).show();
                load.setContentView(R.layout.load);
                move = ((TextView) view).getText().toString();
                Intent i = new Intent(getApplicationContext(), StartMoving.class);
                i.putExtra("moveName", move);
                startActivity(i);               
            }
        });

        etAddMove.setOnKeyListener(new OnKeyListener() {

            @SuppressLint("NewApi")
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // TODO Auto-generated method stub
                if ( (event.getAction() == KeyEvent.ACTION_DOWN  ) && (keyCode== KeyEvent.KEYCODE_ENTER)   )
                {               
                    String ssmoveName = etAddMove.getText().toString();
                    int x = ssmoveName.length() - 1;
                    if (ssmoveName.equals("") || Character.isWhitespace(ssmoveName.charAt(0)) || Character.isWhitespace(ssmoveName.charAt(x))) {
                        Toast.makeText(ListMovingNames.this, 
                                "Please enter a valid name! Avoid giving a blank name or white space at the beginning or end of the name", 
                                Toast.LENGTH_LONG).show();
                    }else{
                        try {
                            String scheck = SQLHandlerview.checkMove(ssmoveName);
                            if (scheck.equals(ssmoveName)) {
                                Toast.makeText(ListMovingNames.this, "Move name already exist please give a different name", Toast.LENGTH_LONG).show();
                            } else{
                                AlertDialog load = new AlertDialog.Builder(ListMovingNames.this).show();
                                load.setContentView(R.layout.load);
                                SQLHandlerview.createMove(ssmoveName);
                                SQLHandlerview.createList();
                                SQLHandlerview.setTodo(ssmoveName);
                                Intent i = new Intent(getApplicationContext(), StartMoving.class);
                                i.putExtra("moveName", ssmoveName);
                                startActivity(i);
                            }
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            AlertDialog load = new AlertDialog.Builder(ListMovingNames.this).show();
                            load.setContentView(R.layout.load);
                            SQLHandlerview.createMove(ssmoveName);
                            SQLHandlerview.createList();
                            SQLHandlerview.setTodo(ssmoveName);
                            Intent i = new Intent(getApplicationContext(), StartMoving.class);
                            i.putExtra("moveName", ssmoveName);
                            startActivity(i);
                        }
                    }
                    return true;
                }
                return false;
            }
        });

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        allFunctions();
        SQLHandlerview.open();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        if (SQLHandlerview != null) {
            SQLHandlerview.close();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            // do something on back.

            Intent i = new Intent(ListMovingNames.this, MainActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

}

这是 sql 处理程序

public class SQLHandler {

    public static final String KEY_MOVENAME = "movename";
    public static final String KEY_ID1 = "_id";
    public static final String KEY_ID5 = "_id";

    public static final String KEY_MOVEDATE = "movedate";
    public static final String KEY_TOTALMOVEWEIGHT = "totalmoveweight";
    public static final String KEY_TOTALITEM = "totalitem";

    private static final String DATABASE_NAME = "mymovingfriend";
    private static final int DATABASE_VERSION = 1;

    public static final String KEY_LISTITEMNAME = "listitemname";
    public static final String KEY_LISTITEMWEIGHT = "listitemweight";
    public static final String KEY_LISTITEMROOM = "listitemroom";

    private static final String DATABASE_TABLE1 = "movingname";
    private static final String DATABASE_TABLE5 = "lisitem";

    public static final String CREATE_TABLE_1 = "CREATE TABLE " + DATABASE_TABLE1 + " (" + 
            KEY_ID1 + " INTEGER PRIMARY KEY AUTOINCREMENT," + 
            KEY_MOVEDATE + " TEXT NOT NULL, " + 
            KEY_TOTALMOVEWEIGHT + " TEXT NOT NULL, " + 
            KEY_TOTALITEM + " INTEGER NOT NULL, " +
            KEY_MOVENAME + " TEXT NOT NULL);";

    public static final String CREATE_TABLE_5 = "CREATE TABLE " + DATABASE_TABLE5 + " (" + 
            KEY_ID5 + " INTEGER PRIMARY KEY AUTOINCREMENT," + 
            KEY_LISTITEMNAME + " TEXT NOT NULL, " + 
            KEY_LISTITEMWEIGHT + " TEXT NOT NULL, " +
            KEY_LISTITEMROOM + " TEXT NOT NULL);";

    private DbHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;

    private static class DbHelper extends SQLiteOpenHelper{

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

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

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE1);
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE5);
            onCreate(db);
        }
    }

    public SQLHandler(Context c){
        ourContext = c;
    }

    public SQLHandler open() throws SQLException{
        ourHelper = new DbHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;
    }

    public void close(){
        ourDatabase.close();
        ourHelper.close();
    }

    public long createMove(String smovename){
        ContentValues cv = new ContentValues();
        cv.put(KEY_MOVENAME, smovename);
        cv.put(KEY_MOVEDATE, "Not yet set");
        cv.put(KEY_TOTALMOVEWEIGHT, "0");
        cv.put(KEY_TOTALITEM, 0);
        return ourDatabase.insert(DATABASE_TABLE1, null, cv);
    }

    public void createList(){
        ContentValues cv = new ContentValues();

        String[] sroom = new String[]{"Kitchen", "Bedroom", "Dinning Room"};
        String[] sitem = new String[]{"Dishwasher", "Bed", "Table"};
        String[] sweight = new String[]{"40", "25", "15"};

        for (int i = 0; i < sroom.length; i++) {
            cv.put(KEY_LISTITEMROOM, sroom[i]);
            cv.put(KEY_LISTITEMNAME, sitem[i]);
            cv.put(KEY_LISTITEMWEIGHT, sweight[i]);
            ourDatabase.insert(DATABASE_TABLE5, null, cv);
        }
    }

    public ArrayList<String> getitems(){
        String[] columns = new String[]{KEY_ID5, KEY_ITEMNAME};
        Cursor c = ourDatabase.query(DATABASE_TABLE5, columns, null, null, null, null, null);
        ArrayList<String> items = new ArrayList<String>();

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            items.add(c.getString(1));
        }

        return items;
    }

    public ArrayList<String> getitemweight(){
        String[] columns = new String[]{KEY_ID5, KEY_ITEMWEIGHT};
        Cursor c = ourDatabase.query(DATABASE_TABLE5, columns, null, null, null, null, null);
        ArrayList<String> items = new ArrayList<String>();

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            items.add(c.getString(1));
        }

        return items;
    }

    public ArrayList<String> getitemclass(){
        String[] columns = new String[]{KEY_ID5, KEY_LISTITEMROOM};
        Cursor c = ourDatabase.query(DATABASE_TABLE5, columns, null, null, null, null, null);
        ArrayList<String> items = new ArrayList<String>();

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            items.add(c.getString(1));
        }

        return items;
    }

}
4

1 回答 1

2

私有静态最终 int DATABASE_VERSION = 1;

This value is still equal to 1. I'm assuming that is the value you used when the database was created. onCreate() is only called the first time that DbHelper is called on the device. onUpgrade won't be called unless the value of DATABASE_VERSION is changed. This means that your new table was never actually created in the database.

Try setting DATABASE_VERSION = 2.

------EDIT-----

There appears to be a typo in the value of DATABASE_TABLE5.

private static final String DATABASE_TABLE5 = "lisitem";

----for loops will fail if query returns 0 results-----

use:

if(c.moveToFirst()){

do{
    items.add(c.getString(1);
    c.moveToNext();
}while(!c.isAfterLast());

}

于 2012-10-18T02:20:10.240 回答