无法使用给定的查询删除数据

cannot delete data using given Query

我有三个 table,我想删除它们。

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

Table 劳动力:id1(PK), subContractors,noOfPeople,noOfHours,TInfo_id(FK to table Info)

Table 工作详情:id2(PK),project,workDescription, Twf_id(FK to table workForce),TableInfo_id(FK to table Info) //contains multiple row

Table 信息

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

Table 劳动力

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

Table 工作详情

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

假设我想使用下面的代码删除 table Info 中的 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)

您需要添加空格和一个 AND。此外,您不需要添加 Where 因为它已经由 SqliteDatabase.

添加

您似乎还试图在一个调用中从两个 table 中删除。每个 table.

需要一个删除调用
  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 + "" });
}

所以 sql 语句读作:

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

但最有可能的是,您只需要执行如下操作:

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[] 数组取代了 ? 标记。您需要先查询 ID,然后调用相应的删除操作。修复您的 where 子句代码,这将很容易。