A CSV file is just a text file with commas delimiting the data elements
so you can TextInputStream. Below is an example of putting a CSV file
into a listbox. You can just as easily read a line and then insert it
into your REALdb but to start off with it may be wise to import the data
into a listbox to insure you have read your CSV file correctly before
inserting the data into the database.
Here is a PushButton Action to launch a method called ImportList which
will import a CSV file into a ListBox.
Sub Action()
Dim dlg as OpenDialog
Dim f as FolderItem
Dim fName,pName As String
dlg=New OpenDialog
dlg.InitialDirectory = Volume(0).Child("Desktop")
dlg.Title = "Select a CSV text file"
dlg.Filter = "Text Documents (*.txt)" // file type defined in File
Types dialog
f=dlg.ShowModal()
If f <> Nil then
fName = f.Name
pName = f.AbsolutePath
ImportList(fName,pName,Window1.ListBox1)
Else
//User Cancelled
End if
End Sub
Here is the Import List method that also strips off enclosing quotes. If
your CSV file doesn't have enclosing quotes you can eliminate that
section.
Sub ImportList(FileName As String,FilePath As String,List As ListBox)
Dim f as FolderItem
Dim TextIn As TextInputStream
Dim count,i,n,r, x As Integer
Dim line,c,s As String
count = List.ColumnCount
List.DeleteAllRows
f = GetFolderItem(FilePath)
If f <> Nil then // check to make sure the file exists
TextIn = f.OpenAsTextFile
If TextIn <> NIL Then
Do
// read a line
line = TextIn.ReadLine
// add a row in the listbox
List.AddRow ""
For i = 1 To count
// read the data
c = NthField(line,Chr(44),i)
r = List.LastIndex
// strip off the enclosing quotes
x = Len(c)
s = Mid(c,2,(x-2))
// put the data in the cell
List.Cell(r,(i-1)) = s
Next
Loop Until TextIn.EOF = True
End If
Else
// error message
MsgBox "Error Reading input file: " + FileName
End If
End Sub
You then will need to to insert your data into the correct table(s) from
the listbox. If you need help with that email me off list. I am in and
out today so no guarantee ho fast I will get bac to you.
Tom
-----Original Message-----
From: gettingstarted-bounces at lists dot realsoftware dot com
[mailto:gettingstarted-bounces at lists dot realsoftware dot com] On Behalf Of
Trev
X
Sent: Saturday, April 30, 2005 2:11 PM
To: gettingstarted at lists dot realsoftware dot com
Subject: Help with parsing CSV
I have a large amount of information stored in a .csv file. I need to
be able to move the information from that .csv file to my realdatabase
in my program. Several times a week I will have an updated csv file,
and will need to open it and write over the old information.
I realize there is Seth's csv parser and Norman's parser, but I'm a
newbie and don't know how to implement this on a practical level. Can
someone please give me some sample code or general ideas on how to open
up the csv file and then import all the data into a realdb?
Thanks for your help!
_________________________________________________________________
On the road to retirement? Check out MSN Life Events for advice on how
to get there! http://lifeevents.msn.com/category.aspx?cid=Retirement
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>
|