2

我正在开发一个短信应用程序,我试图从每个对话中获取最后一条短信。

这是我的 SQL 语句:

SELECT MAX(smsTIMESTAMP) AS smsTIMESTAMP,_id, smsID, smsCONID, smsMSG, smsNUM, smsREAD, smsTYPE, smsSHORTMSG, COUNT(*) AS smsNUMMESSAGES FROM sms GROUP BY smsCONID ORDER BY smsTIMESTAMP desc

我在 SQLite Expert 中运行了查询,得到了正确的响应:

在此处输入图像描述

但是,当我在我的应用程序中运行它时,我得到:

在此处输入图像描述

这是我的桌子

在此处输入图像描述

这是我的数据操纵器类:

public class DataManipulator {
    private static final  String DATABASE_NAME = "smsapplication.db";
    private static final int DATABASE_VERSION = 3;
    static final String TABLE_NAME = "sms";
    private static Context context;
    static SQLiteDatabase db;
    private static DataManipulator instance;

    private SQLiteStatement insertStmt;

    private static final String INSERT = "insert or ignore into "
        + TABLE_NAME + " (smsID, smsCONID, smsMSG, smsNUM, smsREAD, smsTIMESTAMP, smsTYPE, smsSHORTMSG) values (?,?,?,?,?,?,?,?)";

    public DataManipulator(Context context) {
        DataManipulator.context = context;
        OpenHelper openHelper = new OpenHelper(DataManipulator.context);
        DataManipulator.db = openHelper.getWritableDatabase();
        this.insertStmt = DataManipulator.db.compileStatement(INSERT);
    }
    public static DataManipulator getInstance(Context mContext)
    {
        if(instance == null)
        {
            instance = new DataManipulator(mContext);
        }
        return instance;
    }
    public long insert(int smsID, int smsCONID, String smsMSG,
            String smsNUM, int smsREAD, long smsTIMESTAMP, String smsTYPE, String smsSHORTMSG) {
        this.insertStmt.bindLong(1, smsID);
        this.insertStmt.bindLong(2, smsCONID);
        this.insertStmt.bindString(3, smsMSG);
        this.insertStmt.bindString(4, smsNUM);
        this.insertStmt.bindLong(5, smsREAD);
        this.insertStmt.bindString(6, String.valueOf(smsTIMESTAMP));
        this.insertStmt.bindString(7, smsTYPE);
        this.insertStmt.bindString(8, smsSHORTMSG);
        return this.insertStmt.executeInsert();
    }

    public void deleteAll() {
        db.delete(TABLE_NAME, null, null);
    }

    public Cursor getLastSmsForAllConversations()
    {
        Cursor cursor = db.query(TABLE_NAME, new String[] {"_id"," MAX(smsTIMESTAMP) AS smsTIMESTAMP ", "smsCONID", "smsNUM", "smsREAD", "smsTYPE","smsSHORTMSG","COUNT(*) AS smsNUMMESSAGES"  },
                null, null, "smsCONID", null, "smsTIMESTAMP desc"); 
        return cursor;
    }
    public Cursor getConversationMessages(int conID)
    {
        Cursor cursor = db.query(TABLE_NAME, new String[] {"_id", "smsID", "smsCONID", "smsMSG", "smsNUM", "smsREAD", "smsTIMESTAMP", "smsTYPE","smsSHORTMSG"  },
                "smsCONID="+conID, null, null, null, "smsTIMESTAMP asc"); 
        return cursor;
    }
    public void printCursor()
    {
        Cursor cursor = db.rawQuery("SELECT MAX(smsTIMESTAMP) AS smsTIMESTAMP,_id, smsID, smsCONID, smsMSG, smsNUM, smsREAD, smsTYPE, smsSHORTMSG, COUNT(*) AS smsNUMMESSAGES FROM sms GROUP BY smsCONID ORDER BY smsTIMESTAMP desc", null);

            //  db.query(TABLE_NAME, new String[] {"_id","smsTIMESTAMP AS smsTIMESTAMP ", "smsCONID", "smsNUM", "smsREAD", "smsTYPE","smsSHORTMSG"  },"smsCONID=40", null, null, null, "smsTIMESTAMP desc");  
        if (cursor.moveToFirst()){
               do{
                  String data ="";
                  data= cursor.getString(cursor.getColumnIndex("smsTIMESTAMP"))+" - " +cursor.getString(cursor.getColumnIndex("smsSHORTMSG"));
                  Log.i("data", data);
               }while(cursor.moveToNext()); 
            }
            cursor.close();
    }

    private static class OpenHelper extends SQLiteOpenHelper {

        OpenHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + TABLE_NAME + " (_id INTEGER , smsID INTEGER PRIMARY KEY, smsCONID INTEGER, smsMSG TEXT,smsNUM TEXT, smsREAD INTEGER, smsTIMESTAMP INTEGER, smsTYPE TEXT, smsSHORTMSG TEXT)");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
         @Override
            public synchronized void close() {

            if (db != null)
                db.close();

            super.close();

            }
    } 
}
4

2 回答 2

2

使用时GROUP BY,结果的每一行对应原始表的多行。如何计算这些结果有三种可能性:

  • 具有聚合函数的列类似于MAXCOUNT计算组中所有行的值;
  • 出现在GROUP BY子句中的列仅取自组中的任何记录(因为组中的所有记录对该列具有相同的值);
  • 其他列是一个问题,因为组中的行可能具有不同的值。在标准 SQL 中,此类列是被禁止的;SQLite 允许它们,但只是为您提供组中任何随机行的值,这几乎不是您想要的。从 SQLite 3.7.11 开始,您可以从匹配 aMINMAX;的行中获取值。这是您在 SQLite Exprert 中看到的,但不是在具有较旧 SQLite 的 Android 上。

要解决您的问题,您必须首先使用 aGROUP BY来获取足够的信息来识别您想要的记录:

SELECT smsCONID,                            -- OK: used in GROUP BY
       MAX(smsTIMESTAMP) AS smsTIMESTAMP,   -- OK: aggregate MAX
       COUNT(*) AS smsNUMMESSAGES           -- OK: aggregate COUNT
FROM sms
GROUP BY smsCONID
ORDER BY smsTIMESTAMP DESC

然后,将该结果表与原始sms表连接以获取这些记录的其他列:

SELECT *
FROM (SELECT smsCONID,
             MAX(smsTIMESTAMP) AS smsTIMESTAMP,
             COUNT(*) AS smsNUMMESSAGES
      FROM sms
      GROUP BY smsCONID) AS grouped
     JOIN sms
       ON grouped.smsCONID     = sms.smsCONID
      AND grouped.smsTIMESTAMP = sms.smsTIMESTAMP
ORDER BY smsTIMESTAMP DESC
于 2012-12-18T11:20:29.427 回答
0

您的查询和代码是正确的我没有发现任何问题而不是上面的查询试试这个..

选择_id,smsID,smsCONID,smsMSG,smsNUM,smsREAD,smsTYPE,smsSHORTMSG,从sms GROUP BY smsCONID ORDER BY smsTIMESTAMP desc LIMIT 1

如果你也想计数然后这样做..

SELECT COUNT(*) AS smsNUMMESSAGES, _id, smsID, smsCONID, smsMSG, smsNUM, smsREAD, smsTYPE, smsSHORTMSG, FROM sms GROUP BY smsCONID ORDER BY smsTIMESTAMP desc LIMIT 1

于 2012-12-17T15:08:26.623 回答