语法错误android sqlite:near“group”

问题描述 投票:0回答:2

这是logcat窗口显示的异常

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 (?,?,?,?,?,?,?,?,?,?,?,?)

希望有人理解这是我的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);
}

这就是我所说的这种方法

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", "[email protected]");
            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();
        }
    }


}
android sqlite
2个回答
4
投票

“组”这引起了问题。尝试使用mGroup(或其他任何东西)而不是group。

原因:group是sql中的保留字(关于group by子句的here)。

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

所以你不能使用GROUP或group。

所以你的方法应该是:

    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);

    }

编辑此外,如果你有KEY_GROUP = "group";定义。将其改为别的东西。最后一点。也不要使用日期。它也是保留的。

祝你好运。


0
投票

您是否在customer表中使用group作为列名?,问题似乎是使用'date'和'group'作为列名,因为它们都是保留字。例如,尝试将列名修改为date1和group1。

© www.soinside.com 2019 - 2024. All rights reserved.