Recent

Author Topic: Working with sqlite in AsyncTask  (Read 495 times)

las

  • New Member
  • *
  • Posts: 26
Working with sqlite in AsyncTask
« on: May 09, 2019, 11:31:31 pm »
Hi!
I'm trying to work with sqlite in AsyncTask. But if I create jSqliteDataAccess in the process of the program, and not put on the form, it does not work. If I pass a link to jSqliteDataAccess, located on the form of a procedure that works in AsyncTask, then a Java exception occurs. After several calls to Sqlite (most likely because from AsyncTask you cannot access objects running in the main thread). How can I work with sqlite in AsyncTask?

jmpessoa

  • Hero Member
  • *****
  • Posts: 1479
Re: Working with sqlite in AsyncTask
« Reply #1 on: May 10, 2019, 04:09:17 am »

Hi, las!

I can try implement:

SelectAsync   
DropTableAsync
CreateTableAsync
InsertIntoTableAsync
UpdateTableAsync, ...
[and others.... whats?]

and  events to  handle/notify  about the  tasks...

What about?
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

las

  • New Member
  • *
  • Posts: 26
Re: Working with sqlite in AsyncTask
« Reply #2 on: May 10, 2019, 02:06:23 pm »
I run a procedure that loads several dozen files across the network and puts the data into the sqlite database. The whole procedure lasts about a minute. Implementing one asynchronous database query will not help. It is necessary to change the algorithm of work. It would be very helpful to be able to run several execSQL in one transaction (several thousand queries pass).

However, the question remains: why if I create jSqliteDataAccess programmatically, then it does not work if it is not on the form? What is wrong?

Like that:
db: = jSqliteDataAccess.Create (self);
db.init (gApp);
db.cursor: = jSqliteCursor.Create (db.Owner);
db.cursor.init (gApp);

The same situation with jTextFileManager. If I just write as below, then the application falls. And if you put jTextFileManager on the form, then everything works.

fm: = jTextFileManager.Create (AndroidModule1);
fm.Init (gApp);

jmpessoa

  • Hero Member
  • *****
  • Posts: 1479
Re: Working with sqlite in AsyncTask
« Reply #3 on: May 12, 2019, 05:22:54 am »
Quote
It would be very helpful to be able to run several execSQL in one transaction....

Done!

Added build-in  transaction and boolean result to  "InsertIntoTableBatch"  method
Added build-in  transaction and boolean result to  "UpdateTableBatch"  method

and

Added "ExecSQLBatchAsync" [build-in  transaction] method   [can be used to "Insert" or "Update" ...  etc...]
Added  event "OnAsyncPostExecute" to notify Async task result....

Added SetBatchAsyncTaskType to remind  ExecSQLBatchAsync state....

Please, test it!

Quote
why if I create jSqliteDataAccess programmatically, then it does not work if it is not on the form? What is wrong?

This is a LAMW limitation ... you need to have at least one component of this type on form for the dynamic/runtime component creation to work....
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard