我遇到了一个问题,我似乎无法克服它。我正在使用LOAD LOCAL DATA INFILE
从文本文件(两列29.12 23.42,例如用空格分隔)中获取信息。最初这只与MySQL中的一个列一起使用,但是我必须添加一个ID AUTO INCREMENT
所以我可以使用最新的条目,此外我想现在将另一列存储在我的数据库中。这是我的代码,它可能很明显,但我似乎无法找到它:
<?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);
?>
所以我想要的是第1列是ID,每次进入时都会自动增加。第二行是PotVal,第三行是Current。然后我想只存储在第二和第三列。
我得到的问题是ID和Current显示的值不正确,我似乎只能得到一行。
提前致谢!
答案 0 :(得分:4)
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?*/
像这样写:
mysql_query('
LOAD DATA LOCAL INFILE "IWEMSData.txt"
REPLACE INTO TABLE Received_Data
FIELDS TERMINATED BY " "
LINES TERMINATED BY "\\r\\n"
(PotVal, Current)
;')