查询案例的 SQlite listview 结果
Posted
技术标签:
【中文标题】查询案例的 SQlite listview 结果【英文标题】:SQlite listview results from query case when 【发布时间】:2020-03-25 07:57:11 【问题描述】:尝试在具有双重 sqlite 条件的 android 列表视图中显示 sqlite 信息。查询在 DB 浏览器中进行了测试,得到了预期的结果,但我无法在 Android 中带来相同的结果。
真实的桌子是这样的:
Sqlite 查询
SELECT
number as no,
outletname as name,
(CASE WHEN week = "WEEK1" THEN sunday ELSE 0 END) AS WK1S,
(CASE WHEN week = "WEEK1" THEN monday ELSE 0 END) AS WK1M,
(CASE WHEN week = "WEEK1" THEN tuesday ELSE 0 END) AS WK1T,
(CASE WHEN week = "WEEK1" THEN wednesday ELSE 0 END) AS WK1W,
(CASE WHEN week = "WEEK1" THEN thursday ELSE 0 END) AS WK1T,
(CASE WHEN week = "WEEK1" THEN saturday ELSE 0 END) AS WK1SA,
(CASE WHEN week = "WEEK2" THEN sunday ELSE 0 END) AS WK2S,
(CASE WHEN week = "WEEK2" THEN monday ELSE 0 END) AS WK21M,
(CASE WHEN week = "WEEK3" THEN sunday ELSE 0 END) AS WK3S,
(CASE WHEN week = "WEEK3" THEN monday ELSE 0 END) AS WK3M,
(CASE WHEN week = "WEEK3" THEN tuesday ELSE 0 END) AS WK3T,
(CASE WHEN week = "WEEK3" THEN wednesday ELSE 0 END) AS WK3W,
(CASE WHEN week = "WEEK3" THEN thursday ELSE 0 END) AS WK3T,
(CASE WHEN week = "WEEK3" THEN saturday ELSE 0 END) AS WK3SA
FROM labels5
UNION all
SELECT "GRAND TOTAL",
NULL ,
COUNT(CASE WHEN week = "WEEK1" AND sunday LIKE "%sunday%" THEN 1 END) AS WK1S,
COUNT(CASE WHEN week = "WEEK1" AND monday LIKE "%monday%" THEN 1 END) AS WK1M,
COUNT(CASE WHEN week = "WEEK1" AND tuesday LIKE "%tuesday%" THEN 1 END) AS WK1T,
COUNT(CASE WHEN week = "WEEK1" AND wednesday LIKE "%wednesday%" THEN 1 END) AS WK1W,
COUNT(CASE WHEN week = "WEEK1" AND thursday LIKE "%thursday%" THEN 1 END) AS WK1T,
COUNT(CASE WHEN week = "WEEK1" AND saturday LIKE "%saturday%" THEN 1 END) AS WK1SA,
COUNT(CASE WHEN week = "WEEK2" AND sunday LIKE "%sunday%" THEN 1 END) AS WK2S,
COUNT(CASE WHEN week = "WEEK2" AND monday LIKE "%monday%" THEN 1 END) AS WK2M,
COUNT(CASE WHEN week = "WEEK1" AND sunday LIKE "%sunday%" THEN 1 END) AS WK3S,
COUNT(CASE WHEN week = "WEEK3" AND monday LIKE "%monday%" THEN 1 END) AS WK3M,
COUNT(CASE WHEN week = "WEEK3" AND tuesday LIKE "%tuesday%" THEN 1 END) AS WK3T,
COUNT(CASE WHEN week = "WEEK3" AND wednesday LIKE "%wednesday%" THEN 1 END) AS WK3W,
COUNT(CASE WHEN week = "WEEK3" AND thursday LIKE "%thursday%" THEN 1 END) AS WK3T,
COUNT(CASE WHEN week = "WEEK3" AND saturday LIKE "%saturday%" THEN 1 END) AS WK3SA
FROM labels5
查询输出:
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:background="#FFC7C7C7"
android:orientation="vertical"
android:divider="@drawable/mydivider"
android:showDividers="middle"
android:dividerPadding="22dp"
android:weightSum="9">
<LinearLayout
android:id="@+id/lvcontainer"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:layout_weight="0.5"
android:background="#FFC7C7C7"
android:orientation="horizontal"
android:padding="1dp"
android:weightSum="3">
<TextView
android:id="@+id/txtproductcompany"
style="?android:dividerVertical"
android:layout_
android:layout_
android:layout_weight="0.63"
android:gravity="left"
android:text="number"
android:textColor="#000000"
android:textSize="13sp" />
<TextView
android:id="@+id/txtproductname"
style="?android:dividerVertical"
android:layout_
android:layout_
android:layout_weight="0.59"
android:gravity="left"
android:padding="3dp"
android:text="name"
android:textColor="#000000"
android:textSize="9sp" />
<TextView
android:id="@+id/txtproductprice1"
style="?android:dividerVertical"
android:layout_
android:layout_
android:gravity="left"
android:padding="3dp"
android:text="WK1S"
android:textColor="#000000"
android:textSize="13sp" />
<TextView
android:id="@+id/txtproductprice1"
style="?android:dividerVertical"
android:layout_
android:layout_
android:gravity="left"
android:padding="3dp"
android:text="WK1M"
android:textColor="#000000"
android:textSize="13sp" />
<TextView
android:id="@+id/txtproductprice2"
android:layout_
android:layout_
android:gravity="left"
android:padding="3dp"
android:text="WK1T"
android:textColor="#000000"
android:textSize="13sp" />
<TextView
android:id="@+id/txtproductprice3"
android:layout_
android:layout_
android:gravity="left"
android:padding="3dp"
android:text="WK1W"
android:textColor="#000000"
android:textSize="13sp" />
<TextView
android:id="@+id/txtproductprice4"
android:layout_
android:layout_
android:gravity="left"
android:padding="3dp"
android:text="WK1TH"
android:textColor="#000000"
android:textSize="13sp" />
<TextView
android:id="@+id/txtproductprice5"
android:layout_
android:layout_
android:gravity="left"
android:padding="3dp"
android:text="WK2SA"
android:textColor="#000000"
android:textSize="13sp" />
<TextView
android:id="@+id/txtproductprice122"
style="?android:dividerVertical"
android:layout_
android:layout_
android:gravity="left"
android:padding="3dp"
android:text="WK2S"
android:textColor="#000000"
android:textSize="13sp" />
<TextView
android:id="@+id/txtproductprice11"
style="?android:dividerVertical"
android:layout_
android:layout_
android:gravity="left"
android:padding="3dp"
android:text="WK2M"
android:textColor="#000000"
android:textSize="13sp" />
<TextView
android:id="@+id/txtproductprice22"
android:layout_
android:layout_
android:gravity="left"
android:padding="3dp"
android:text="WK2T"
android:textColor="#000000"
android:textSize="13sp" />
<TextView
android:id="@+id/txtproductprice31"
android:layout_
android:layout_
android:gravity="left"
android:padding="3dp"
android:text="WK2W"
android:textColor="#000000"
android:textSize="13sp" />
<TextView
android:id="@+id/txtproductprice41"
android:layout_
android:layout_
android:gravity="left"
android:padding="3dp"
android:text="WK2TH"
android:textColor="#000000"
android:textSize="13sp" />
<TextView
android:id="@+id/txtproductprice51"
android:layout_
android:layout_
android:gravity="left"
android:padding="3dp"
android:text="WK2SA"
android:textColor="#000000"
android:textSize="13sp" />
<TextView
android:id="@+id/txtproductprice19"
style="?android:dividerVertical"
android:layout_
android:layout_
android:gravity="left"
android:padding="3dp"
android:text="WK3S"
android:textColor="#000000"
android:textSize="13sp" />
<TextView
android:id="@+id/txtproductprice13"
style="?android:dividerVertical"
android:layout_
android:layout_
android:gravity="left"
android:padding="3dp"
android:text="WK3M"
android:textColor="#000000"
android:textSize="13sp" />
<TextView
android:id="@+id/txtproductprice23"
android:layout_
android:layout_
android:gravity="left"
android:padding="3dp"
android:text="WK3T"
android:textColor="#000000"
android:textSize="13sp" />
<TextView
android:id="@+id/txtproductprice33"
android:layout_
android:layout_
android:gravity="left"
android:padding="3dp"
android:text="WK3W"
android:textColor="#000000"
android:textSize="13sp" />
<TextView
android:id="@+id/txtproductprice43"
android:layout_
android:layout_
android:gravity="left"
android:padding="3dp"
android:text="WK3TH"
android:textColor="#000000"
android:textSize="13sp" />
<TextView
android:id="@+id/txtproductprice53"
android:layout_
android:layout_
android:gravity="left"
android:padding="3dp"
android:text="WK3SA"
android:textColor="#000000"
android:textSize="13sp" />
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_
android:layout_below="@+id/lvcontainer"
android:layout_weight="8.72"
android:layout_
android:divider="@drawable/separator_line"
android:dividerHeight="4.0sp"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:dividerPadding="10dp"
android:layout_centerVertical="true"
android:showDividers="beginning|middle|end"
android:cacheColorHint="#00000000"
android:footerDividersEnabled="true"
android:headerDividersEnabled="true"
></ListView>
<TextView
android:id="@+id/txtresulttext"
android:layout_
android:layout_
android:layout_below="@android:id/list"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:layout_weight="0.5"
android:gravity="left"
android:text=""
android:textColor="#FFF55F54"
android:textSize="20sp"
android:textStyle="italic|bold"></TextView>
<LinearLayout
android:id="@+id/lvbottom"
android:layout_
android:layout_
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="@+id/btnupload"
android:layout_
android:layout_
android:layout_weight="1"
android:background="#1083f5"
android:gravity="center"
android:text="UPLOAD"
android:textColor="#ffffff"
android:textSize="15sp"
android:textStyle="bold"
android:visibility="invisible" />
</LinearLayout>
</LinearLayout>
尝试使用跟随光标但没有结果
public ArrayList<HashMap<String, String>> getAllProducts12()
ArrayList<HashMap<String, String>> journalList;
journalList = new ArrayList<HashMap<String, String>>();
//String selectQuery = "SELECT * FROM labels5";
SQLiteDatabase database = this.getWritableDatabase();
String sql1="SELECT number,outletname ("+"CASE WHEN week = WEEK1 THEN sunday ELSE 0 END) AS WK1S," +
"(CASE WHEN week = WEEK1 THEN monday ELSE 0 END) AS WK1M,"+
"(CASE WHEN week = WEEK1 THEN thursday ELSE 0 END) AS WK1TH,"
+"(CASE WHEN week = WEEK1 THEN tuesday ELSE 0 END) AS WK1T,"
+"(CASE WHEN week = WEEK1 THEN wednesday ELSE 0 END) AS WK1W,"
+ "FROM" + "labels5 UNION all SELECT GRANDTOTAL" +"NULL" +
"COUNT(CASE WHEN week = WEEK1 AND sunday LIKE sunday THEN 1 END) AS WK1S," +
"COUNT(CASE WHEN week = WEEK1 AND monday LIKE monday THEN 1 END) AS WK1M,"+
"COUNT(CASE WHEN week = WEEK1 AND tuesday LIKE tuesday THEN 1 END) AS WK1MT," +
"COUNT(CASE WHEN week = WEEK1 AND thursday LIKE thursday THEN 1 END) AS WK1TH,"
+"COUNT(CASE WHEN week = WEEK1 AND wednesday LIKE wednesday THEN 1 END) AS WK1W,FROM" + "labels5";
Cursor cursor=database.rawQuery(sql1,null);
if (cursor.moveToFirst())
do
//Id, Company,Name,Price
HashMap<String, String> map = new HashMap<String, String>();
map.put("WK1S", cursor.getString(0));
map.put("WK1M", cursor.getString(1));
map.put("WK1T", cursor.getString(2));
map.put("WK1W", cursor.getString(3));
map.put("WK1TH", cursor.getString(4));
journalList.add(map);
Log.e("dataofList",cursor.getString(0)+","+cursor.getString(1)+","+cursor.getString(2)+","+cursor.getString(3)+","+cursor.getString(4)+","+cursor.getString(4));
while (cursor.moveToNext());
return journalList;
错误:
原因:android.database.sqlite.SQLiteException:没有这样的功能:GRANDTOTALNULLCOUNT(代码1 SQLITE_ERROR2):,编译时:SELECT number,outletname(CASE WHEN week = WEEK1 THEN星期天 ELSE 0 END) AS WK1S,(CASE WHEN week = WEEK1 THEN monday ELSE 0 END) AS WK1M,(CASE WHEN week = WEEK1 THEN thursday ELSE 0 END) AS WK1TH,(CASE WHEN week = WEEK1 THEN 星期二 ELSE 0 END) AS WK1T,(CASE WHEN week = WEEK1 THEN wednesday ELSE 0 END) AS WK1W,FROMlabels5 UNION all SELECT GRANDTOTALNULLCOUNT(CASE WHEN week = WEEK1 AND Sunday like sunday THEN 1 END) AS WK1S,COUNT(CASE WHEN week = WEEK1 AND monday LIKE monday THEN 1 END) AS WK1M,COUNT(CASE WHEN = WEEK1 AND tuesday LIKE tuesday THEN 1 END) AS WK1MT,COUNT(CASE WHEN week = WEEK1 AND thursday Like thursday THEN 1 END) AS WK1TH ,COUNT(CASE WHEN week = WEEK1 AND wednesday LIKE wednesday THEN 1 END) AS WK1W,FROMlabels5 在 android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
非常不确定 rawquery 是否支持这种情况。如果有人可以通过建议光标查询发现亮点或任何人将不胜感激,将不胜感激
【问题讨论】:
朋友们让我知道我在这里缺少什么来分享您的建议?? 【参考方案1】:当你连接多段 sql 代码时,请确保在这些段之间包含空格或逗号,如果像 "GRAND TOTAL"
这样的列包含非法字符(空格),请将其括在方括号中,或者如果它是字符串文字用单引号括起来:
String sql1 = "SELECT " +
"number as no, " +
"outletname as name, " +
"(CASE WHEN week = 'WEEK1' THEN sunday ELSE 0 END) AS WK1S, " +
"(CASE WHEN week = 'WEEK1' THEN monday ELSE 0 END) AS WK1M, " +
"(CASE WHEN week = 'WEEK1' THEN tuesday ELSE 0 END) AS WK1T, " +
"(CASE WHEN week = 'WEEK1' THEN wednesday ELSE 0 END) AS WK1W, " +
"(CASE WHEN week = 'WEEK1' THEN thursday ELSE 0 END) AS WK1T, " +
"(CASE WHEN week = 'WEEK1' THEN saturday ELSE 0 END) AS WK1SA, " +
"(CASE WHEN week = 'WEEK2' THEN sunday ELSE 0 END) AS WK2S, " +
"(CASE WHEN week = 'WEEK2' THEN monday ELSE 0 END) AS WK21M, " +
"(CASE WHEN week = 'WEEK3' THEN sunday ELSE 0 END) AS WK3S, " +
"(CASE WHEN week = 'WEEK3' THEN monday ELSE 0 END) AS WK3M, " +
"(CASE WHEN week = 'WEEK3' THEN tuesday ELSE 0 END) AS WK3T, " +
"(CASE WHEN week = 'WEEK3' THEN wednesday ELSE 0 END) AS WK3W, " +
"(CASE WHEN week = 'WEEK3' THEN thursday ELSE 0 END) AS WK3T, " +
"(CASE WHEN week = 'WEEK3' THEN saturday ELSE 0 END) AS WK3SA " +
"FROM labels5 " +
"UNION all " +
"SELECT " +
"'GRAND TOTAL', " +
"NULL , " +
"COUNT(CASE WHEN week = 'WEEK1' AND sunday LIKE '%sunday%' THEN 1 END) AS WK1S, " +
"COUNT(CASE WHEN week = 'WEEK1' AND monday LIKE '%monday%' THEN 1 END) AS WK1M, " +
"COUNT(CASE WHEN week = 'WEEK1' AND tuesday LIKE '%tuesday%' THEN 1 END) AS WK1T, " +
"COUNT(CASE WHEN week = 'WEEK1' AND wednesday LIKE '%wednesday%' THEN 1 END) AS WK1W, " +
"COUNT(CASE WHEN week = 'WEEK1' AND thursday LIKE '%thursday%' THEN 1 END) AS WK1T, " +
"COUNT(CASE WHEN week = 'WEEK1' AND saturday LIKE '%saturday%' THEN 1 END) AS WK1SA, " +
"COUNT(CASE WHEN week = 'WEEK2' AND sunday LIKE '%sunday%' THEN 1 END) AS WK2S, " +
"COUNT(CASE WHEN week = 'WEEK2' AND monday LIKE '%monday%' THEN 1 END) AS WK2M, " +
"COUNT(CASE WHEN week = 'WEEK1' AND sunday LIKE '%sunday%' THEN 1 END) AS WK3S, " +
"COUNT(CASE WHEN week = 'WEEK3' AND monday LIKE '%monday%' THEN 1 END) AS WK3M, " +
"COUNT(CASE WHEN week = 'WEEK3' AND tuesday LIKE '%tuesday%' THEN 1 END) AS WK3T, " +
"COUNT(CASE WHEN week = 'WEEK3' AND wednesday LIKE '%wednesday%' THEN 1 END) AS WK3W, " +
"COUNT(CASE WHEN week = 'WEEK3' AND thursday LIKE '%thursday%' THEN 1 END) AS WK3T, " +
"COUNT(CASE WHEN week = 'WEEK3' AND saturday LIKE '%saturday%' THEN 1 END) AS WK3SA " +
"FROM labels5";
【讨论】:
以下错误“您可能错过了这些列之间的空格: “labels5 UNION all SELECT GRANDTOTAL”+“NULL”+“COUNT ... 尝试将其更改为: "labels5 UNION all SELECT GRANDTOTAL " +"NULL" + " COUNT ...
【讨论】:
以上是关于查询案例的 SQlite listview 结果的主要内容,如果未能解决你的问题,请参考以下文章
相同的 Pandas 和 SQLite 查询没有给出相同的结果
Android 日记应用 - 搜索 SQLite 数据库并在 ListView 中显示结果
如何将数据从 listview 单选按钮保存到 SQLite?