Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite returned: error code = 1, msg = no such column:kitchen1

I've been getting this error for two days now, and I can't find the problem with my code.

here's the database code.

SQLHandler.java

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SQLHandler {
    public static final String KEY_ROOMMOVEHOLDER = "roommoveholder";
    public static final String KEY_ROOM = "room";

    public static final String KEY_ITEMMOVEHOLDER = "itemmoveholder";
    public static final String KEY_ITEMNAME = "itemname";
    public static final String KEY_ITEMVALUE = "itemvalue";
    public static final String KEY_ROOMHOLDER = "roomholder";

    public static final String KEY_MOVENAME = "movename";
    public static final String KEY_ID1 = "_id";
    public static final String KEY_ID2 = "_id";
    public static final String KEY_ID3 = "_id";
    public static final String KEY_ID4 = "_id";
    public static final String KEY_MOVEDATE = "movedate";

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

    public static final String KEY_TODOMOVE = "todomove";
    public static final String KEY_SORTANDPURGE = "sortandpurge";
    public static final String KEY_RESEARCH = "research";
    public static final String KEY_CREATEMOVINGBINDER = "createmovingbinder";
    public static final String KEY_ORDERSUPPLIES = "ordersupplies";
    public static final String KEY_USEITORLOSEIT = "useitorloseit";
    public static final String KEY_TAKEMEASUREMENTS = "takemeasurements";
    public static final String KEY_CHOOSEMOVER = "choosemover";
    public static final String KEY_BEGINPACKING = "beginpacking";
    public static final String KEY_LABEL = "label";
    public static final String KEY_SEPARATEVALUES = "separatevalues";
    public static final String KEY_DOACHANGEOFADDRESS = "doachangeofaddress";
    public static final String KEY_NOTIFYIMPORTANTPARTIES = "notifyimportantparties";

    private static final String DATABASE_TABLE1 = "movingname";
    private static final String DATABASE_TABLE2 = "movingrooms";
    private static final String DATABASE_TABLE3 = "movingitems";
    private static final String DATABASE_TABLE4 = "todolist";

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

    public static final String CREATE_TABLE_2 = "CREATE TABLE " + DATABASE_TABLE2 + " (" + 
            KEY_ID2 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
            KEY_ROOMMOVEHOLDER + " TEXT NOT NULL , " + 
            KEY_ROOM + " TEXT NOT NULL);";

    public static final String CREATE_TABLE_3 = "CREATE TABLE " + DATABASE_TABLE3 + " (" + 
            KEY_ID3 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
            KEY_ITEMNAME + " TEXT NOT NULL, " + 
            KEY_ITEMVALUE + " TEXT NOT NULL, " +
            KEY_ROOMHOLDER + " TEXT NOT NULL, " +   
            KEY_ITEMMOVEHOLDER + " TEXT NOT NULL);";

    public static final String CREATE_TABLE_4 = "CREATE TABLE " + DATABASE_TABLE4 + " (" + 
            KEY_ID4 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
            KEY_TODOMOVE + " TEXT NOT NULL, " +
            KEY_SORTANDPURGE + " TEXT NOT NULL, " + 
            KEY_RESEARCH + " INTEGER NOT NULL, " +
            KEY_CREATEMOVINGBINDER + " TEXT NOT NULL, " + 
            KEY_ORDERSUPPLIES + " TEXT NOT NULL, " +
            KEY_USEITORLOSEIT + " TEXT NOT NULL, " + 
            KEY_TAKEMEASUREMENTS + " TEXT NOT NULL, " +
            KEY_CHOOSEMOVER + " TEXT NOT NULL, " + 
            KEY_BEGINPACKING + " TEXT NOT NULL, " +
            KEY_LABEL + " TEXT NOT NULL, " + 
            KEY_SEPARATEVALUES + " TEXT NOT NULL, " +
            KEY_DOACHANGEOFADDRESS + " TEXT NOT NULL, " + 
            KEY_NOTIFYIMPORTANTPARTIES + " 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_2);
            db.execSQL(CREATE_TABLE_3);
            db.execSQL(CREATE_TABLE_4);
        }

        @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_TABLE2);
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE3);
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE4);
            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(){
        ourHelper.close();
    }

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

    public long addRooms(String sroommoveholder, String sroom){
        ContentValues cv = new ContentValues();
        cv.put(KEY_ROOMMOVEHOLDER, sroommoveholder);
        cv.put(KEY_ROOM, sroom);
        return ourDatabase.insert(DATABASE_TABLE2, null, cv);
    }

    public long addItems(String sitemmoveholder, String sroomholder, String sitemname, String sitemvalue){
        ContentValues cv = new ContentValues();
        cv.put(KEY_ITEMMOVEHOLDER, sitemmoveholder);
        cv.put(KEY_ROOMHOLDER, sroomholder);
        cv.put(KEY_ITEMNAME, sitemname);
        cv.put(KEY_ITEMVALUE, sitemvalue);
        return ourDatabase.insert(DATABASE_TABLE3, null, cv);
    }

    public long todoList(String todoitem){
        ContentValues cv = new ContentValues();
        cv.put(todoitem, "Done");
        return ourDatabase.insert(DATABASE_TABLE4, null, cv);
    }

    public Cursor getMove(){
        String[] columns = new String[]{KEY_ID1, KEY_MOVENAME};
        Cursor cursor = ourDatabase.query(DATABASE_TABLE1, columns, null, null, null, null, null);
        return cursor;
    }

    public String getRoom(String r) throws SQLException{
        String roomName = null;
        String[] columns = new String[]{KEY_ID2, KEY_ROOMMOVEHOLDER, KEY_ROOM};
        Cursor c = ourDatabase.query(DATABASE_TABLE2, columns, KEY_ROOM + "=" + r , null, null, null, null);

        if (c != null) {
            c.moveToFirst();
            roomName = c.getString(2);
        }
        return roomName;
    }

}

the error occurs everytime i call this method

public String getRoom(String r) throws SQLException{
        String roomName = null;
        String[] columns = new String[]{KEY_ID2, KEY_ROOMMOVEHOLDER, KEY_ROOM};
        Cursor c = ourDatabase.query(DATABASE_TABLE2, columns, KEY_ROOM + "=" + r , null, null, null, null);

        if (c != null) {
            c.moveToFirst();
            roomName = c.getString(2);
        }
        return roomName;
    }

the reason for this must be the declaration of the table, but I'm not sure. I checked the database using adb shell and i can't see the table exists and has a value inside it but the error says no such column exist.

like image 775
philip Avatar asked Sep 24 '12 02:09

philip


1 Answers

If room is a character type (and it is, according to the CREATE_TABLE_2 string where it's defined as TEXT NOT NULL), you need to replace:

KEY_ROOM + "=" + r

with:

KEY_ROOM + "= '" + r + "'"

The way you have it, you're ending up with the query segment:

where room = kitchen1

and it's complaining that there's no kitchen1 column in that table, rightly so.

By quoting it, you end up with the correct:

where room = 'kitchen1'

This will turn the cursor creation line into:

Cursor c = ourDatabase.query(DATABASE_TABLE2, columns,
    KEY_ROOM + "='" + r + "'", null, null, null, null);
//  ^^^^^^^^^^^^^^^^^^^^^^^^^
//         Changed bit
like image 117
paxdiablo Avatar answered Oct 02 '22 18:10

paxdiablo