Import big file text with freeze screen

luiswagnersantos

Member
Licensed User
Longtime User
Hi friends.
I need some tips on how to solve the problem below.

My server generates a text file with 6,000 products already formatted in sql
example:
PHP:
insert into [DB_PRODUTOS] values ​​(6685, '000004 ', '7897374400047', 'ALUMINIOS MARANA', 'METALS', 'ROUND pan # 35', 'PC', 14.04000, .00000, .00000,2,'' , 0, 'N', '9 ')
insert into [DB_PRODUTOS] values ​​(6680, '000005 ', '7897374400054', 'ALUMINIOS MARANA', 'METALS', 'pan RECTANGULAR LOW N. 02', 'PC', 8.54000, .00000, .00000,2 '', 0,' N ', '5')
i ......
This file has 4800 lines with size: 735 KB

In android I am using the following code to read the file and saves it in the database.


'Reading File Text
PHP:
Sub ReadTextReader
Dim sdRoot As String
Dim TextReader1 As TextReader
   
sdRoot = File.DirDefaultExternal & "/"
TextReader1.Initialize(File.OpenInput(sdRoot , "produtos.txt"))
   
Dim line As String
line = TextReader1.ReadLine    
   
Do While line <> Null
    Log(line) 'write the line to LogCat
      
                 'Post in database
                DB_PRODUTOS_EXECUTE(line)

      line = TextReader1.ReadLine
    Loop
   TextReader1.Close
End Sub

'Post in database
PHP:
Sub DB_PRODUTOS_EXECUTE(str_sql As String)As String

BancodeDados.SQL.BeginTransaction

Try
   
   BancodeDados.SQL.ExecNonQuery(str_sql)
   BancodeDados.SQL.TransactionSuccessful
   
Catch
   Msgbox("Error: DB_PRODUTOS_EXECUTE. Erro: "&LastException,"Error")
   Log("Error: DB_PRODUTOS_EXECUTE. Error: "&LastException)
End Try
   BancodeDados.SQL.EndTransaction
End Sub

In android I am using the following code to read the file and saves it in the bank.

This procedure is working but I can not put a progressbar to inform the User that the screen freezes.
Could someone give some tips on how to show progress to the user?
If using as a service is correct?


I appreciate any response.
thank you
 

mc73

Well-Known Member
Licensed User
Longtime User
I think that a progress bar would require that you know the total mumber of lines of your text file. This can be done of course, by firstly running the loop and counting, and afterwards rerunning the loop to do what you want. Also, you could write the row lines size, somewhere, be it in the beginning of your text file or to another file.
Another solution would be to simply inform the user, which line you are currently reading, by using a label for example.
 
Upvote 0

fabio borges

Member
Licensed User
Longtime User
A maneira simples é voce dividir o tambem do arquivo que voce ja tem pelo tamanho da linha que esta lendo e acumular esse tamanho, dai voce tera o quantidade de bytes ja lido do arquivo em percuntual, dai é só alimentar um seekbar por exemplo.
 
Upvote 0

luiswagnersantos

Member
Licensed User
Longtime User
response
Caro Fabio
Obrigado por responder.
Como faço para pegar o tamanho da linha ?
Obrigado

Dear Fabio
Thanks for responding.
How do I get the size of the line?
thank you

English Question.
A simple way is you also share the file that you already have the size of the line who is reading this and build size, hence you will have the number of bytes already read from the file percuntual, give just a seekbar food for example.
 
Upvote 0

luiswagnersantos

Member
Licensed User
Longtime User
Obrigado Fábio, fiz de várias maneiras. Contando o numero de linhas e também como tinha me orientado mas quando clico no botão para executar ele fica com foco. O processo todo é executado e depois só me retorna o final. Não consigo atualizar nenhum controle neste momento.

English

Thanks Fabio, I made a number of ways. Counting the number of lines as well as he guided me but when I click to run it gets focused. The whole process is run and then just returns me to the end. I can not update any control at this time.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Perhaps you should try 'doevents' inside your loop.
Obrigado Fábio, fiz de várias maneiras. Contando o numero de linhas e também como tinha me orientado mas quando clico no botão para executar ele fica com foco. O processo todo é executado e depois só me retorna o final. Não consigo atualizar nenhum controle neste momento.

English

Thanks Fabio, I made a number of ways. Counting the number of lines as well as he guided me but when I click to run it gets focused. The whole process is run and then just returns me to the end. I can not update any control at this time.
 
Upvote 0

luiswagnersantos

Member
Licensed User
Longtime User
uauauauuauaua.
I do not think it was just the problem.
Problem solved!
Thank mc73. You're the man!
 
Upvote 0

luiswagnersantos

Member
Licensed User
Longtime User
Tank´s Fabio and mc 73 for helping

Code Used




PHP:
Sub ReadTextReader As String
Dim i As Int 
Dim sdRoot As String
Dim TextReader1 As TextReader
Dim NumLinhas As Int
Dim line As String
   
   
   i=0
   sdRoot = File.DirDefaultExternal & "/"
    
   TextReader1.Initialize(File.OpenInput(sdRoot , "produtos.txt"))
   'Get Number Lines of File
   NumLinhas = GetNumberLines("produtos.txt")
   
   'Messages for users
   ListLog.AddSingleLine("Please Wait.. processing")
   ProgressDialogShow("Please Wait.. processing")
   
    line = TextReader1.ReadLine    
   
   'Read File
    Do While line <> Null
         i=i +1
         line = TextReader1.ReadLine
         ProgBarStatus.Progress=(i*100)/NumLinhas
         DB_PRODUTOS_EXECUTE(line)
         DoEvents 'never forget within a loop
   Loop
   
   TextReader1.Close'Fechando Arquivo
   ProgressDialogHide
   ToastMessageShow("Success!",False)
End Sub



PHP:
Sub DB_PRODUTOS_EXECUTE(str_sql As String)As String
BancodeDados.SQL.BeginTransaction

Try
   
   BancodeDados.SQL.ExecNonQuery(str_sql)
   BancodeDados.SQL.TransactionSuccessful
   
Catch
   Msgbox("Erro em executar a DB_PRODUTOS_EXECUTE. Erro: "&LastException,"Erro")
   Log("Erro em executar: DB_PRODUTOS_EXECUTE. Erro: "&LastException)
End Try
   BancodeDados.SQL.EndTransaction
End Sub



PHP:
Sub GetNumberLines(NomeArquivo As String) As Int
Dim sdRoot As String 
Dim TextReader1 As TextReader 
Dim line As String 
Dim i As Int


'Zerando Váriavel
Try
   i=0
   sdRoot = File.DirDefaultExternal & "/" 
   TextReader1.Initialize(File.OpenInput(sdRoot ,NomeArquivo)) 
     

   line = TextReader1.ReadLine     
     
   Do While line <> Null 
       'Log(line) 'write the line to LogCat 
        line = TextReader1.ReadLine 
      i=i+1
    Loop 
   
Catch
   Msgbox("Erro em executar a GetNumberLines. Erro: "&LastException,"Erro")
   Log("Erro em executar: GetNumberLines. Erro: "&LastException)
End Try
    TextReader1.Close 'Fechando o Arquivo
   Return i
End Sub
 
Upvote 0
Top