LOADCSV Problem

peggjones

Active Member
Licensed User
Longtime User
Are there any known bugs glitches etc. with Loadcsv?

I am having the following issue;

I download a csv file and copy it from the downloads folder to sdcard0.

When I try to run loadcsv it falls over with "StringIndexOutofBounds".


If I process exactly the same file from my PC with loadcsv on my nexus it works perfectly.

Is it something in the copying process that is causing the problem?

I've copied using ASTRO and ES file explorer.

Many Thanks.
 

Geezer

Active Member
Licensed User
Longtime User
LoadCSV will cause index out of bounds if any of the fields are empty, especially the last one.

For example, you have 4 fields and data as below

1,2,3,4
1,a,b,c
a,b,c
1,2,3,4

the 3rd line will cause the error because there is no last field

If I remember correctly, this line would also cause an error

1,,3,4

but it's been a long time since I tried that.
 
Upvote 0

peggjones

Active Member
Licensed User
Longtime User
Virtual Portfolio, , , ,
Client Name:,Ms Paul smith, , ,
Client Number:,447444, , ,
Valuation as at,28-04-2013 10:25, , ,

the above data causes a problem.

The strange thing is if I open the file with any other software (simple spreadsheet or kingsoft office)it works ok.
 
Upvote 0

peggjones

Active Member
Licensed User
Longtime User
A bit more info

The line of code in mu app which doesn't work is -

Table = su.LoadCSV(File.Dirrootexternal, "portfolio-summary.csv", ",")

Note that I have copied the file from the download folder on my nexus to dirrootexternal.

The following line of code does work. It is effectively reading a file that I downloaded to my PC.

Table = su.LoadCSV(File.DirAssets, "portfolio-summary.csv", ",")

Thanks
 
Upvote 0

Geezer

Active Member
Licensed User
Longtime User
Virtual Portfolio, , , ,
Client Name:,Ms Paul smith, , ,
Client Number:,447444, , ,
Valuation as at,28-04-2013 10:25, , ,

the above data causes a problem.

The strange thing is if I open the file with any other software (simple spreadsheet or kingsoft office)it works ok.

The data you are importing ends with "," which means the last field is empty, which causes the error.
 
Upvote 0

peggjones

Active Member
Licensed User
Longtime User
I've tried running the app with the data ending with 1,2,3 and no commas and it still doesn't work.

Many Thanks.
 
Upvote 0

peggjones

Active Member
Licensed User
Longtime User
Any other solution

The file that I download is from Hargreaves Lansdown a mainstream supplier of unit trust prices. All the other software that I use has no trouble dealing with the file. Does anyone know how I can get the file into my app so I can load it to a database?

Thanks to all.
 
Upvote 0

Geezer

Active Member
Licensed User
Longtime User
Post your full routine that you are using to read the file, and also a zip of the csv file and i'll take a look in the morning.
 
Upvote 0

peggjones

Active Member
Licensed User
Longtime User
I'm running the following code. It still doesn't work I get

StringIndexOutofboundsException:length=124;regionstart=121;regionlength=122

Sub Button1_Click
Dim su As StringUtils
Dim Table As List
Dim SQL As SQL
Dim never As String
Dim sql1 As SQL
Dim txt As String
Dim found As Boolean

' Initialize database.

SQL.Initialize(File.DirRootExternal, "AssetsManager.db", True)

' Initialize table.

Table.Initialize
now = DateTime.now
Dim DateNow, TimeNow As EditText
DateNow.initialize(0)
DateNow.Text= DateTime.Date(DateTime.Now)

' Work will be used to create unique primary key.

work = now

' only run this bit when resetting database.

If never = "x" Then

DBUtils.DropTable(SQL, "prices")


Dim m As Map
m.Initialize
m.Put("Code",DBUtils.DB_TEXT)
m.Put("Description", DBUtils.DB_TEXT)
m.Put("Units", DBUtils.DB_INTEGER)
m.Put("Price", DBUtils.DB_INTEGER)
m.Put("Value", DBUtils.DB_INTEGER)
m.Put("Date", DBUtils.DB_INTEGER)
m.Put("Pkey", DBUtils.DB_integer)

DBUtils.CreateTable(SQL, "prices", m, "pkey")

End If


Log(File.Dirrootexternal)

' Load Prices file into accessible file. CSV file must be in the same location as the database.


' If I run this routine it doesn't work.
Table = su.LoadCSV(File.Dirrootexternal, "portfolio-summary.csv", ",")
' If I run this routine it does work.
' Table = su.LoadCSV(File.DirAssets, "portfolio-summary.csv", ",")

key = now

' Move the data into a map.

Dim Table2 As List
Dim Items() As String
Table2.Initialize
For i = 5 To 13
Items = Table.Get(i)
Dim m As Map
m.Initialize
m.Put("Code", Items(0))
m.Put("Description", Items(1))
m.Put("Units", Items(2))
m.Put("Price", Items(3))
m.Put("Value", Items(4))
m.put("Date", DateNow.text)
work = work + 1
m.Put("pkey", work)
Table2.Add(m)
Next

' Write map to database.

DBUtils.InsertMaps(SQL, "prices", Table2)

End Sub
 
Upvote 0

peggjones

Active Member
Licensed User
Longtime User
Solved

I downloaded a new set of prices this morning and it now runs OK.

There must have been something in yesterday's download that LOADCSV didn't like!

What remains a mystery is why another apps like "Simple Spreadsheet" did not have a problem reading the file.

Many thanks for all the help.
 
Upvote 0
Top