ANDROID:查找 SqliteDatabase 中单个表的大小 [英] ANDROID: Finding the size of individual tables in a SqliteDatabase

查看:48
本文介绍了ANDROID:查找 SqliteDatabase 中单个表的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照以下方式做一些事情:

I am trying to do something along the following lines:

SELECT bytes from user_segments where segment_name = 'TABLE_NAME';

我知道我可以通过

mDb = SQLiteDatabase;
long size = new File(mDb.getPath()).length();

是否可以在 Android 的 SQLiteDatabase 中获取单个表在存储中占用多少空间?

Is it possible to get how much space an individual table takes up in storage in Android's SQLiteDatabase?

编辑--

由于缺乏响应,我想 SQLite 是不可能的.我目前正在使用一种 hack-ish 方式来计算表中的行数并估计单行占用多少空间(基于给定的表架构).

By the lack of response, I guess that it's not possible with SQLite. I am currently using a hack-ish way where I count the number of rows in the table and estimate how much space a single row takes (based on the given table schema).

推荐答案

如果您主要将二进制或字符串数​​据存储在单个列中,则可以使用聚合查询:

If you mostly store binary or string data in a single column, you can use an aggregate query:

SELECT SUM(LENGTH(the_column)) FROM the_table

即使每行的大小变化很大(例如,如果您将图片存储在数据库中),这也可以为您提供合理的估计.但是,如果您有许多列,每列包含少量数据,那么您的方法(估计每行固定大小)会更好.

This can give you a reasonable estimate even when the size of each row varies a lot (for example if you store pictures in the database). However, if you have many columns with a small amount of data in each, your approach (estimating a fixed size per row) is better.

在 Android 上,您可以按如下方式实现:

On Android, you can implement it like the following:

SQLiteDatabase database = ... // your database
// you can cache the statement below if you're using it multiple times
SQLiteStatement byteStatement = database.compileStatement("SELECT SUM(LENGTH(the_column)) FROM the_table");
long bytes = byteStatement.simpleQueryForLong();

这篇关于ANDROID:查找 SqliteDatabase 中单个表的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