Android SQL Null 常量值

发布于 2024-10-20 05:28:27 字数 1450 浏览 2 评论 0原文

我有这个提示给你......

我们正在尝试将参数传递给内容解析器中的查询。如果参数为 NULL,我们希望获取所有行,或者相反,获取参数中指示的特定值。

我们使用的方法声明:

    static String allArtists = "";
    ////
      Some code
      ...
      Vector<String> discs = new Vector<String>();

      discs = getDiscs(allArtists);
    ////

    public Vector<String> getDiscs(String artistName){

因此,我们所做的查询如下:

Uri discsUri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;

        ContentResolver contRes = getContentResolver();

        String[] queryColumns = {   MediaStore.Audio.Albums._ID,
                                    MediaStore.Audio.Albums.ALBUM,
                                    MediaStore.Audio.Albums.ARTIST };

        String selectionQuery = "? = ?";
        String [] selectionArgs = { MediaStore.Audio.Albums.ARTIST, artistName };
        String sortOrderClause = MediaStore.Audio.Albums.ALBUM + " ASC";

        Cursor queryRes = contRes.query(discsUri, 
                                        queryColumns,
                                        selectionQuery,
                                        selectionArgs,
                                        sortOrderClause
                                         );

为了使该方法可重用,我们传递artistName作为参数,并且因为我们希望选择所有行,所以我们传递常量allArtists。

关键是这个查询没有返回任何行,因为常量 allArtists = "" 并不像我们预期的那样充当空值。

最后的问题是:我必须如何声明该常量,以便查询将其视为 NULL 值而不是“”?

多谢。

I have this cuestion for you...

We are trying to pass an argument to a query in a content resolver. We want to get all rows if the argument is NULL, or get the specific value indicated at the argument as contrary.

The method we use declares:

    static String allArtists = "";
    ////
      Some code
      ...
      Vector<String> discs = new Vector<String>();

      discs = getDiscs(allArtists);
    ////

    public Vector<String> getDiscs(String artistName){

So, the query we made is as follows:

Uri discsUri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;

        ContentResolver contRes = getContentResolver();

        String[] queryColumns = {   MediaStore.Audio.Albums._ID,
                                    MediaStore.Audio.Albums.ALBUM,
                                    MediaStore.Audio.Albums.ARTIST };

        String selectionQuery = "? = ?";
        String [] selectionArgs = { MediaStore.Audio.Albums.ARTIST, artistName };
        String sortOrderClause = MediaStore.Audio.Albums.ALBUM + " ASC";

        Cursor queryRes = contRes.query(discsUri, 
                                        queryColumns,
                                        selectionQuery,
                                        selectionArgs,
                                        sortOrderClause
                                         );

In order to make this method reusable, we pass artistName as argument, and because we want all rows to be selected, we pass the constant allArtists.

The point is that this query returns no rows, because the constant allArtists = "" doesnt act as the null value as we expected.

The question finally is: How must I declare the constant in a way that the query takes it as a NULL value instead of ""?.

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若能看破又如何 2024-10-27 05:28:27

将空字符串转为null

    String selectionQuery = "? = nullif(?,'''')";

Turn empty strings into null

    String selectionQuery = "? = nullif(?,'''')";
仙气飘飘 2024-10-27 05:28:27

我们通过这种方式解决了选择问题:

        String[] queryColumns = {   MediaStore.Audio.Albums._ID,
                                    MediaStore.Audio.Albums.ALBUM,
                                    MediaStore.Audio.Albums.ARTIST };

        String  selectionQuery  = "( nullif('" + artistName + "','') ISNULL )" +
                                  " OR (" + MediaStore.Audio.Albums.ARTIST + " == '" + artistName + "' )";
        String  sortOrderClause = MediaStore.Audio.Albums.ALBUM + " ASC";

        Cursor queryRes = contRes.query(discsUri, 
                                        queryColumns,
                                        selectionQuery,
                                        null,
                                        sortOrderClause);

希望这对其他人有帮助。
问候!

We resolved our problem making the selection this way:

        String[] queryColumns = {   MediaStore.Audio.Albums._ID,
                                    MediaStore.Audio.Albums.ALBUM,
                                    MediaStore.Audio.Albums.ARTIST };

        String  selectionQuery  = "( nullif('" + artistName + "','') ISNULL )" +
                                  " OR (" + MediaStore.Audio.Albums.ARTIST + " == '" + artistName + "' )";
        String  sortOrderClause = MediaStore.Audio.Albums.ALBUM + " ASC";

        Cursor queryRes = contRes.query(discsUri, 
                                        queryColumns,
                                        selectionQuery,
                                        null,
                                        sortOrderClause);

Hope this helps anyone else.
Greetings!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文