LDSDev LDSDev - 25 days ago 7
Android Question

Android SQLite "Table has no no column" but it does. (code 1)

I have followed a tutorial on the site Android Hive and adapted it to my needs as I am very new to using SQLite within android application. However I seem to be running into the same error every time. I have posted my logcat below. As you can see it says that my table 'times' has no column 'multiply' however whenever I feel I have fixed the problem, through renaming it etc it just brings up the same problem on a different column in my table.

My class DatabaseHelper is as follows:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "TimeDatabase";

// Contacts table name
private static final String TABLE_TIMES = "times";

// Contacts Table Columns names
private static final String KEY_ID = "_id";
private static final String KEY_LENGTH = "length";
private static final String KEY_DIFFICULTY = "difficulty";
private static final String KEY_ADD = "addition";
private static final String KEY_SUBTRACT = "subtraction";
private static final String KEY_DIVISION = "division";
private static final String KEY_MULTIPLY = "multiply";
private static final String KEY_DECIMAL = "decimal";
private static final String KEY_NEGATIVE = "negative";
private static final String KEY_TIME = "time";
private static final String KEY_DATE = "date";

public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TIMES_TABLE = "CREATE TABLE " + TABLE_TIMES + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_LENGTH + " INTEGER, "
+ KEY_DIFFICULTY + " INTEGER, " + KEY_ADD + " BOOLEAN, " + KEY_SUBTRACT
+ " BOOLEAN, " + KEY_DIVISION + " BOOLEAN, " + KEY_MULTIPLY + " BOOLEAN, "
+ KEY_DECIMAL + " BOOLEAN, " + KEY_NEGATIVE + " BOOLEAN, " + KEY_TIME
+ " LONG, " + KEY_DATE + " DATETIME" + ")";
db.execSQL(CREATE_TIMES_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TIMES);

// Create tables again
onCreate(db);
}

/**
* All CRUD(Create, Read, Update, Delete) Operations
*/


void addTimeData (TimeData timeData){

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();



SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_LENGTH,timeData.getLength()); //Numbers of questions
values.put(KEY_DIFFICULTY,timeData.getDifficulty()); //Difficulty
values.put(KEY_ADD, timeData.getAdd()); //Add Questions?
values.put(KEY_SUBTRACT, timeData.getSubtract()); //Subtract Questions?
values.put(KEY_DIVISION,timeData.getDivision()); //Div Questions?
values.put(KEY_MULTIPLY,timeData.getMultiply()); //Mult Quetsions?
values.put(KEY_DECIMAL, timeData.getDecimals()); //Decimals?
values.put(KEY_NEGATIVE, timeData.getNegative()); //Negatives?
values.put(KEY_TIME, timeData.getTime()); //How long did it take?
values.put(KEY_DATE, dateFormat.format(date)); //todays date;

// Inserting Row
db.insert(TABLE_TIMES, null, values);
db.close(); // Closing database connection
}

//Getting all Data

public List<TimeData> getAllTimeData() throws ParseException {
List<TimeData> TimeList = new ArrayList<TimeData>();
//select all query
String selectQuery = "SELECT * FROM " + TABLE_TIMES;

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

if (cursor.moveToFirst()) {
do {
TimeData tData = new TimeData();
tData.setID(Integer.parseInt(cursor.getString(0)));
tData.setLength(Integer.parseInt(cursor.getString(1)));
tData.setDifficulty(Integer.parseInt(cursor.getString(2)));
tData.setAdd(Boolean.parseBoolean(cursor.getString(3)));
tData.setSubtract(Boolean.parseBoolean(cursor.getString(4)));
tData.setDivision(Boolean.parseBoolean(cursor.getString(5)));
tData.setMultiply(Boolean.parseBoolean(cursor.getString(6)));
tData.setDecimals(Boolean.parseBoolean(cursor.getString(7)));
tData.setNegative(Boolean.parseBoolean(cursor.getString(8)));
tData.setTime(Long.parseLong(cursor.getString(9)));

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt = dateFormat.parse(cursor.getString(10));

tData.setDate(dt);
// Adding contact to list
TimeList.add(tData);
} while (cursor.moveToNext());
}
return TimeList;
}


}


This is my Logcat

06-16 19:57:55.213: E/SQLiteLog(20352): (1) table times has no column named multiply
06-16 19:57:55.245: E/SQLiteDatabase(20352): Error inserting time=20313415529 division=false length=10 multiply=false difficulty=11 negative=false date=2014-06-16 19:57:55 addition=true decimal=false subtraction=false
06-16 19:57:55.245: E/SQLiteDatabase(20352): android.database.sqlite.SQLiteException: table times has no column named multiply (code 1): , while compiling: INSERT INTO times(time,division,length,multiply,difficulty,negative,date,addition,decimal,subtraction) VALUES (?,?,?,?,?,?,?,?,?,?)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at com.sambrooks.mathlete.DatabaseHandler.addTimeData(DatabaseHandler.java:94)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at com.sambrooks.mathlete.AppActivity3.onClick(AppActivity3.java:252)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.view.View.performClick(View.java:4240)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.view.View$PerformClick.run(View.java:17721)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.os.Handler.handleCallback(Handler.java:730)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.os.Handler.dispatchMessage(Handler.java:92)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.os.Looper.loop(Looper.java:137)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.app.ActivityThread.main(ActivityThread.java:5103)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at java.lang.reflect.Method.invoke(Method.java:525)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at dalvik.system.NativeStart.main(Native Method)


I would greatly appreciate any help. I have changed the Database Version and uninstalled the app and deleted its data multiple times and it has not fixed the problem like other answers have suggested.

Answer

You're missing a comma here, after BOOLEAN.

... + KEY_DIVISION + " BOOLEAN " + KEY_MULTIPLY + " BOOLEAN, " ...

Isn't this execSQL() call throwing an exception?

I guess it's related, since the problem is with the KEY_MULTIPLY field.

Comments