마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

SQLite 오류 질문이요

0 추천

리스트뷰를 데이터베이스에 넣고 불러오는중인데 잘안되네요

어디서 틀린건지 잘모르겠습니다

혹시 몰라 소스 올려봅니다

MainActivity

 

 

 

 

 

 

이름 하나가지고만하면 되는데 Double형인 x와 y를 추가하면 그때부터 안되네요...

bowwow (220 포인트) 님이 2017년 1월 16일 질문
글자 읽을 수 있는 분 계세요?
개발을 죽어라 했더니 노안이...ㅠㅠ
아이고 내눈알아...너무 화질구지네요 ㅋㅋㅋ...
안보여서 이곳에다가 적을게요 ㅠㅠ


private void doWhileCursorToArray() {

        mCursor = null;
        mCursor = mDbOpenHelper.getAllColumns();
        Log.i(TAG, "Count = " + mCursor.getCount());

        while (mCursor.moveToNext()) {
            mInfoClass = new InfoClass(
                    mCursor.getInt(mCursor.getColumnIndex("_id")),
                    mCursor.getString(mCursor.getColumnIndex("name")),
                    mCursor.getDouble(mCursor.getColumnIndex("x")),
                    mCursor.getDouble(mCursor.getColumnIndex("y")),
                    mCursor.getFloat(mCursor.getColumnIndex("r")),
                    mCursor.getString(mCursor.getColumnIndex("day")),
                    mCursor.getInt(mCursor.getColumnIndex("fh")),
                    mCursor.getInt(mCursor.getColumnIndex("fm")),
                    mCursor.getInt(mCursor.getColumnIndex("lh")),
                    mCursor.getInt(mCursor.getColumnIndex("lm")),
                    mCursor.getString(mCursor.getColumnIndex("sound")),
                    mCursor.getString(mCursor.getColumnIndex("mode")),
                    mCursor.getString(mCursor.getColumnIndex("cs")),
                    mCursor.getString(mCursor.getColumnIndex("cm"))
            );
            mInfoArr.add(mInfoClass);
        }
        mCursor.close();
    }




public class DataBases {
    public static final class CreateDB implements BaseColumns{
        public static final String NAME ="name";
        public static final String X ="x";
        public static final String Y ="y";
        public static final String R ="r";
        public static final String DAY ="day";
        public static final String FH ="fh";
        public static final String FM ="fm";
        public static final String LH ="lh";
        public static final String LM ="lm";
        public static final String SOUND ="sound";
        public static final String MODE ="mode";
        public static final String SWITCH ="cs";
        public static final String MESSAGE ="cm";
        public static final String _TABLENAME ="mytable";

        public static final String _CREATE=
                "create table " + _TABLENAME + "("
                + _ID + " integer primary key autoincrement, "
                + NAME + " text not null,"
                + X + " text not null, "
                + Y + " text not null, "
                + R + " text not null, "
                + DAY + " text not null, "
                + FH + " text not null, "
                + FM + " text not null, "
                + LH + " text not null, "
                + LM + " text not null, "
                + SOUND + " text not null, "
                + MODE + " text not null, "
                + SWITCH + " text not null, "
                + MESSAGE + " text not null, );";
    }
}


    public InfoClass(int _id, String name, Double x, Double y, float r, String day, int fh, int fm, int lh, int lm, String sound, String mode, String cs, String cm) {
        this._id = _id;
        this.name = name;
        this.x = x;
        this.y = y;
        this.r = r;
        this.day = day;
        this.fh = fh;
        this.fm = fm;
        this.lh = lh;
        this.lm = lm;
        this.sound = sound;
        this.mode = mode;
        this.cs = cs;
        this.cm = cm;
    }


