SQLiteDatabases和光标 [英] SQLiteDatabases and Cursors

查看:204
本文介绍了SQLiteDatabases和光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果有人可以给我的Andr​​oid游标的简要概述。一对夫妇的具体问题:

I was wondering if someone could give me a brief overview of Android Cursors. A couple of specific questions:

1 - 我有一个数据库查询后返回游标的方法:

1 - I have a method which returns a cursor after a database query:

    public static Cursor getVehicles()
{
    SQLiteDatabase db = vehicleData.getReadableDatabase();
    Cursor cursor = db.query(TABLE_NAME, GET_VEHICLES_FROM_CLAUSE, null, null, null, null, ORDER_BY);

    return cursor;
}

为了做家政,我想db.close()刚刚返回语句之前。然而,这引起了返回指针到包含任何行。这是为什么?

In order to do housekeeping, I tried db.close() just before the return statement. However, this caused the returned cursor to contain no rows. Why is this?

2 - 什么是关闭游标并关闭数据库的区别

2 - What's the difference between closing a cursor and closing a database?

3 - 我是否需要关闭游标上调用,如果它是一个局部变量,或者我可以离开它的垃圾收集清理

3 - Do I need to call close on a Cursor if it is a local variable, or can I leave it to the garbage collector to clean up?

4 - 我的数据库很小,只有我的应用程序使用 - 可我只是把它打开

4 - My database is small and only used by my application - can I just keep it open?

推荐答案

1)光标是只是一个指针由您查询返回的数据,它不包含从查询的所有数据。这是为了提高性能/效率(大的结果集不是一次读取 - >使用更少的内存)。因此,如果您关闭数据库,光标无法检索数据 - >它是空的。

1) The cursor is just a pointer to the data returned by your query, it doesn't contain all the data from your query. This is to increase performance/efficiency (large resultsets aren't read at once -> less memory used). Therefore, if you close the database, the cursor can't retrieve the data -> it's empty.

2)当你关闭游标,所有相关的资源被释放 - >你不能访问这个光标(因为它已被释放)相关的数据,但你可以使用这个或其他游标新的查询。当您关闭数据库,则无法查询它了(直到你重新打开它)。

2) When you close a cursor, all associated resources are released -> you can't access the data associated with this cursor (since it has been released), but you can make new queries using this or other cursors. When you close a database, you can't query it anymore (until you re-open it).

3)始终关闭游标。否则,你会遇到的问题 - 如果游标不关闭,新的查询被封锁的GC会抱怨。

3) Always close cursors. Otherwise you will run into problems - the GC will complain if the cursor isn't closed and new queries are blocked.

4)如果您关闭它,当你的应用程序完成的,是的。

4) If you close it when your app finishes, yes.

这篇关于SQLiteDatabases和光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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