无法使用给定的Query删除数据

时间:2015-10-19 01:56:03

标签: java android sqlite cursor

我有三张桌子,我想删除它们。

表格信息:id(PK),name,status,date,weather

表WorkForce:id1(PK), subContractors,noOfPeople,noOfHours,TInfo_id(FK to table Info)

表工作详细信息:id2(PK),project,workDescription, Twf_id(FK to table workForce),TableInfo_id(FK to table Info) //contains multiple row

表格信息

 ID          NAME        Weather        Date     Status
 ----------  ----------  ----------  ----------  ----------
    1           Paul        Sunny         15/10      MC
    2           Allen       Rainy         15/10      Working

Table WorkForce

ID1          SubContractors   NoOfPeople      NoOfHours        TInfo_id
----------  --------------   ----------       ----------     -----------
1           AAA                2                 2                 1
2           BBB                3                 1                 2

表工作详情

ID2         Project       WorkDescription        TableInfo_id      Twf_id
----------  ----------     --------------          ----------    ----------
1              A               B                       1             1
2                                                      1             1
3                                                      1             1
4                                                      1             1
5               C               D                      2             2
6                                                      2             2
7                                                      2             2
8                                                      2             2

假设我想使用下面的代码删除表格信息中的ID 17

ListDisplay.java

  listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            public boolean onItemLongClick(final AdapterView<?> p, View v, final int po, long id) {
                final long id1=id;
                AlertDialog.Builder builder = new AlertDialog.Builder(ListDisplay.this);
                builder.setTitle("Delete");
                builder.setMessage("Are you sure you want to delete?");
                builder.setIcon(android.R.drawable.ic_dialog_alert);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int ii) {

                        database = dbHelper.getWritableDatabase();
                        Cursor cursor = (Cursor) p.getItemAtPosition(po);

                        // Get the state's capital from this row in the database.
                        long ID =
                                cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
                      sqlcon.delete(ID);

                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()

                        {
                            public void onClick(DialogInterface dialog, int ii) {
                                dialog.dismiss();
                            }
                        }

                );
                builder.show();
                return true;
            }
        });

InfoAPI.java

  public void delete(long a)
    {
         database.delete(MyDatabaseHelper.TABLE_INFO + MyDatabaseHelper.TABLE_WORKFORCE, MyDatabaseHelper.TABLE_WORKDETAILS + "WHERE" + MyDatabaseHelper.ID + "=" + a+ MyDatabaseHelper.TInfo_id+ "=" + a +MyDatabaseHelper.Twf_id+ "=" + a, null);
    }

我收到错误如下

10-19 01:45:54.816    6002-6002/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.project.project, PID: 6002
    android.database.sqlite.SQLiteException: unrecognized token: "17TInfo_id" (code 1): , while compiling: DELETE FROM InformationWorkForce WHERE WorkDetailsWHERE_id=17TInfo_id=17TWf_id=17
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)

1 个答案:

答案 0 :(得分:2)

您需要添加空格和AND。此外,您不需要添加Where,因为SqliteDatabase已经添加了 public void delete(long a) { database.delete(MyDatabaseHelper.TABLE_INFO + MyDatabaseHelper.TABLE_WORKFORCE, MyDatabaseHelper.ID + " =? " AND " + MyDatabaseHelper.TInfo_id + " =?" + MyDatabaseHelper.Twf_id + "=? ", new String[] { a + "", a + "", a + "" }); }

看起来您还试图在一次通话中从两个表中删除。每个表需要一次删除调用。

DELETE FROM InformationWorkForce WHERE _id=17 AND TInfo_id=17 AND TWf_id=17

因此sql语句读作:

database.delete(MyDatabaseHelper.TABLE_INFO, MyDatabaseHelper.TInfo_id + " =?", new String[] { tinfo_id + "" });
database.delete(MyDatabaseHelper.TABLE_WORKFORCE, MyDatabaseHelper.Twf_id + " =?", new String[] { twf_id + "" });

最有可能的是,你需要做这样的事情:

?

请注意,新的String []数组取代了feature.geometry.coordinates[0]标记。您需要首先查询ID,然后调用相应的删除。修复你的where子句代码,它很容易。