算在源码数据库表的数量 [英] Count the number of tables in the sqlite database

查看:117
本文介绍了算在源码数据库表的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的是目前在我的SQLite数据库中的表的计数。试图寻找,但没有得到任何直接的方法。

I need a count of the tables that are currently in my sqlite database. Tried searching but did not get any direct method.

我在DbHelper尝试过这种方法。

I tried this method in my DbHelper.

public int countTables() {
    int count = 0;
    String SQL_GET_ALL_TABLES = "SELECT * FROM sqlite_master WHERE type='table'";
    Cursor cursor = getReadableDatabase()
            .rawQuery(SQL_GET_ALL_TABLES, null);
    cursor.moveToFirst();
    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {

        count++;
        getReadableDatabase().close();

    }
    cursor.close();
    return count;
}

但这种方法给了我一个错误的数。我只有3个在我的数据库表,但此方法返回算作5.什么可能是错用此方法。有没有直接的方法来获取计数。

But this method gives me a wrong count. I have only 3 tables in my DB, but this method returns count as 5. What could be wrong with this method. Is there any direct method to get the count.

推荐答案

您的方法是好的,你甚至可以使用 SELECT COUNT(*)... 。但也有自动创建两个表 android_metadata sqlite_sequence ,简单地将这些考虑在内:5 - 2 = 3的表你创建的。

Your method is fine, you could even use SELECT count(*) .... But there are two tables created automatically android_metadata and sqlite_sequence, simply takes these into account: 5 - 2 = 3 tables that you created.

或者因为Squonk提供有关何时 sqlite_sequence 是出色的文件创建时,它可能的没有的存在,我建议这个查询:

Or since Squonk provided excellent documentation about when sqlite_sequence is created and when it might not exist, I recommend this query:

SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name != 'android_metadata' AND name != 'sqlite_sequence';

几个指针,以缩短你的code

这一切:

cursor.moveToFirst();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {

可以完成这样的:

Can be accomplished like this:

while(cursor.moveToNext()) {

Cursor.moveToNext()返回true,如果下一行存在。如果一个新的光标不为空使用MoveToNext()将返回真和在第一位置设置这个索引。当 cursor.isAfterLast()==真,使用MoveToNext()返回false。

Cursor.moveToNext() return true if the next row exists. If a new cursor is not empty moveToNext() will return true and set this index on the first position. When cursor.isAfterLast() == true, moveToNext() returns false.

但如果你只是想看看有多少行是在游标的使用:

But if you simply want to count how many rows are in the Cursor use:

int count = cursor.getCount();

最后,存储在一个变量的可写的数据库。

Lastly, store your writable database in a variable.

getReadableDatabase().close();

我没有测试过这一点,但如果getReadableDatabase()返回一个新的SQLiteDatabase对象每一次,那么你只是一旦你已经创造了它关闭这个新的数据库......我会关闭数据库的一次的,我会在>我已经关闭了所有我从中获取的游标。

I haven't tested this, but if getReadableDatabase() returns a new SQLiteDatabase object each time then you are simply closing this new database as soon as you have created it... I would close the database once and I would close it after I have closed all of the Cursors I retrieved from it.

SQLiteDatabase database = getReadableDatabase();
...

database.close();

如果您使用的是大量的查询考虑开放你的onResume(数据库),并关闭它的onPause()。

If you are using a lot of queries consider opening your database in onResume() and closing it in onPause().

这篇关于算在源码数据库表的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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