Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid SQLite INSERT error
    primarykey
    data
    text
    <p>Im getting an error I cannot figure out. Hope you can help!</p> <p>I'm making a table of groups names with a unique id which is the primary key.</p> <pre><code>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); } } </code></pre> <p>I call these classes from another class with following code:</p> <pre><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); </code></pre> <p>The error I get is </p> <pre><code>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(?, ?); </code></pre> <p>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.</p> <p>Ok I changed group into groep (dutch for group) and now I get these errors:</p> <pre><code>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(?, ?); </code></pre> <p>Cheers! Kat</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload