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

private static final int DATABASE_VERSION = 1;

この値はまだ 1 です。これは、データベースの作成時に使用した値であると想定しています。onCreate() は、デバイスで DbHelper が初めて呼び出されたときにのみ呼び出されます。DATABASE_VERSION の値が変更されない限り、onUpgrade は呼び出されません。これは、新しいテーブルが実際にはデータベースに作成されていないことを意味します。

DATABASE_VERSION = 2 を設定してみてください。

- - - 編集 - - -

DATABASE_TABLE5 の値にタイプミスがあるようです。

private static final String DATABASE_TABLE5 = "lisitem";

----クエリが 0 の結果を返す場合、for ループは失敗します-----

使用する:

if(c.moveToFirst()){

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

}

于 2012-10-18T02:20:10.240 に答える