MySQL “LOAD LOCAL DATA INFILE”

蓝咒 提交于 2019-12-13 06:03:53

问题


I have hit a problem and i cant seem to get over it. I am taking information from a text file(two columns 29.12 23.42 for example separated by a space), using LOAD LOCAL DATA INFILE. Initially this was working with only one column in MySQL, however i have had to add an ID AUTO INCREMENT so i can use the most recent entry, in addition i want to now store the other column in my Database. Here is my code, its probably pretty obvious but I can't seem to find it:

<?PHP
$username = "root";
$password = "";
$hostname = "localhost";
$table = "Received_Data";

// Connect to Database and Table

$dbhandle = mysql_connect($hostname, $username, $password)
    or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

$selectdatabase = mysql_select_db("IWEMSData",$dbhandle)
    or die("Could not select IWEMSData");
echo "Connected to IWEMSData<br>";

// Clear Current Table and ReCreate

$dt = /*"UPDATE Received_Data SET PotVal = ''";*/ "DROP TABLE Received_Data";
mysql_query($dt);

$ctb = "CREATE TABLE Received_Data
(
Current DECIMAL(30,2) NOT NULL,
PotVal DECIMAL(30,2) NOT NULL, 

ID BIGINT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(ID,PotVal,Current)
)";

mysql_query($ctb, $dbhandle);

echo "Received_Data Table has been created";

// Text File to read in

$Textfile = 'IWEMSData.txt';

mysql_query('

LOAD DATA LOCAL INFILE "IWEMSData.txt" 
REPLACE INTO TABLE Received_Data 
FIELDS TERMINATED BY "" 
LINES TERMINATED BY "\\r\\n
(PotVal, Current)
";')

or die('Error Loading Data File.<br>' . mysql_error());

// Close Database connection when complete

mysql_close($dbhandle);
?>

So what i want is Column 1 is ID which will AUTO INCREMENT upon each entry. The second row is PotVal and the third row is Current. Then i want to only store in the second and third column.

The problem i am getting is that ID and Current are displaying the incorrect values and i can only seem to get one row.

Thanks in advance!


回答1:


mysql_query('
LOAD DATA LOCAL INFILE "IWEMSData.txt" 
REPLACE INTO TABLE Received_Data 
FIELDS TERMINATED BY "" /*<- terminated by nothing? Shouldn't there be a space in it?*/
LINES TERMINATED BY "\\r\\n /*<- the closing " is missing*/
(PotVal, Current)
";') /*<- ah, here's the missing " What does it do here?*/

Write it like this:

mysql_query('
LOAD DATA LOCAL INFILE "IWEMSData.txt" 
REPLACE INTO TABLE Received_Data 
FIELDS TERMINATED BY " " 
LINES TERMINATED BY "\\r\\n"
(PotVal, Current)
;')


来源:https://stackoverflow.com/questions/16150401/mysql-load-local-data-infile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!