1. where you open and close depends on your app. You can keep the db
open, and close and re-open in onResume and onPause, or you can open
and close before and after you access the db.

3. just retrieve the data that you need in the getView of your list
adapter, don't retrieve complete tables if you don't need them. Of
course, it depends on your app and on the size of the table whether
you want to keep the table in memory or not.

On May 9, 4:22 pm, kamiseq <[email protected]> wrote:
> hi,
> Im starting with android and have few questions about database access.
>
> 1. Every tutorial I ve read (ie Notepad) that uses sqlite creates and
> opens connection via SQLiteOpenHelper in onCreate callback. but I
> really have never seen that the connection is closed (androids throws
> exceptions that there is a leak as connection is never closed). So I
> assume that onSaveInstanceState is the best bet to close as onCreate
> opens the connection. but maybe it should be sooner - something like
> onResume (for open) and onPause (for close). or maybe those tutorials
> are too trivial and I should create another layer for accessing data
> in services, so that data could be shared between activities and
> wouldn't suffer from activity life cycle.
>
> the only thing is that the exception is thrown after a while - not
> immediately after destroying activity
> D/MyMainActivity :(  305): on Pause.
> D/MyMainActivity :(  305): on Stop.
> D/MyMainActivity :(  305): on Destroy.
> D/dalvikvm(  101): GC freed 408 objects / 18416 bytes in 78ms
> I/ActivityManager(   55): Starting activity: Intent
> { act=android.intent.action.MAIN
> cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=info.kami
> D/MyMainActivity :(  305): on create.
> D/MyMainActivity :(  305): on Start.
> D/MyMainActivity :(  305): on Resume.
> I/ActivityManager(   55): Displayed activity
> my.app.android/.MainActivity: 664 ms (total 664 ms)
> I/ActivityManager(   55): Starting activity: Intent
> { act=android.intent.action.MAIN cat=[android.intent.category.HOME]
> flg=0x10200000 cmp=com.android.l
> D/MyMainActivity :(  305): on Pause.
> D/MyMainActivity :(  305): on Stop.
> I/ActivityManager(   55): Starting activity: Intent
> { act=android.intent.action.MAIN
> cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=info.kami
> D/MyMainActivity :(  305): on Restar.
> D/MyMainActivity :(  305): on Start.
> D/MyMainActivity :(  305): on Resume.
> D/dalvikvm(  305): GC freed 5465 objects / 347528 bytes in 95ms
> E/Database(  305): Leak found
> E/Database(  305): java.lang.IllegalStateException: mPrograms size 1
> E/Database(  305):      at
> android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:
> 1668)
> E/Database(  305):      at dalvik.system.NativeStart.run(Native
> Method)
> E/Database(  305): Caused by: java.lang.IllegalStateException: /data/
> data/my.app.android/databases/data SQLiteDatabase created and never
> closed
> E/Database(  305):      at
> android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:
> 1694)
> E/Database(  305):      at
> android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:
> 738)
> E/Database(  305):      at
> android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:
> 760)
> E/Database(  305):      at
> android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:
> 753)
> E/Database(  305):      at
> android.app.ApplicationContext.openOrCreateDatabase(ApplicationContext.java:
> 473)
> E/Database(  305):      at
> android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:
> 193)
> E/Database(  305):      at
> android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:
> 98)
> E/Database(  305):      at
> my.app.android.dao.SQLiteTemplate.openToReadWrite(SQLiteTemplate.java:
> 60)
> E/Database(  305):      at
> my.app.android.dao.SQLiteCallDao.open(SQLiteCallDao.java:54)
> E/Database(  305):      at
> my.app.android.MainActivity.getDao(MainActivity.java:102)
> E/Database(  305):      at
> my.app.android.MainActivity.onCreate(MainActivity.java:53)
> E/Database(  305):      at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:
> 1047)
> E/Database(  305):      at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
> 2459)
> E/Database(  305):      at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
> 2512)
> E/Database(  305):      at android.app.ActivityThread.access
> $2200(ActivityThread.java:119)
> E/Database(  305):      at android.app.ActivityThread
> $H.handleMessage(ActivityThread.java:1863)
> E/Database(  305):      at
> android.os.Handler.dispatchMessage(Handler.java:99)
> E/Database(  305):      at android.os.Looper.loop(Looper.java:123)
> E/Database(  305):      at
> android.app.ActivityThread.main(ActivityThread.java:4363)
> E/Database(  305):      at
> java.lang.reflect.Method.invokeNative(Native Method)
> E/Database(  305):      at java.lang.reflect.Method.invoke(Method.java:
> 521)
> E/Database(  305):      at com.android.internal.os.ZygoteInit
> $MethodAndArgsCaller.run(ZygoteInit.java:860)
> E/Database(  305):      at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
> E/Database(  305):      at dalvik.system.NativeStart.main(Native
> Method)
> E/Database(  305): Leak found
>
> 2. Is it really that important (from performance point of view) to use
> cursor when possible? usually I map each row in cursor to some type
> and work on a List of that type.
> 3. how to avoid fetching (even that db is local) all data from some
> table when my ListView can show only few results at a time. does api
> support kind of data scrolling - that I missed reading tutorials?? Or
> should I take care of such situation by myself and fetch-replace data
> in the view when user drag the scroll bar.
> 4. I would love to AndroidManifest.xml act as IoC bean descriptor so
> in code you could annotate field to have your data-service injected.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group 
> athttp://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to