field = value
bcusick wrote:
You can ALWAYS "import" data into servoy without using the import dialog. What you do is:
1) Locate file on disk (prompting the user - or using a pre-defined location);
2) Read the file into a variable;
3) Parse the variable and put each of the pieces of data into the correct field using
- Code: Select all
field = value
Just curious - why are you importing through the Servoy interface on a regular basis (i.e. why do you need to store the import orders)?
//Show a file open dialog and retrieve file name
var filename = application.showFileOpenDialog();
//Read binary file, shows file selectdialog and reads all data
var rawData = application.readFile(filename);
//count lines
var max = utils.stringPatternCount(rawData, '\n')
//loop and parse
for ( var i = 1 ; i < max ; i++ )
{
//get the first line up to the carriage return
//do another for loop here to loop through the columns
for ( var x = 0 ; x < numColumns ; i++ )
{
//set temp to first column
var temp = (PARSE CODE HERE)
//if x = 1 - set a field
if(x == 1)
{
field1 = temp
} else if(x ==2) {
field2 = temp
}
}
}
antonio wrote:Coming back to this, could there be (or is there) a utils.stringMiddleLine (like MiddleWords, LeftWords etc, but it returns an entire line, similar to the FMP function MiddleValues.
getSQLImportString (String sqlStatement, String tableName, String[] fieldNames, String filePath, String fileDelimiter, int isFirstLineData)
//get the user to select the file
var myFile = plugins.file.showFileOpenDialog(1);
//specify the order of the column names
var columnNames = new Array("field1","field2","field3","field4");
//get the SQL statement to run
var sqlString = plugins.DDC_Utils.getSQLImportString("INSERT", controller.getTableName(), columnNames, myFile.getAbsolutePath(), "\\t", 1);"
//run the SQL statement
var exec = plugins.rawSQL.executeSQL(controller.getServerName(), controller.getTableName(), sqlString)
//flush the client's cache
plugins.rawSQL.flushAllClientsCache(controller.getServerName(), controller.getTableName())
//show the status of the import after completion
plugins.dialogs.showInfoDialog( "", "Import Status: " + exec, "ok")
public String js_getSQLImportString (String sqlStatement, String tableName, String[] fieldNames, String filePath, String fileDelimiter, int isFirstLineData)
{
String myReturn = "";
try{
BufferedReader br = new BufferedReader(new FileReader(filePath));
String s;
String sql = sqlStatement + " INTO " + tableName + " (";
for(int i = 0; i< fieldNames.length; i++){
sql += fieldNames[i] + ",";
}
sql = sql.substring(0, sql.length()-1); //remove last ,
sql += ") VALUES ";
int j = 0;
while ((s=br.readLine())!=null){
j++;
if(j>1){
String line = s.replaceAll(fileDelimiter,"','");
sql += "('" + line + "'),";
}
}
br.close();
sql = sql.substring(0, sql.length()-1); //remove last ,
sql += ";";
myReturn = sql;
}catch(Exception e){
myReturn = "false: " + e;
}
return myReturn;
}
application.executeProgram('split','-b','1m','-a','3',data_file_path,'temp_');
plugins.DDC_Utils.getSQLImportString(sqlStatement, tableName, String[] fieldNames, filePath, fileDelimiter, number isFirstLineData)
Return to Discuss Feature Requests
Users browsing this forum: No registered users and 7 guests