Kat
Kat

Reputation: 666

Android SQLite INSERT error

Im getting an error I cannot figure out. Hope you can help!

I'm making a table of groups names with a unique id which is the primary key.

public class GroupDatabaseAdapter {

public static final String KEY_ID = "id";
public static final String KEY_NAME = "name";
private static final String DATABASE_TABLE = "groep";
private Context context;
private SQLiteDatabase database;
private GroupDatabaseHandler dbHandler;

public GroupDatabaseAdapter(Context context) {
    this.context = context;
}

public GroupDatabaseAdapter open() throws SQLException {
    dbHandler = new GroupDatabaseHandler(context);
    database = dbHandler.getWritableDatabase();
    return this;
}

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

/**
 * Create a new group If the group is successfully created return the new
 * rowId for that note, otherwise return a -1 to indicate failure.
 */
public long createGroup(long id, String name) 
{
    ContentValues values = createContentValues(id, name);

    return database.insert(DATABASE_TABLE, null, values);
}
/**
 * Return a Cursor over the list of all groups in the database
 * 
 * @return Cursor over all groups
 */
public Cursor fetchAllGroups() {
    return database.query(DATABASE_TABLE, new String[] { KEY_ID,
            KEY_NAME }, null, null, null,
            null, null);
}

private ContentValues createContentValues(Long id, String name) {
    ContentValues values = new ContentValues();
    values.put(KEY_ID, id);
    values.put(KEY_NAME, name);
    return values;
}
}



public class GroupDatabaseHandler extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "groups.db";

private static final int DATABASE_VERSION = 1;

// Database creation sql statement
private static final String DATABASE_CREATE = "CREATE TABLE lesson (id INTEGER PRIMARY KEY NOT NULL , name TEXT NOT NULL);";

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

@Override
public void onCreate(SQLiteDatabase database) 
{
    database.execSQL(DATABASE_CREATE);
}

}

I call these classes from another class with following code:

GroupDatabaseAdapter groupDb = new GroupDatabaseAdapter(this).open();
long id = groupDb.createGroup(641, "3 bac group 2");
System.out.println(id);
id = groupDb.createGroup(642, "3 bac group 3");
System.out.println(id);

The error I get is

11-24 23:08:07.036: I/Database(11436): sqlite returned: error code = 1, msg = near "group": syntax error
11-24 23:08:07.075: E/Database(11436): Error inserting id=1 name=3 bac group 2
11-24 23:08:07.075: E/Database(11436): android.database.sqlite.SQLiteException: near "group": syntax error: , while compiling: INSERT INTO group(id, name) VALUES(?, ?);

In the same program I made another table which does work. The only difference between that table and this one is at the primary key. In the other table it is auto incremented, here I want the rowid to be the id of the group. Hope someone sees my mistake.

Ok I changed group into groep (dutch for group) and now I get these errors:

11-25 00:09:10.812: I/Database(457): sqlite returned: error code = 1, msg = no such table: groep
11-25 00:09:11.082: E/Database(457): Error inserting _id=1 name=3 bac group 2
11-25 00:09:11.082: E/Database(457): android.database.sqlite.SQLiteException: no such table: groep: , while compiling: INSERT INTO groep(_id, name) VALUES(?, ?);

Cheers! Kat

Upvotes: 2

Views: 2538

Answers (2)

miles82
miles82

Reputation: 6794

You created a table called lesson and insert into a table called groep.

Upvotes: 0

dmon
dmon

Reputation: 30168

Just a hunch: try renaming the database table to something other than "group" (e.g. "groups"). I'm pretty sure "group" is a reserved keyword in SQL.

Upvotes: 5

Related Questions