项目一次又一次地保存在sqlite数据库中



只有当我想要的条件得到满足时,我才会尝试将值保存在sqlite数据库中,但即使条件得到满足,它也会将值保存到数据库中,并且不会停止。

这是我的代码

int asas = 0;
       if (list!= null) {
         for (int i1 = 0; i1 < list.size(); i1++) {
           String s= list.get(i1);
           while (msgBody.toUpperCase().contains(s.toUpperCase()) && !contactExists(context, msg_from)) {
               asas++;
             if (asas>=3)
             {
                screenMessage(context, msg_from, msg_from, msgBody, msgDate); //saving value in this table
             }
             if (asas <3 && !contactExists(context, msg_from)) {

                screenMessagee(context, msg_from, msg_from, msgBody, msgDate); //saving value in this table
             break;
              }
             break;
           }
         }
       }

现在让我解释一下我的代码。我有一个包含存储在其中的不同项目的列表视图。现在我在这里做的是检查用户给定的名为"msgBody"的字符串是否包含存储的列表视图项目。这就是为什么我使用int asas=0,这样当用户提供字符串时,while条件会检查用户提供的字符串中有多少个列表视图项。如果字符串包含3个或更多项目,则将其保存在特定的表中。如果字符串包含的项目少于3个,则将其保存在另一个表中。现在我面临的问题是,如果提供的字符串包含let示例5项,那么提供的字符串将在该表中保存3次

screenMessage(context, msg_from, msg_from, msgBody, msgDate); 

我发现它可能会发生,因为while condition当达到存储的3个项目时,它将值保存在数据库中,但由于提供的字符串包含5个项目,所以while conditions没有停止,并将值保存了两次,直到所有5个项目的条件都满足为止。如果在数据库表

中发现3个项目并且值必须只保存一次,请帮助如何停止条件

根据我的理解,您需要计算存储在列表中的单词在特定字符串中出现的次数,然后将其存储在两个数据库表中的一个表中,具体取决于它是否小于3。

我真的不理解你的代码,但试试这个:

int asas = 0;
   if (list!= null) {
     for (int i1 = 0; i1 < list.size(); i1++) {
       String s= list.get(i1);

       if(msgBody.toUpperCase().contains(s.toUpperCase()) && !contactExists(context, msg_from)){
           asas++;
       }

   }
     if(asas>0){
    if(asas<3)
    {
        screenMessagee(context, msg_from, msg_from, msgBody, msgDate); //saving value in this table
    }
    else{
        screenMessage(context, msg_from, msg_from, msgBody, msgDate); //saving value in this table
    }
     }

最新更新