删除Android的SQLite数据库前N行数据库、Android、SQLite

2023-09-13 23:40:46 作者:じ我的爱、很霸道

请让我知道如何删除Android的SQLite数据库正行。我用这个code:

 字符串ALTER_TBL =从删除+ MYDATABASE_TABLE +
         其中,+ KEY_ID +号(选前3名+ KEY_ID +由+ MYDATABASE_TABLE +ORDER BY _id);;

          sqLiteDatabase.execSQL(ALTER_TBL);
 

但它显示了一个错误。

  03-21 13:19:39.217:信息/数据库(1616年):源码返回:错误code = 1,味精=近中:语法错误
03-21 13:19:39.226:ERROR /数据库(1616年):失败1(近,在:语法错误)在0x23fed8时preparing'从detail1where_id中删除(选择顶部3_idfromdetail1order由_id);。
 
Android小功能实现之SQLite数据库的基本操作

解决方案

 字符串ALTER_TBL =从删除+ MYDATABASE_TABLE +
     其中,+ KEY_ID +中(选择+ KEY_ID +由+ MYDATABASE_TABLE +为了通过_id LIMIT 3);;
 

有SQLite中没有前3名命令,我知道,你要加一个限制 注意空格,当您添加字符串连接在一起: +表+,其中=删除frommytablewhere从删除

此方法使用两个步骤来删除前N行。

查找第N行:

SELECT id_column FROM TABLE_NAME ORDER BY id_column LIMIT 3

结果是ID,这些ID重新present前N的列表(此处:3)的行。该 ORDER BY 部分是很重要的,因为SQLite不保证没有该条款的任何命令。如果没有 ORDER BY 语句可以删除3个随机行。

从ID列表相匹配的表中删除任何行:

DELETE FROM table_name的WHERE id_column IN({步骤1的结果})

如果步骤1的结果为空,什么都不会发生,如果有不足只是这些都将被删除N行。

要注意的是, id_column 必须是唯一重要的是,否则超过预定的行会被删除。如果是用于排序的列不是唯一的整个语句可以被更改为 DELETE FROM table_name的WHERE unique_column IN(SELECT unique_column FROM table_name的ORDER BY sort_column LIMIT 3)。提示:SQLite的的 ROWID 是一个很好的候选人 unique_column 表上删除时(在视图上删除时可能无法正常工作 - 不知道这里)。

要删除过去 N行的排序顺序已被逆转为下降( DESC ):

  DELETE FROM table_name的WHERE unique_column IN(
    选择unique_column FROM table_name的ORDER BY sort_column DESC LIMIT 3
  )
 

要删除N个日到M 日行的限制子句可以通过 OFFSET 。下面的例子将跳过第2行,返回/删除接下来的3

  SELECT unique_column FROM table_name的ORDER BY sort_column LIMIT 3偏移2
 

设置限制为负值(如 LIMIT -1偏移2 )将返回除了所有行第2导致删除一切,但第2行 - 这也可以通过转动完成的 SELECT ... WHERE。在() SELECT ... WHERE ... NOT IN()

SQLite的有一个选项启用 ORDER BY NOx限制ñ部分,直接在没有一个子查询的删除语句。这种选择是在Android 未启用,并且不能被激活,但是这可能会感兴趣的使用SQLite在其他系统上的人:

  DELETE FROM table_name的ORDER BY sort_column LIMIT 3
 

Please let me know how to delete n-rows in android sqlite database. I used this code:

   String ALTER_TBL ="delete from " + MYDATABASE_TABLE +
         "where"+KEY_ID+"in (select top 3"+ KEY_ID +"from"+ MYDATABASE_TABLE+"order by _id );";

          sqLiteDatabase.execSQL(ALTER_TBL);

But it shows an error.

03-21 13:19:39.217: INFO/Database(1616): sqlite returned: error code = 1, msg = near "in": syntax error
03-21 13:19:39.226: ERROR/Database(1616): Failure 1 (near "in": syntax error) on 0x23fed8 when preparing 'delete from detail1where_id in (select top 3_idfromdetail1order by _id );'.

解决方案

String ALTER_TBL ="delete from " + MYDATABASE_TABLE +
     " where "+KEY_ID+" in (select "+ KEY_ID +" from "+ MYDATABASE_TABLE+" order by _id LIMIT 3);";

there is no "top 3" command in sqlite I know of, you have to add a limit watch out for spaces when you add strings together : "delete from" + TABLE + "where" = "delete frommytablewhere"

This approach uses two steps to delete the first N rows.

Find the first N rows:

SELECT id_column FROM table_name ORDER BY id_column LIMIT 3

The result is a list of ids that represent the first N (here: 3) rows. The ORDER BY part is important since SQLite does not guarantee any order without that clause. Without ORDER BY the statement could delete 3 random rows.

Delete any row from the table that matches the list of ids:

DELETE FROM table_name WHERE id_column IN ( {Result of step 1} )

If the result from step 1 is empty nothing will happen, if there are less than N rows just these will be deleted.

It is important to note that the id_column has to be unique, otherwise more than the intended rows will be deleted. In case the column that is used for ordering is not unique the whole statement can be changed to DELETE FROM table_name WHERE unique_column IN (SELECT unique_column FROM table_name ORDER BY sort_column LIMIT 3). Hint: SQLite's ROWID is a good candidate for unique_column when deleting on tables (may not work when deleting on views - not sure here).

To delete the last N rows the sort order has to be reversed to descending (DESC):

DELETE FROM table_name WHERE unique_column IN (
    SELECT unique_column FROM table_name ORDER BY sort_column DESC LIMIT 3
  )

To delete the Nth to Mth row the LIMIT clause can be extended by an OFFSET. Example below would skip the first 2 rows and return / delete the next 3.

SELECT unique_column FROM table_name ORDER BY sort_column LIMIT 3 OFFSET 2

Setting the LIMIT to a negative value (e.g. LIMIT -1 OFFSET 2) would return all rows besides the first 2 resulting in deletion of everything but the first 2 rows - that could also be accomplished by turning the SELECT .. WHERE .. IN () into SELECT .. WHERE .. NOT IN ()

SQLite has an option to enable the ORDER BY x LIMIT n part directly in the DELETE statement without a sub-query. That option is not enabled on Android and can't be activated but this might be of interest to people using SQLite on other systems:

 DELETE FROM table_name ORDER BY sort_column LIMIT 3