global variable in MySQL - mysql

I dont think it is possible but there is no harm to ask.
I have this sql file :
LOAD DATA INFILE 'myfile.csv'
INTO TABLE TCONTACTN4DS
FIELDS TERMINATED BY ";"
LINES TERMINATED BY "\r\n"
if there is no path in the INFILE option, it goest to the data/name_of_the_DB folder to get the file.
I could put any path in the infile but let's assume that I dont know the path that will be used and I dont want those scripts to be changed.
Can I declare (dont know where exactly) a global variable that could be changed to describe the path ? How would I use it if it is possible ?

The mysql documentation on load data infile is very explicit about this, so no, you cannot do this:
The server uses the following rules to locate the file:
If the file name is an absolute path name, the server uses it as given.
If the file name is a relative path name with one or more leading components, the server searches for the file relative to the server's
data directory.
If a file name with no leading components is given, the server looks for the file in the database directory of the default database.
Since load data cannot be used in stored routines either, you cannot even have a workaround for this limitation.

Related

MySQL : --secure-file-priv

I am learcing SQL and am trying to load a .csv file into it with "load infile" etc...
However I am getting the error, "The MySQL server is running with the --secure-file-priv so it cannot execute...".
I checked SHOW VARIABLES LIKE 'secure_file_priv' and it showed that the variable only allows loading data from C:\ProgramData\MySQL\MySQL Server 5.7\Uploads\
However after moving my data to that folder the same error keeps coming up. Can anybody help? Thanks. Andreas
I had the same problem when loading csv-files. As in https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_secure_file_priv described, you can only read and write in the path in the variable secure-file-priv.
Therefore you have to include the full path in the query. Since you did not post a concrete query, I can only guess that you tried something like LOAD DATA infile 'temp_0.csv' INTO TABLE series_data_in;. It should work with something like LOAD DATA infile '/var/lib/mysql-files/temp_0.csv' INTO TABLE series_data_in; (or in your case LOAD DATA infile 'C:/ProgramData/MySQL/MySQL\ Server\ 5.7/Uploads/temp_0.csv' INTO TABLE series_data_in ; - be aware of correctly escaped spaces).

How to use ftp file path for SQL loader?

I could not use ftp file path in SQL loader. Help to solve this problem.
LOAD DATA INFILE 'ftp file path'
TRUNCATE INTO TABLE temp
FIELDS TERMINATED BY ',' TRAILING NULLCOLS "(col1,col2)";
The string following the INFILE parameter needs to be the name of a file containing the data. See the documentation here: http://docs.oracle.com/cd/B28359_01/server.111/b28319/ldr_control_file.htm#i1008015
If you are trying to make it a dynamic path to a file, you need to specify it on the command line when you call sql-Loader. In that case see the DATA parameter here: http://docs.oracle.com/cd/B28359_01/server.111/b28319/ldr_params.htm#i1004682

MySQL LOAD DATA Error (Errcode: 2 - "No such file or directory")

I am trying to load data into a table of my MySQL database, and getting this error.
LOAD DATA LOCAL INFILE 'C:\Users\Myself\Desktop\Blah Blah\LOAD DATA\week.txt'
INTO TABLE week;
Reference: this
The path is hundred percent correct, I copied it by pressing shift and clicking "copy path as" and checked it many times. So any tips on this will be much appreciated.
.
My research: Seeing this answer, I tried by changing C:\Users to C:\\Users. It did not work for me.
Secondly, is there a way to use some kind of a relative path (rather than an absolute path) here?
I spent 2 days on this and finally got my mistake, Just changing backslashes by forward ones, as one contributor previously said. And finally worked for me.
so was:
LOAD DATA LOCAL INFILE 'C:/ProgramData/MySQL/MySQL Server 5.7/Data/menagerie/pet.txt' INTO TABLE pet;
I just can say thanks a lot.
p.s. don't waste time on ytb...
I don't know what version of MySQL you are using but a quick Google search found possible answers to both your questions. Below are excerpts from the MySQL 5.1 Reference Manual:
The file name must be given as a literal string. On Windows, specify
backslashes in path names as forward slashes or doubled backslashes
The LOCAL keyword affects where the file is expected to be found:
If LOCAL is specified, the file is read by the client program on the
client host and sent to the server. The file can be given as a full
path name to specify its exact location. If given as a relative path
name, the name is interpreted relative to the directory in which the
client program was started.
Regards.
If using MySQL Workbench on a local Windows PC to connect to a remote MySQL server,
Add the "LOCAL" keyword
Add double backslashes "\\" to your folder path
If text file's first row has column names add "IGNORE 1 LINES".
LOAD DATA LOCAL INFILE 'C:\\MyTabDelimited.txt'
INTO TABLE my_table IGNORE 1 LINES;
Simply replace backslash with slash in the path.
This works for me (MySQL Workbench 6.3 on Win 10):
LOAD DATA LOCAL INFILE 'C:/Users/Myself/Desktop/Blah Blah/LOAD DATA/week.txt'
INTO TABLE week;
Ref. https://dev.mysql.com/doc/refman/5.5/en/loading-tables.html
One more reason for this type of error is another languge in the path.
You might have almost the entire path in English, but the username might be auto-filled in another language.
Try removing the word LOCAL from your query.
Try moving the week.txt file to the desktop
then execute in a terminal window:
LOAD DATA LOCAL INFILE 'C:\Users\Myself\Desktop\week.txt'
INTO TABLE week;
Instead of using double backslash That slash is also worked for me too.
I resolve this problem by replacing the path
Replace format "C:\Users\Myself\Desktop\week.txt"
With this different format "C:/Users/Myself/Desktop/week.txt"
My computer didnt recognize the ( \ ) symbols.

can LOAD DATA INFILE use a giving path instead of the default mysql data path?

I am trying to use LOAD DATA INFILE to upload xls/CSVfile content into mysql database.
the file in which I want to upload is located on a different path that the databases. I don't want to put the xls/CSV file in my same path as my data so I put them into a different path. but it seems that MySQL ignore the path I am providing It always searching for a file located in the data bases directory.
here is my code
LOAD DATA INFILE 'D:\uploads\new_campaign_request.xls'
INTO TABLE new_db.new_campaign
FIELDS TERMINATED BY ',';
How can I get MySQL to search for the file
LOAD DATA INFILE 'D:\\uploads\\new_campaign_request.xls'
INTO TABLE new_db.new_campaign(column_name)
FIELDS TERMINATED BY ',';
User double back slash for load file path and define column name in which data should be loaded

MySQL Load Data Local Infile - Path as User Variable

I would like to include a user variable in the MySQL `LOAD DATA LOCAL INFILE' file path. Our small team often accesses raw data files from Dropbox, so everyone has to find/replace the user name in the path definitions.
I was expecting the following to work but keep getting syntax errors:
SET #USER := 'user';
SET sql_mode='PIPES_AS_CONCAT';
LOAD DATA LOCAL
INFILE '/Users/' || #USER || '/Dropbox/Data/data.csv'
Also fails with CONCAT(). Any ideas? Thanks!
Similar questions asked here: Load data Infile #variable into infile error but without accepted answer.
A citation from MySQL documentation:
The file name must be given as a literal string. On Windows, specify backslashes in path names as forward slashes or doubled backslashes. The character_set_filesystem system variable controls the interpretation of the file name.
That means that it can not be a parameter of a prepared statement, stored procedure, or anything "server-side". The string/path evaluation must be done client side.