Innov-AI
    Documentation | MentDB Weak Server
Parse a no-regular text file and integrate row into a SQL Database

  • try {
    
    	sql connect "session1" {cm get "demo_cm_mysql";};
    	
    	#Open a reader;
    	file reader_open "r1" "data/file.txt" TEXT "utf-8";
    
    	#Parse the file;
    	while (is not null (-> "[line]" (file reader_get_line "r1"));) {
    
    		switch (string substring [line] 0 1)
    			("X") {
    				-> "[A]" (string substring [line] 0 1);
    				-> "[B]" (string substring [line] 4 9);
    				-> "[C]" (string substring [line] 10 15);
    			}
    			("X") {
    				-> "[A]" (string substring [line] 0 1);
    				-> "[B]" (string substring [line] 8 12);
    				-> "[C]" (string substring [line] 13 20);
    			}
    			{
    				-> "[A]" (string substring [line] 0 1);
    				-> "[B]" (string substring [line] 8 10);
    				-> "[C]" (string substring [line] 14 17);
    			}
    		;
    		
    		sql dml "session1" (concat "insert into products (id, name, quantity) values (" (sql encode [A]) ", " (sql encode [B]) ", " (sql encode [C]) ");");
    
    	};
    
    	#Close the reader;
    	file reader_close "r1";
    
    	sql disconnect "session1";
    
    } {
    
    	try {file reader_close "r1";} {} "[sub_err]";
    	try {sql disconnect "session1";} {} "[err]";
    
    } "[err]";
Services     Supporters     Contact     About     Legal notice © 2024