Data won't save to SQL database, getting error "close() was never explicitly called on database"

Posted by SnowLeppard on Stack Overflow See other posts from Stack Overflow or by SnowLeppard
Published on 2012-09-06T19:22:28Z Indexed on 2012/09/06 21:38 UTC
Read the original article Hit count: 375

Filed under:
|
|

I have a save button in the activity where the user enters data which does this:

String subjectName = etName.getText().toString();
String subjectColour = etColour.getText().toString();

SQLDatabase entrySubject = new SQLDatabase(AddSubject.this);
entrySubject.open();
entrySubject.createSubjectEntry(subjectName, subjectColour);
entrySubject.close();

Which refers to this SQL database class:

package com.***.schooltimetable;

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 SQLDatabase {

public static final String KEY_SUBJECTS_ROWID = "_id";
public static final String KEY_SUBJECTNAME = "name";
public static final String KEY_COLOUR = "colour";

private static final String DATABASE_NAME = "Database";
private static final String DATABASE_TABLE_SUBJECTS = "tSubjects";
private static final int DATABASE_VERSION = 1;

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 " + DATABASE_TABLE_SUBJECTS + " ("
                + KEY_SUBJECTS_ROWID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_SUBJECTNAME
                + " TEXT NOT NULL, " + KEY_COLOUR + " TEXT NOT NULL);");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_SUBJECTS);
        onCreate(db);
    }

}

public SQLDatabase(Context c) {
    ourContext = c;
}

public SQLDatabase open() throws SQLException {
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() {
    ourHelper.close();
}

public long createSubjectEntry(String subjectName, String subjectColour) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_SUBJECTNAME, subjectName);
    cv.put(KEY_COLOUR, subjectColour);
    return ourDatabase.insert(DATABASE_TABLE_SUBJECTS, null, cv);
}

public String[][] getSubjects() {
    // TODO Auto-generated method stub
    String[] Columns = new String[] { KEY_SUBJECTNAME, KEY_COLOUR };
    Cursor c = ourDatabase.query(DATABASE_TABLE_SUBJECTS, Columns, null, null, null, null, null);
    String[][] Result = new String[1][];

    // int iRow = c.getColumnIndex(KEY_LESSONS_ROWID);
    int iName = c.getColumnIndex(KEY_SUBJECTNAME);
    int iColour = c.getColumnIndex(KEY_COLOUR);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        Result[0][c.getPosition()] = c.getString(iName);
        Result[1][c.getPosition()] = c.getString(iColour);

        Settings.subjectCount = c.getPosition();
        TimetableEntry.subjectCount = c.getPosition();
    }
    return Result;
}

This class has other variables and other variations of the same methods for multiple tables in the database, i've cut out the irrelevant ones. I'm not sure what I need to close and where, I've got the entrySubject.close() in my activity. I used the methods for the database from the NewBoston tutorials.

Can anyone see what I've done wrong, or where my problem is? Thanks.

© Stack Overflow or respective owner

Related posts about android

Related posts about database