从SQLite数据库中删除前8行。

huangapple go评论72阅读模式
英文:

Delete the first 8 rows from an SQLite database

问题

以下是翻译好的内容:

public void deleteFirstRow()
{
    SQLiteDatabase db = this.getWritableDatabase();

    Cursor cursor = db.query("eventosUTN", null, null, null, null, null, null);

    if(cursor.moveToFirst()) {
        String rowId = cursor.getString(cursor.getColumnIndex("id"));
        db.delete("eventosUTN", "id" + "=?",  new String[]{rowId});
    }
    db.close();
}

public void checkRowsSize(Events events, Context contex)
{  
    new Thread(new Runnable() { 
        public void run() {
        if(events.getRowCount() > 8){
            while(events.getRowCount() > 8){
                events.deleteFirstRow(contex);
            }
        }
    }
}).start();
}

public int getRowCount(){
    String query = "SELECT * FROM eventsUTN";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    return cursor.getCount();
}

public void checkRowsSize(Events events, Context contex)
{  
    new Thread(new Runnable() { 
        public void run() {
        if(events.getRowCount() > 8){
            events.deleteFirstRow(contex);
        }
    }
}).start();
}
英文:

I delete rows in a SQLite table with this:

public void deleteFirstRow()
{
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.query("eventosUTN", null, null, null, null, null, null);
if(cursor.moveToFirst()) {
String rowId = cursor.getString(cursor.getColumnIndex("id"));
db.delete("eventosUTN", "id" + "=?",  new String[]{rowId});
}
db.close();
}

I need to delete the first 8 rows when the database has a size bigger than 8 rows, so:

public void checkRowsSize(Events events, Context contex)
{  
new Thread(new Runnable() { 
public void run() {
if(events.getRowCount()>8){
while(events.getRowCount()>8){
events.deleteFirstRow(contex);
}
}
}
}).start();
}

The method Events's getRowCount is:

public int getRowCount(){
String query = "Select*FROM eventsUTN";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
return cursor.getCount();
} 

But When I set the while loop in checkRowSize it delete the all database. Why this? But If I set:

public void checkRowsSize(Events events, Context contex)
{  
new Thread(new Runnable() { 
public void run() {
if(events.getRowCount()>8){
events.deleteFirstRow(contex);
}
}
}).start();
}

It just delete the first row every time the Activity is created, until the database has a size smaller than 8 rows. So without the while loop it works fine!

答案1

得分: 2

以下是翻译好的内容:

您可以使用以下语句在表格大小大于8行时删除前8行

delete from eventosUTN
where 
  (select count(*) from eventosUTN) > 8
  and 
  id in (select id from eventosUTN limit 8)

其中id是您表格的主键。但是您应该知道,在表格中您认为的前8行并不总是您实际得到的前8行。表格行是无序的。因此,您可能应该使用ORDER BY子句定义此顺序的逻辑,如下所示:

delete from eventosUTN
where 
  (select count(*) from eventosUTN) > 8
  and 
  id in (select id from eventosUTN order by id limit 8)

因此,您可以在Java代码中这样执行:

String where = "(select count(*) from eventosUTN) > 8 and id in (select id from eventosUTN order by id limit 8)";
db.delete("eventosUTN", where, null);

如果您的表格的主键整数类型,并且已被定义为AUTOINCREMENT,这意味着它始终递增且不会被重用,那么您还可以使用以下语句:

delete from eventosUTN 
where id < (select id from eventosUTN order by id limit 8, 1)

在Java代码中如下所示:

String where = "id < (select id from eventosUTN order by id limit 8, 1)";
db.delete("eventosUTN", where, null);
英文:

You can delete the first 8 rows when the table has a size bigger than 8 rows with this statement:

delete from eventosUTN
where 
(select count(*) from eventosUTN) &gt; 8
and 
id in (select id from eventosUTN limit 8)

where id is the primary key of your table.<br/>
But you should know that what you think as first 8 rows in a table is not always what you get.<br/>
Table rows are unordered.<br/>
So maybe you should define the logic of this order with the use of an ORDER BY clause like:

delete from eventosUTN
where 
(select count(*) from eventosUTN) &gt; 8
and 
id in (select id from eventosUTN order by id limit 8)

So you can do this in java code like this:

String where = &quot;(select count(*) from eventosUTN) &gt; 8 and id in (select id from eventosUTN order by id limit 8)&quot;;
db.delete(&quot;eventosUTN&quot;, where, null);

<br/>
If the primary key of your table is integer and has been defined as AUTOINCREMENT which means that it is always increasing and never reused then you could also use this statement:

delete from eventosUTN 
where id &lt; (select id from eventosUTN order by id limit 8, 1)

and in java code:

String where = &quot;id &lt; (select id from eventosUTN order by id limit 8, 1)&quot;;
db.delete(&quot;eventosUTN&quot;, where, null);

huangapple
  • 本文由 发表于 2020年4月5日 20:54:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/61042924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定