3

Below is my code code and is wrong for MySQL. @Path not be allowed variable with syntax "LOAD DATA INFILE" and I don't know exactly path + filename.

Do you have any idea how to solve this issue?

SET @Path = CONCAT(sysf_Get_Options('DRIVE'),sysf_Get_Options('PATH_SAP_FILE'),'277.txt');

LOAD DATA INFILE @Path
  INTO TABLE SAP_TMP 
  LINES TERMINATED BY '\r\n'
  (@var1)
  SET txt = SP_INSERT_ROW_CONTRACTS(@var1)
;

3 Answers 3

5

http://dev.mysql.com/doc/refman/5.6/en/load-data.html says:

The file name must be given as a literal string.

So you can't use a variable for the path. You have to code a literal, quoted string into the statement before it is parsed.

Sign up to request clarification or add additional context in comments.

Comments

1

The workaround we used was to copy the source file into a temporary file and to reference the temporary file in the INFILE line. Something like: (for windows, for Linux it's not so different)

copy /Y C:/path_to_file/277.txt C:/temp/tempData.txt

and then

LOAD DATA INFILE 'C:/temp/tempData.txt'

That way you can hardcode the path in the sql script.

Comments

0

you can use this method or +

var Upload = function (file) {
  this.file = file;
};

$("#csvfile").on("change", function (e) {
  var file = $(this)[0].files[0];
  var upload = new Upload(file);
  upload.doUpload();
});


Upload.prototype.doUpload = function () {

  // Retrieve the FileList object from the referenced element ID
  var myFileList = document.getElementById('csvfile').files;

  // Grab the first File Object from the FileList
  var myFile = myFileList[0];

  // Set some variables containing the three attributes of the file
  var myFileName = myFile.name;
  var myFileSize = myFile.size;
  var myFileType = myFile.type;

  // Alert the information we just gathered
  alert("FileName: " + myFileName + "- FileSize: " + myFileSize + " - FileType: " + myFileType);

  // Let's upload the complete file object
  // Open Our formData Object
  var formData = new FormData();

  // Append our file to the formData object
  // Notice the first argument "file" and keep it in mind
  formData.append('my_uploaded_file', myFile);

  // Create our XMLHttpRequest Object
  var xhr = new XMLHttpRequest();

  // Open our connection using the POST method
  xhr.open("POST", 'uploadcsv.php');

  // Send the file
  xhr.send(formData);


  var fileFullpath="'C:\\\\"+ myFileName + "'";
  var query = "LOAD DATA LOW_PRIORITY LOCAL INFILE " + fileFullpath + " INTO TABLE tb_Name CHARACTER SET cp1256 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY ',' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (Field1, Field2, Field3, Field4);";

			
  //INSERT file into DB
  $.ajax({
    type:'POST',
    url:'insertData.php',
    async:false, //to make sure all data inserted
    data:{'data':query},
    success: function(data){
      if(data){
        $("#status").append("<br />Data added successfuly");
      }else{
        $("#status").append("<br />Something went wrong, can't edit the row. ");
      }
    }
  });
			
			
			
  //Then delete uploaded file after INSERT
  $.ajax({
    type: "POST",
    url: 'deletecsv.php',
    data: {'file' : myFileName },
    success: function (response) {
      alert("deleted");
    },
    error: function () {
      alert("can't delete file csv");
    }
  });
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form method="post" id="fileinfo" name="fileinfo" onsubmit="return submitForm();"> 
  Upload CSV file :
  <input type="file" class="form-control" name="csvfile" id="csvfile" />
</form>
<div id="status"></div>

and the files: uploadcsv.php

//uploadcsv
$fileName = $_FILES['my_uploaded_file']['name'];
$fileType = $_FILES['my_uploaded_file']['type'];
$fileTemppath = $_FILES['my_uploaded_file']['tmp_name'];
$fileError = $_FILES['my_uploaded_file']['error'];
//$fileContent = file_get_contents($_FILES['file']['tmp_name']);
//print_r ($fileContent);

if($fileError == UPLOAD_ERR_OK){
    //Processes your file here
    move_uploaded_file($fileTemppath, "C:/" . $fileName);
}else{
   switch($fileError){
     case UPLOAD_ERR_INI_SIZE:   
          $message = 'Error uploading file that exceeds allowed size.';
          break;
     case UPLOAD_ERR_FORM_SIZE:  
          $message = 'Error uploading file that exceeds allowed size.';
          break;
     case UPLOAD_ERR_PARTIAL:    
          $message = 'Error: do not finish? The action of uploading the file.';
          break;
     case UPLOAD_ERR_NO_FILE:    
          $message = 'Error: No file was uploaded.';
          break;
     case UPLOAD_ERR_NO_TMP_DIR: 
          $message = 'Error: Server not configured for file upload.';
          break;
     case UPLOAD_ERR_CANT_WRITE: 
          $message= 'Error: Possible failure to save file.';
          break;
     case  UPLOAD_ERR_EXTENSION: 
          $message = 'Error: File upload not completed.';
          break;
     default: $message = 'Error: File upload not completed.';
              break;
    }
      echo json_encode(array(
               'error' => true,
               'message' => $message
            ));
}

insertData.php:

//insertData
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "databasename";
    @mysql_connect($servername,$username,$password) or die ("error in host connection");
    @mysql_select_db($dbname) or die("error in db connection");

$thedata = $_POST['data'];

$result=mysql_query($thedata);
if(isset($result)) {echo "YES";} else {echo "NO";}

and last file will be deletecsv.php

//deletecsv
$fileName = $_POST['file'];
unlink("C:/" . $fileName);

Hope it help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.