戻る

count(*)
indexedDB

select count(*) from table
件数を取得する関数はindexedDBでは用意されていないようなので代替方法のサンプルとなります。

[サンプル]
copy
async function testSelectCount(dbName, storeName) 
{
	const db = await databaseOpen(dbName);
	//count(*)
	let count = 0;
	await runTx(db, storeName, "readwrite", (store) => 
	{
		const cursorReq = store.openCursor();
		cursorReq.onsuccess = (event) => 
		{
			const cursor = event.target.result;
			if (cursor) 
			{
				count++;
			}
			cursor.continue();
		};
	});
	db.close();
	console.log("testSelectCount count:" + count);
}
async function testSelectCountWhere(dbName, storeName) 
{
	const db = await databaseOpen(dbName);
	//count(*) where
	let count = 0;
	await runTx(db, storeName, "readwrite", (store) => 
	{

		const cursorReq = store.openCursor();

		cursorReq.onsuccess = (event) => 
		{
			const cursor = event.target.result;
			if (cursor) 
			{
				const record = cursor.value;
				if(record.f2 > 4)
				{
					count++;
				}
				cursor.continue();
			}
		};
	});
	db.close();
	console.log("testSelectCountWhere count:" + count);
}
testSelectCount("aaa", "tbl");
testSelectCountWhere("aaa", "tbl");

async function testSelectCount(dbName, storeName)
全件を対象とした合計をカウントするサンプルです。

const cursorReq = store.openCursor();
カーソルを準備します。

const cursor = event.target.result;
カーソルのレコードを取得します。

if (cursor)
カーソルが存在するケース

count++;
カウントアップします。

cursor.continue();
次のカーソルを読み込みます。

async function testSelectCountWhere(dbName, storeName)
f2の値が4よりも大きい値をカウントするサンプルです。

const record = cursor.value;
カーソルからレコードを取得します。

if(record.f2 > 4)
レコードのf2フィールドを判定します。




戻る


著作権情報
ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。
ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、
または公開したりすることはできません。
当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、
その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について
一切の責任を負わないものとします。