public long insertColumn(String name, Double x, Double y, Float radius, String dayText,
                             Integer firstH, Integer firstM, Integer lastH, Integer lastM,
                             String sound, String mode, String cs, String cm) {
        ContentValues values = new ContentValues();
        values.put(DataBases.CreateDB.NAME, name);
        values.put(DataBases.CreateDB.X, x);
        values.put(DataBases.CreateDB.Y, y);
        values.put(DataBases.CreateDB.R, radius);
        values.put(DataBases.CreateDB.DAY, dayText);
        values.put(DataBases.CreateDB.FH, firstH);
        values.put(DataBases.CreateDB.FM, firstM);
        values.put(DataBases.CreateDB.LH, lastH);
        values.put(DataBases.CreateDB.LM, lastM);
        values.put(DataBases.CreateDB.SOUND, sound);
        values.put(DataBases.CreateDB.MODE, mode);
        values.put(DataBases.CreateDB.SWITCH, cs);
        values.put(DataBases.CreateDB.MESSAGE, cm);
        return mDB.insert(DataBases.CreateDB._TABLENAME, null, values);
    }

    public boolean updateColumn(long id, String name, Double x, Double y, Double radius, String dayText,
                                Integer firstH, Integer firstM, Integer lastH, Integer lastM,
                                String sound, String mode, String cs, String cm) {
        ContentValues values = new ContentValues();
        values.put(DataBases.CreateDB.NAME, name);
        values.put(DataBases.CreateDB.X, x);
        values.put(DataBases.CreateDB.Y, y);
        values.put(DataBases.CreateDB.R, radius);
        values.put(DataBases.CreateDB.DAY, dayText);
        values.put(DataBases.CreateDB.FH, firstH);
        values.put(DataBases.CreateDB.FM, firstM);
        values.put(DataBases.CreateDB.LH, lastH);
        values.put(DataBases.CreateDB.LM, lastM);
        values.put(DataBases.CreateDB.SOUND, sound);
        values.put(DataBases.CreateDB.MODE, mode);
        values.put(DataBases.CreateDB.SWITCH, cs);
        values.put(DataBases.CreateDB.MESSAGE, cm);
        return mDB.update(DataBases.CreateDB._TABLENAME, values, "_id=" + id, null) > 0;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        if (v == null) {
            holder = new ViewHolder();
            v = mInflater.inflate(R.layout.listitem, null);
            holder.name = (TextView) v.findViewById(R.id.lvName);
            holder.x = (TextView) v.findViewById(R.id.lvX);
            holder.y = (TextView) v.findViewById(R.id.lvY);
            holder.r = (TextView) v.findViewById(R.id.lvR);
            holder.day = (TextView) v.findViewById(R.id.lvDay);
            holder.fh = (TextView) v.findViewById(R.id.lvFH);
            holder.fm = (TextView) v.findViewById(R.id.lvFM);
            holder.lh = (TextView) v.findViewById(R.id.lvLH);
            holder.lm = (TextView) v.findViewById(R.id.lvLM);
            holder.sound = (TextView) v.findViewById(R.id.lvSound);
            holder.mode = (TextView) v.findViewById(R.id.lvMode);
            holder.cs = (TextView) v.findViewById(R.id.lvCallSwitch);
            holder.cm = (TextView) v.findViewById(R.id.lvCallMessage);

            v.setTag(holder);
        } else {
            holder = (ViewHolder)v.getTag();
        }

        InfoClass info = InfoArr.get(position);

        holder.name.setText(info.name);
        holder.x.setText(info.x.toString());
        holder.y.setText(info.y.toString());
        holder.r.setText(info.r.toString());
        holder.day.setText(info.day);
        holder.fh.setText(info.fh);
        holder.fm.setText(info.fm);
        holder.lh.setText(info.lh);
        holder.lm.setText(info.lm);
        holder.sound.setText(info.sound);
        holder.mode.setText(info.mode);
        holder.cs.setText(info.cs);
        holder.cm.setText(info.cm);

        return v;
    }

1개의 답변

0 추천
x와 y를 추가하고나서 안된다고 하셨죠?

혹시 SqliteOpenHelper에서 onUpgrade에 대한 처리 하셨나요?

디버그로 디바이스에 apk 밀어넣을때 update처럼 처리되므로 onUpgrade에 대한 처리가 필요합니다.

아니면 앱을 아예 삭제 한 후에 다시 밀어넣으세요 그럼 정상적으로 onCreate를 탈겁니다.
Development Guy (70,570 포인트) 님이 2017년 1월 17일 답변
네 onCreate와 마찬가지로 이렇게 했습니다
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS" + DataBases.CreateDB._TABLENAME);
            onCreate(db);
        }
OpenHelper에 생성자로 들어가는 versioncode도 올리셨나요?
이 버전코드가 올라가야 onUpgrade를 정상적으로 탈거에요

public class DbOpenHelper {
    private static final String DATABASE_NAME = "didyoucome.db";
    private static final int DATABASE_VERSION = 1;
    public static SQLiteDatabase mDB;
    private DataBaseHelper mDBHelper;
    private Context mCtx;

    private class DataBaseHelper extends SQLiteOpenHelper {

        public DataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }
계속 이부분에서 오류가나네요
    public Cursor getAllColumns() {
        return mDB.query(DataBases.CreateDB._TABLENAME, null, null, null, null, null, null);
    }
위에 올리신 stacktrace 보면 거기가 아니라
getColumnIndex("x") 라인에서 인데요;;
명백하게 요녀석이 -1을 리턴하고 있는데...
앱 한번 완전삭제 하시고 다시 설치해서 해보세요
그리고 앱 완전삭제하고 다시 했을때 된다면 onUpgrade에 대한 처리 제대로 안된겁니다.
...