Read in delimited file to list
I want to read in a list of information from a specified url that will eventually be put into a database. I want to use a 2d array to simulate the rows and columns of the database. How would I accomplish this with cold fusion?
example file:
blah|stuff|123123
wu开发者_StackOverflow社区rd|noise|123121
There are a number of options depending on the application. Some examples are:
1) Treat the content as a series of lists, with rows delimited by new lines and columns by a "|". Then loop through the lists and append the data to an array or insert it into a database.
...
<cfset rows = []>
<cfloop list="#content#" delimiters="#chr(13)##chr(10)#" index="data">
<cfset arrayAppend(rows, listToArray(data, "|", true))>
</cfloop>
2) Another option is cfhttp, which can convert well formed files directly into a query object. It has some limitations, but works in many cases.
<cfhttp url="http://www.somesite.com/pageThatGeneratesFile.cfm"
firstRowAsHeaders="false"
delimiter="|"
name="queryResult" />
3) Most databases provide tools for importing text files. For example MS SQL provides BULK INSERT
BULK INSERT TableName
FROM 'c:\yourFile.txt'
WITH
(
FIELDTERMINATOR ='|',
ROWTERMINATOR = '\n'
)
As logical (4) for Leigh's answer: load data in MySQL.
LOAD DATA LOCAL INFILE 'c:\youfile.txt'
INTO TABLE TableName
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\n'
(col1, col2, col3)
More options in manual.
精彩评论