Importing Data

Discuss all feature requests you have for a new Servoy versions here. Make sure to be clear about what you want, provide an example and indicate how important the feature is for you

Importing Data

Postby rochard » Wed Jun 02, 2004 6:06 pm

Is there any way that you could store the import order so that new imports can be done without having to line up all the columns. For me, at least, it would be a tremendous time saver.
Michael Rochard
rochard
 
Posts: 142
Joined: Thu Jan 29, 2004 12:11 am

Postby bcusick » Fri Jun 04, 2004 10:59 am

Hi Michael,

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)?

Bob Cusick
bcusick
 
Posts: 1255
Joined: Wed Apr 23, 2003 11:27 pm
Location: Thousand Oaks, CA USA

Importing Data

Postby rochard » Fri Jun 04, 2004 4:54 pm

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;


Sorry but I don't understand how you do this! Can you please explain this process in more detail?

3) Parse the variable and put each of the pieces of data into the correct field using
Code: Select all
field = value


How do you parse the variable?

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)?



Two reasons; firstly for transferring a lot of records from an existing FM database and having to go through much trial and error to get it all right; secondly to bring in a data file collected from an external source.
Michael Rochard
rochard
 
Posts: 142
Joined: Thu Jan 29, 2004 12:11 am

Postby bcusick » Fri Jun 04, 2004 8:01 pm

Michael,

Unfortunately, I don't have the time to write a fully functional example - but hopefully the below will point you in the right direction:
Code: Select all
//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
      }
   }
}


You would parse text string just like you do now in FileMaker.

Bob
bcusick
 
Posts: 1255
Joined: Wed Apr 23, 2003 11:27 pm
Location: Thousand Oaks, CA USA

Importing Data

Postby rochard » Sat Jun 05, 2004 12:44 am

Many thanks, Bob. That's a great help.
Michael Rochard
rochard
 
Posts: 142
Joined: Thu Jan 29, 2004 12:11 am

Postby Harjo » Sat Jun 05, 2004 11:54 am

Hi All,
I still prefer a way of doing this thru the import of Servoy itself.
The dev-guys have improved the import-speed.

The solution as described above is an option, but not a very fast one!.
And, if I am correct, this feauture is planned for the 2.5 release.
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands

Postby antonio » Mon Jun 26, 2006 2:57 am

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.
Tony
Servoy 8 - 2022.03 LTS
antonio
 
Posts: 638
Joined: Sun Apr 02, 2006 2:14 am
Location: Australia

Postby ROCLASI » Mon Jun 26, 2006 8:22 am

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.

There is no MiddleLine function in Servoy. But you could make your own global method for this and call that where you need it.
Or even stick it in a (core) module and load that in all your projects you need so you can reuse it.

Hope this helps.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Postby antonio » Mon Jun 26, 2006 8:33 am

Thanks, I'll do just that.

It does seem like a useful function to have in the utils.

Tony
Tony
Servoy 8 - 2022.03 LTS
antonio
 
Posts: 638
Joined: Sun Apr 02, 2006 2:14 am
Location: Australia

Postby sbutler » Fri Jun 30, 2006 10:48 pm

FYI..just read this. I have a funciton in a free plugin which allows you to specify a file, table and field names, and it returns a SQL import statment, which can run with the Raw SQL plugin...if your interested. You can see it at http://it.dnacenter.com/servoy/plugin_ddc_utils.html It basically does what Bob described above, but inside of a plugin.

THe functions is...
Code: Select all
getSQLImportString (String sqlStatement, String tableName, String[] fieldNames, String filePath, String fileDelimiter, int isFirstLineData)


An import script would look like..

Code: Select all
     
     //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")


This is the actual Java code behind the plugin...nothing to exciting. If you are using MySQL, you have the option of calling and INSERT, or REPLACE. A replace is like the FileMaker equivalent of updating matching records, and creating non-matching records.

Code: Select all
     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;
     }
Scott Butler
iTech Professionals, Inc.
SAN Partner

Servoy Consulting & Development
Servoy University- Training Videos
Servoy Components- Plugins, Beans, and Web Components
Servoy Guy- Tips & Resources
ServoyForge- Open Source Components
User avatar
sbutler
Servoy Expert
 
Posts: 759
Joined: Sun Jan 08, 2006 7:15 am
Location: Cincinnati, OH

Postby Harjo » Sat Jul 01, 2006 11:52 am

cool! did you do allready a performance test? servoy import and your import? is it faster?

Your link does'nt seem to work here.
Harjo Kompagnie
ServoyCamp
Servoy Certified Developer
Servoy Valued Professional
SAN Developer
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands

Postby sbutler » Sat Jul 01, 2006 4:59 pm

The subdomain "it" is pretty new, so the link may not work everywhere yet.

I didn't do any speed test again's Servoy's import, but for what we we're using it for, it was pretty fast. One of the main differences vs bob's code is that it does not set the fields through servoy...instead it returns back a SQL string to be run through the raw SQL plugin...so I would think it would be a bit faster. The only downside is that you have to flush the client's cache since the inserts are done in the raw SQL plugin.

Attached is the plugin..
Attachments
ddc_utils.jar
DDC Utils plugin
(8.36 KiB) Downloaded 352 times
Scott Butler
iTech Professionals, Inc.
SAN Partner

Servoy Consulting & Development
Servoy University- Training Videos
Servoy Components- Plugins, Beans, and Web Components
Servoy Guy- Tips & Resources
ServoyForge- Open Source Components
User avatar
sbutler
Servoy Expert
 
Posts: 759
Joined: Sun Jan 08, 2006 7:15 am
Location: Cincinnati, OH

Postby swingman » Sun Jul 02, 2006 10:26 am

Hi,
a thought/tip.

One problem I have come across when importing using my own import logic is that Servoy does not like to read very large text files. I had problems with a 22MB merge file from FileMaker. If you are on Mac OS or Linux, you can split the file into 1MB chunks using the following:

Code: Select all
application.executeProgram('split','-b','1m','-a','3',data_file_path,'temp_');


This command is very fast and creates a bunch of files named temp_aaa, temp_aab etc

This does not split by lines, so you need to keep the last line of each chunk and attach it to the front of the next chunk before processing the data with the rawSQL plugin.
Christian Batchelor
Certified Servoy Developer
Batchelor Associates Ltd, London, UK
http://www.batchelorassociates.co.uk

http://www.postgresql.org - The world's most advanced open source database.
User avatar
swingman
 
Posts: 1472
Joined: Wed Oct 01, 2003 10:20 am
Location: London

Postby rafig » Tue Mar 20, 2007 8:05 pm

Just trying out the
Code: Select all
plugins.DDC_Utils.getSQLImportString(sqlStatement, tableName, String[] fieldNames, filePath, fileDelimiter, number isFirstLineData)

in a method (to try and automate imports) and there seems to be an issue with the last parameter, isFirstLineData.
I assume the options are
0 - for no
1 - for yes
Whatever I set it to, it ignores the first line. So if I only have one record/line to import, it doesn't work. And if I have more, but no header line, it misses the first record/line.

Thanks,
Rafi
rafig
 
Posts: 708
Joined: Mon Dec 22, 2003 12:58 pm
Location: Watford, UK


Return to Discuss Feature Requests

Who is online

Users browsing this forum: No registered users and 11 guests