0

I believe the error lies in the way I have setup DATABASE_CREATE. Am I missing something here? I'm getting SQLiteException where long id = -1. Let me know if I am providing enough details. I've tried setting KEY_ID = "_id";

E/SQLiteLog(2385): (1) near "group": syntax error
E/SQLiteDatabase(2385): Error inserting group=demo
E/SQLiteDatabase(2385): android.database.sqlite.SQLiteException: near "group": syntax error (code 1): , while compiling: INSERT INTO groups(group) VALUES (?)
LOG(2385): Inserting record...
LOG(2385): mGroupName = demo
LOG(2385): long id = -1

DBAdapter.java

public static final String KEY_ID = "id";
public static final String KEY_GROUP_NAME = "group";
public static final String TAG = "DBAdapter";
public static final String DATABASE_NAME = "GroupsDB";
public static final String DATABASE_TABLE = "groups";
public static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table if not exists groups (id integer primary key autoincrement, group VARCHAR not null);";

. . .

//---insert a record into the database
public long insertRecord(String group) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_GROUP_NAME, group);
    return db.insert(DATABASE_TABLE, null, initialValues);  
}

MainActivity.java

EditText groupNameText = (EditText) findViewById(R.id.groupNameEditText);
String mGroupname = groupNameText.getText().toString();

...

                try {
                    String destPath = "/data/data/" + getPackageName() + "/databases/GroupsDB";
                    File f = new File(destPath);
                    if(!f.exists()) {
                        CopyDB( getBaseContext().getAssets().open("mydb"),
                        new FileOutputStream(destPath));
                    }
                }catch (FileNotFoundException e) {
                    e.printStackTrace();
                }catch (IOException e) {
                    e.printStackTrace();
                }

                DBAdapter db = new DBAdapter(this);

                //try hard coding the record here, if unable to insertRecord for any reason it will return -1
                db.open();
                long id = db.insertRecord(mGroupname);
                Log.i(TAG, "Inserting record...");
                Log.i(TAG, "mGroupName = " + mGroupname);
                Log.i(TAG, "long id = " + id);
                db.close();

1 Answer 1

5

group is a SQLite keyword. See: http://www.sqlite.org/lang_keywords.html

Change all references to "group" to something like "mgroup"

Sign up to request clarification or add additional context in comments.

2 Comments

I wasted about a hour trying to figure out what's wrong with my table name "group" :) who would thought.
Like @Hades200621... I've wasted more than an hour without knowing what is going on... Thanks @KlassAktKreations!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.