I am trying to add a key to my SQLite DB but when I try to access the new column, the program throws "no such column type (code 1)". I verified in SQLite Editor that the new column is in fact present. Am I missing something?
SOLUTION FOUND My Database creation was NOT taking in the DATABASE_VERSION, I had simply set it to use "1". As soon as I set it to use that variable, and changed it from 1 to 2, it updated my database!
Here is my class
package com.example.blizz_000.lureorganizer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.sql.SQLException;
public class LureOrganizer {
public com.example.blizz_000.lureorganizer.DatabaseHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
public static final String KEY_NAME = "model";
public static final String KEY_MAKER = "company";
public static final String KEY_SIZE = "size";
public static final String KEY_COLOR = "color";
public static final String KEY_TYPE = "TEST";
public static final String KEY_ROWID = "_id";
private static final String DATABASE_NAME = "fishing_db";
private static final String DATABASE_TABLE = "LURES";
public static int DATABASE_VERSION = 1;
// DATABASE HELPER CLASS*********************************************************
private static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE +" (" +
KEY_ROWID +"INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT, " +
KEY_MAKER + " TEXT, " +
KEY_COLOR + " TEXT, " +
"TEST text, " +
KEY_SIZE + " TEXT);");
//db.execSQL("ALTER TABLE LURES ADD COLUMN TEST TEST"); // Won't find the table
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
// END DATABASE HELPER CLASS *********************************************************
//*********************BEGIN MY METHODS**************************
public LureOrganizer(Context c) {
ourContext = c;}
//Opens Db
public LureOrganizer open() throws SQLException{
ourHelper = new com.example.blizz_000.lureorganizer.DatabaseHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;}
//Opens Db NO EXCEPTION
public LureOrganizer opennoe() {
ourHelper = new com.example.blizz_000.lureorganizer.DatabaseHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;}
//Closes Db
public void close() {
ourHelper.close();}
//Entries
public long createEntry(String name, String maker, String color, String size, String type) {
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_MAKER, maker);
cv.put(KEY_COLOR, color);
cv.put(KEY_SIZE, size);
cv.put(KEY_TYPE, type);
return ourDatabase.insert(DATABASE_TABLE, null, cv);}
//RETRIEVE ALL PIECES OF THE DATA *****************************************************************************************************//
//Retrive Data for SINGLE PIECE
public String[] getData(String id) {
String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_MAKER, KEY_COLOR, KEY_SIZE};
Cursor d = ourDatabase.rawQuery("select * from LURES WHERE _id = ?",
new String[]{id});
String[] resultarray = new String[5];
int iName = d.getColumnIndex(KEY_NAME);
int iMaker = d.getColumnIndex(KEY_MAKER);
int iColor = d.getColumnIndex(KEY_COLOR);
int iSize = d.getColumnIndex(KEY_SIZE);
int iType = d.getColumnIndex(KEY_TYPE);
for (d.moveToFirst(); !d.isAfterLast(); d.moveToNext()) {
resultarray[0] = d.getString(iMaker);
resultarray[1] = d.getString(iName);
resultarray[2] = d.getString(iColor);
resultarray[3] = d.getString(iSize);
resultarray[4] = d.getString(iType);
}
return resultarray;
}
//Retrieve Data
public String[] getName() {
String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_MAKER, KEY_COLOR, KEY_SIZE, KEY_TYPE};
Cursor d = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result ="";
String[] resultarray = new String[5];
int iRow = d.getColumnIndex(KEY_ROWID);
int iName = d.getColumnIndex(KEY_NAME);
int iMaker = d.getColumnIndex(KEY_MAKER);
int iColor = d.getColumnIndex(KEY_COLOR);
int iSize = d.getColumnIndex(KEY_SIZE);
int iType = d.getColumnIndex(KEY_TYPE);
for(d.moveToFirst(); !d.isAfterLast(); d.moveToNext()){
resultarray[0] = d.getString(iMaker);
resultarray[1] = d.getString(iName);
resultarray[2] = d.getString(iColor);
resultarray[3] = d.getString(iSize);
resultarray[4] = d.getString(iType);
}
return resultarray;
}
//CURSOR READER
public Cursor readEntry() {
String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_MAKER, KEY_COLOR, KEY_SIZE, KEY_TYPE};
Cursor d = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
return d;
}
public void DeleteRow(String id) {
ourDatabase.delete(DATABASE_TABLE, "_id = ?",
new String[]{id});
}
}
I know that the code works other than the KEY_TYPE stuff, so I don't feel it is necessary to include other classes, but I will if it helps.