Android SQL Null 常量值
我有这个提示给你......
我们正在尝试将参数传递给内容解析器中的查询。如果参数为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将空字符串转为null
Turn empty strings into null
我们通过这种方式解决了选择问题:
希望这对其他人有帮助。
问候!
We resolved our problem making the selection this way:
Hope this helps anyone else.
Greetings!