1

When I try to run my app, it crashes.

E/SQLiteLog﹕ (1) no such column: nmb

Here's my DB code

private static final String DB_NAME = "mydb";
private static final int DB_VERSION = 4;
private static final String DB_TABLE = "mytab";

public static final String COLUMN_ID = "_id";
public static final String COLUMN_IMG = "img";
public static final String COLUMN_TXT = "txt";
public static final String COLUMN_NMB = "nmb";

private static final String DB_CREATE =
        "create table " + DB_TABLE + "(" +
                COLUMN_ID + " integer primary key autoincrement, " +
                COLUMN_IMG + " integer, " +
                COLUMN_TXT + " text, " +
                COLUMN_NMB + " number" +
                ");";

private final Context mCtx;


private DBHelper mDBHelper;
private SQLiteDatabase mDB1;

public DB2(Context ctx) {
    mCtx = ctx;
}

public void open() {
    mDBHelper = new DBHelper(mCtx, DB_NAME, null, DB_VERSION);
    mDB1 = mDBHelper.getWritableDatabase();
}

public void close() {
    if (mDBHelper!=null) mDBHelper.close();
}



public Cursor getAllData() {
    String[] columns = {"_id","txt","img","nmb"};
    return mDB1.query(DB_TABLE, columns, null, null, null, null, null);
}

public void addRec(String txt, int img, String nmb) {
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_TXT, txt);
    cv.put(COLUMN_IMG, img);
    cv.put(COLUMN_NMB, nmb);
    mDB1.insert(DB_TABLE, null, cv);
}

public void delRec(long id) {
    mDB1.delete(DB_TABLE, COLUMN_ID + " = " + id, null);
}

private class DBHelper extends SQLiteOpenHelper {

    public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,
                    int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DB_CREATE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}

This is my activity code

private static final int CM_DELETE_ID = 1;
ListView lvData;
DB2 db;
SimpleCursorAdapter scAdapter;
Cursor cursor;
EditText et,nmb;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test4);
    et =(EditText)findViewById(R.id.editText2);
    nmb = (EditText)findViewById(R.id.nmb);
    db = new DB2(this);
    db.open();

    cursor = db.getAllData();
    startManagingCursor(cursor);

    String[] from = new String[] { DB2.COLUMN_IMG, DB2.COLUMN_TXT, DB2.COLUMN_NMB };
    int[] to = new int[] { R.id.ivImg, R.id.tvText, R.id.tvNmb };

    scAdapter = new SimpleCursorAdapter(this, R.layout.item, cursor, from, to);
    lvData = (ListView) findViewById(R.id.lvData);
    lvData.setAdapter(scAdapter);

    registerForContextMenu(lvData);

}



public void onButtonClick(View view) {
    db.addRec(et.getText().toString(), R.mipmap.ic_launcher, nmb.getText().toString());
    cursor.requery();
}

public void onCreateContextMenu(ContextMenu menu, View v,
                                ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, CM_DELETE_ID, 0, R.string.delete_record);
}

public boolean onContextItemSelected(MenuItem item) {
    if (item.getItemId() == CM_DELETE_ID) {
        AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        db.delRec(acmi.id);
        cursor.requery();
        return true;
    }
    return super.onContextItemSelected(item);
}

protected void onDestroy() {
    super.onDestroy();
    db.close();
}

In my first DB_VERSION, I didn't have COLUMN_NMB and it was working. Also in "getAllData()" cursor public I only had return mDB1.query(DB_TABLE, null, null, null, null, null, null);. After I've added nmb string, it keeps crashing

2 Answers2

5

Wild guess: you possibly added that column in a second moment.
Uninstall and reinstall your app.

This is because the existing database won't be touched, if you don't reinstall the app.
Or if you don't set a higher DATABASE_VERSION value.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
1

Uninstall and reinstall your application, probably you created a diferent database structure previously that doesn´t contain the nmb field.

Then run your application and the new database structure containing the nmb field will be created.

this is a very common issue :)

Jorgesys
  • 124,308
  • 23
  • 334
  • 268