【发布时间】:2017-09-13 14:24:42
【问题描述】:
使用 CREATE VIEW 的 id 编号如何在 Android 中工作?
主键 (id) 是否会自动生成,或者我是否会遇到错误?
(我将尝试将此视图加载到游标加载器中。)
创建我的交易表的代码:
public static final String TABLE_TRANSACTIONS = "transactions";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_PAYEE = "payee";
public static final String COLUMN_AMOUNT = "amount";
public static final String COLUMN_CATEGORY = "category";
public static final String COLUMN_MONTH = "month";
public void onCreate(SQLiteDatabase db) {
String sqlQueryTransactions = "CREATE TABLE " + TABLE_TRANSACTIONS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PAYEE + " TEXT, " +
COLUMN_AMOUNT + " TEXT, " +
COLUMN_CATEGORY + " TEXT, " +
COLUMN_MONTH + " TEXT " +
")";
db.execSQL(sqlQueryTransactions);
}
表格示例如下:
id payee amount month
1 Tom 90 March
3 Tom 66 April
4 Tom 89 May
10 Jasmine 125 April
11 Nancy 151 March
12 Jasmine 175 April
13 Nancy 152 April
我要创建并用于在列表视图中显示的 VIEW 是:
payee March April May
Tom 90 66 89
Jasmine -- 300 --
Nancy 151 152 --
将使用以下 SQL 查询生成此 VIEW:
SELECT `payee`
, SUM(CASE WHEN `month` = 'March' THEN `amount` END) AS `March`
, SUM(CASE WHEN `month` = 'April' THEN `amount` END) AS `April`
, SUM(CASE WHEN `month` = 'May' THEN `amount` END) AS `May`
FROM `transactions`
GROUP BY `payee`;
【问题讨论】:
-
“ID 编号”是什么意思? ID 列与其他列一样,不属于此视图。
标签: sql sqlite android-studio cursor sql-view