amitagrawal amitagrawal - 5 months ago 18
Android Question

near "group": syntax error android sqlite

Here is the exception shown by logcat window

06-08 11:25:55.532 8480-8480/example.com.shareit E/SQLiteLog: (1) near "group": syntax error
06-08 11:25:55.542 8480-8480/example.com.shareit E/SQLiteDatabase: Error inserting amount=100 share1=0 share2=100 share3=0 paid2=0 share4=0 paid3=0 description=Food paid1=100 paid4=0 group=4 date=6/8/2016
06-08 11:25:55.542 8480-8480/example.com.shareit E/SQLiteDatabase: android.database.sqlite.SQLiteException: near "group": syntax error (code 1): , while compiling: INSERT INTO contacts(amount,share1,share2,share3,paid2,share4,paid3,description,paid1,paid4,group,date) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)


Hope Someone Understands by this and this is my InsertContact()

public long insertContact(int amount, String description, String date, int paid1, int paid2,int paid3,int paid4, int share1, int share2,int share3,int share4,long group) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_AMOUNT, amount);
initialValues.put(KEY_DESCRIPTION, description);
initialValues.put(KEY_DATE, date);

initialValues.put(KEY_PAID1, paid1);
initialValues.put(KEY_PAID2, paid2);
initialValues.put(KEY_PAID3, paid3);
initialValues.put(KEY_PAID4, paid4);

initialValues.put(KEY_SHARE1, share1);
initialValues.put(KEY_SHARE2, share2);
initialValues.put(KEY_SHARE3, share3);
initialValues.put(KEY_SHARE4, share4);
initialValues.put(KEY_GROUP, group);


return db.insert(DATABASE_TABLE, null, initialValues);
}


and this is where I am calling this method

public void saveBill(View view) {

description = ETdescription.getText().toString();
date = TVdate.getText().toString();
amount = Integer.parseInt(ETamount.getText().toString());
paid1 = Integer.parseInt(ETpaid1.getText().toString());
paid2 = Integer.parseInt(ETpaid2.getText().toString());
paid3 = Integer.parseInt(ETpaid3.getText().toString());
paid4 = Integer.parseInt(ETpaid4.getText().toString());
share1 = Integer.parseInt(ETshare1.getText().toString());
share2 = Integer.parseInt(ETshare2.getText().toString());
share3 = Integer.parseInt(ETshare3.getText().toString());
share4 = Integer.parseInt(ETshare4.getText().toString());
sumPaid = paid1 + paid2 + paid3 + paid4;
sumShare = share1 + share2 + share3 + share4;

if (a == false) {
Toast.makeText(this, "Please Select Date", Toast.LENGTH_SHORT).show();
} else {
if ((amount == sumPaid) && (amount == sumShare)) {
DBAdapter db = new DBAdapter(this);
//---add a contact---
db.open();
long id = db.insertContact(amount, description, date, paid1, paid2, paid3, paid4, share1, share2, share3, share4, ++index);
//id = db.insertContact("Mary Jackson", "mary@jackson.com");
db.close();
Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Sum of Shares or Paid is not equal to total amount", Toast.LENGTH_SHORT).show();
}
}


}

Answer

"group" This is causing problem. Try using mGroup(or anything else) instead of group.

Reason: group is a reserved word (here about group by clause) in sql.

SELECT name, sum(price) FROM YOUR_TABLE GROUP BY name

So you cannot use GROUP or group.

So your method should be:

    public long insertContact(int amount, String description, String date, 
int paid1, int paid2,int paid3,
int paid4, int share1, int share2,
int share3,int share4,long mGroup){

    //Put code here.

     initialValues.put(KEY_GROUP, mGroup);

    }

EDIT Also if you have KEY_GROUP = "group"; defined. Change that too to something else. One last point. Don't use date either. It is also reserved.

Best of luck.

Comments