Hot to create a CSV file for a Table

fdx12345

Active Member
Licensed User
Longtime User
The Table class has a saveTabletocsv sub but it requires the csv file to already exist. How can I initially save the file or, check to see if file exist and if it does not, then create the file.?
 

boastrike

Member
Licensed User
Longtime User
Here is a snippet of some code I have that writes the CSV file if that helps. Boa

B4X:
Sub WriteCSVfile(WriteFolder As String)
   
   Dim CSVlist As List
   CSVlist.Initialize
   For I = 0 To lstorders.Size - 1
      Dim row(4) As String
      Dim t As orderline
      t = lstorders.Get(I)
      row(0) = t.Item
      row(1) = t.qty
      row(2) = t.desc
      row(3) = t.note
      If gbsubmissionfile = True Then
         If manager.GetBoolean("PRincludedesc") = True AND manager.GetBoolean("PRnotes") = True Then
            CSVlist.Add(Array As String(row(0), row(1), row(2), row(3)))
         Else
            If manager.GetBoolean("PRincludedesc") Then
               CSVlist.Add(Array As String(row(0), row(1), row(2)))
            Else
               If manager.GetBoolean("PRnotes") Then
                  CSVlist.Add(Array As String(row(0), row(1), row(3)))
               Else
                  CSVlist.Add(Array As String(row(0), row(1)))
               End If
            End If
         End If
      Else
         CSVlist.Add(Array As String(row(0), row(1), row(2), row(3)))
      End If
   Next
   Dim su As StringUtils
   Log("save dir: " & File.DirInternal & " file: " & filename)
   su.SaveCSV(WriteFolder, filename, ",", CSVlist)
   gbsubmissionfile = False

End Sub
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
The Table class has a saveTabletocsv sub but it requires the csv file to already exist. How can I initially save the file or, check to see if file exist and if it does not, then create the file.?

The easiest and fastest way i use is to simply create an initial csv file on the computer and add it to the files list. When the app is compiled and loaded the csv file is copied across

hope this helps
 
Upvote 0

fdx12345

Active Member
Licensed User
Longtime User
Here is the error I am getting

Here is the error:

An error has occurred in sub:table_savetabletocsv (B4A line: 358)

StringUtils1.SaveCSV2(Dir, Filename, ",", Data, headers)

Java.io.FileNotFoundException: /AssetsDir/TestProlect.csv (No such file or directory)

Continue?

end of error message

I have tried placing a *.csv file from another example project, but the program does not find that one either.
 
Upvote 0

fdx12345

Active Member
Licensed User
Longtime User
The "Files" folder is read only??

If I understand the documentation, the "Files" folder is the same thing as the DirAssets folder. If that is the case, I believe all files in that folder is read only.

I have saved a *.csv file (actually the listcity.csv file) to the "Files" folder before compiling the program. When I try to write to that folder, I get the above error.

I believe I need to create my own directory to use for files. If I do that, I need to initially copy a CSV (or create a new one) to my new directory. I have seen this done with other applications but not sure how to do that with B4 and android as what does one call the root. On a PC it is simply "X:" where X is the drive of interest.

At this point, I really need to understand why the program cannot find the test file (current error - see above) then how can I create a new dirctory and move a dummy file to that directory taking in consideration that file has to be initially loaded with the application.
 
Upvote 0

fdx12345

Active Member
Licensed User
Longtime User
Found how to add file but still same error

I just found out how to add a file via the file tab in the IDE but the program can not find the file.


Actual command I am using is:

Table1.SaveTableToCSV(File.DirAssets, "MyFile.csv") were MyFile.csv was loaded via the IDE before compiling. I am still getting can't find the file error (see listing above).

Any thoughts how to correct the problem?
 
Upvote 0

fdx12345

Active Member
Licensed User
Longtime User
Found the answer

Discoverd a posting stating writing to the DirAssets is not allowed and would give the above error. I was expecting the error but did not expect it to show up as not found. The same posting discussed how to create a new directory.
 
Upvote 0

Devan

Member
Licensed User
Longtime User
Hi fdx12345,
Can you pls share the posting. im trying to create new csv file.
tq
 
Upvote 0
Top