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

  • Method 1:
  • 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"));) {
    
    		-> "[A]" (atom get [line] 1 ",");
    		-> "[B]" (atom get [line] 2 ",");
    		-> "[C]" (atom get [line] 3 ",");
    
    		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]";
  • Method 2:
  • 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"));) {
    
    		-> "[A]" (string substring [line] 0 1);
    		-> "[B]" (string substring [line] 4 9);
    		-> "[C]" (string substring [line] 10 15);
    		
    		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