Android Question Import a CSV File

Hi

I need help please.

I can load a CSV File, but when I want to read the file and save it in a SQLite Database, it tells me that the file does not exit.

Any ideas please?

Read CSV File:
[/
Sub Import_Coords
    Dim fd As FileDialog
    fd.FilePath = File.DirRootExternal
    fd.TextColor=Colors.Black
    fd.FileFilter=".csv,.txt"
    fd.ScrollingBackgroundColor=Colors.Gray
    Dim sf As Object = fd.ShowAsync("Select File", "Import", "Cancel", "", Null, False)
    Wait For (sf) Dialog_Result(Result As Int)
    If Result = DialogResponse.POSITIVE Then
        Log("File path: " & fd.FilePath)
        Log("File name: " & fd.ChosenName)
        SaveFile(fd.FilePath, fd.ChosenName)
    End If
End Sub

Sub SaveFile(A As String, B As String)
    Dim FileName As String
    Dim lst1 As List
   
    FileName= A & "/" & B
    Try
        lst1.Initialize
        CGlobals.SQL1.BeginTransaction
        lst1 = SU.LoadCSV(File.DirRootExternal, FileName, ",")
        For i = 0 To lst1.Size - 1
            Dim sColumn() As String
            sColumn = lst1.Get(i)
            If CGlobals.CoordCode=1 Then
                'Site Coords
                CGlobals.SQL1.ExecNonQuery2("INSERT INTO SCoords VALUES (a,b,c,d,e) VALUES (?, ?, ?, ?, ?)", sColumn)
            else if CGlobals.CoordCode=2 Then
                'Global Coords
                CGlobals.SQL1.ExecNonQuery2("INSERT INTO GCoords VALUES (a,b,c,d,e) VALUES (?, ?, ?, ?, ?)", sColumn)
            Else
                'Job Coords
                CGlobals.SQL1.ExecNonQuery2("INSERT INTO Coords VALUES (a,b,c,d,e) VALUES (?, ?, ?, ?, ?)", sColumn)
            End If
        Next
        CGlobals.SQL1.TransactionSuccessful
    Catch
        Log(LastException.Message)
    End Try

'    Dim SU As StringUtils
'    Dim Table As List
'    Table = SU.LoadCSV(File.DirRootExternal, FileName, ",")
'    Dim Table2 As List
'    Dim Items() As String
'    Table2.Initialize
'    For i = 0 To Table.Size - 1
'        Items = Table.Get(i)
'        Dim m As Map
'        m.Initialize
'        m.Put("column 1", Items(0)) 'You will need to replace the columns names with the correct names
'        m.Put("column 2", Items(1))
'        Table2.Add(m)
'    Next
'
'    DBUtils.InsertMaps(CGlobals.SQL1, "YourTable", Table2)



    CGlobals.SQL1.EndTransaction
    Msgbox2Async("Points Imported", "Imported", "OK", "", "", Null,False)
    Wait For Msgbox_Result (Answ As Int)
End Sub
]
 

Mahares

Expert
Licensed User
Longtime User
CGlobals.SQL1.ExecNonQuery2("INSERT INTO SCoords VALUES (a,b,c,d,e) VALUES (?, ?, ?, ?, ?)", sColumn)
The 3 INSERT lines don;t look correct.
Try:
B4X:
CGlobals.SQL1.ExecNonQuery2("INSERT INTO SCoords  VALUES (?, ?, ?, ?, ?)", sColumn)
Aside, from that, since you are using File.DIrRootExternal, I assume you are handling that correctly.
 
Upvote 0
Hi Mahares

Thank you for coming back to me. I am stuck on line no 25, I get a message that the file does not exist, but yet under "Import_Coords", I can see all the Directories and Files and I select a File and thereafter I go to "SaveFile". That is where the problem is, on line 25.

Message:

Logger connected to: Ulefone Armor 8 Pro
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (startapp) Create, isFirst = true **
** Activity (startapp) Resume **
** Activity (startapp) Pause, UserClosed = false **
** Activity (maindesign) Create, isFirst = true **
** Activity (maindesign) Resume **
** Activity (maindesign) Pause, UserClosed = false **
** Activity (addcoords) Create, isFirst = true **
** Activity (addcoords) Resume **
File path: /storage/emulated/0/Download
File name: CONTROL.csv
Error occurred on line: 536 (AddCoords)
java.io.FileNotFoundException: /storage/emulated/0/storage/emulated/0/Download/CONTROL.csv: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:492)
at java.io.FileInputStream.<init>(FileInputStream.java:160)
at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:215)
at anywheresoftware.b4a.objects.streams.File.ReadString(File.java:277)
at anywheresoftware.b4a.objects.StringUtils.LoadCSV2(StringUtils.java:160)
at anywheresoftware.b4a.objects.StringUtils.LoadCSV(StringUtils.java:153)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
at b4a.example.addcoords._savefile(addcoords.java:1536)
at b4a.example.addcoords$ResumableSub_Import_Coords.resume(addcoords.java:1319)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.agraham.dialogs.InputDialog$FileDialog$2.onClick(InputDialog.java:2079)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:184)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7731)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:997)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Linux.open(Native Method)
at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7615)
at libcore.io.IoBridge.open(IoBridge.java:478)
... 36 more
java.lang.Exception: java.io.FileNotFoundException: /storage/emulated/0/storage/emulated/0/Download/CONTROL.csv: open failed: ENOENT (No such file or directory)

Thanks
Michael
 
Upvote 0
Hi Mahares

OK, I solved my problem. See line no 12.

I changed my code to this and it works now:

Changed Code:
[/
Sub SaveFile(A As String, B As String)
    Dim FileName As String
    Dim lst1 As List
   
    FileName= A & "/" & B
   
    Try
        lst1.Initialize
        CGlobals.SQL1.BeginTransaction
        'lst1 = SU.LoadCSV(File.DirRootExternal, FileName, ",")
       [B] lst1 = SU.LoadCSV("", FileName, ",")[/B]
        For i = 0 To lst1.Size - 1
            Dim sColumn() As String
            sColumn = lst1.Get(i)
            If CGlobals.CoordCode=1 Then
                'Site Coords
                CGlobals.SQL1.ExecNonQuery2("INSERT INTO SCoords VALUES (?, ?, ?, ?, ?)", sColumn)
            else if CGlobals.CoordCode=2 Then
                'Global Coords
                CGlobals.SQL1.ExecNonQuery2("INSERT INTO GCoords VALUES (?, ?, ?, ?, ?)", sColumn)
            Else
                'Job Coords
                CGlobals.SQL1.ExecNonQuery2("INSERT INTO Coords VALUES (?, ?, ?, ?, ?)", sColumn)
            End If
        Next
        CGlobals.SQL1.TransactionSuccessful
    Catch
        Log(LastException.Message)
    End Try

]

Now it works!!
 
Upvote 0
Top