При пакетной вставке в PHP вместо последней строки дублируется вторая последняя строка, а фактическая последняя строка нPhp

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 При пакетной вставке в PHP вместо последней строки дублируется вторая последняя строка, а фактическая последняя строка н

Сообщение Anonymous »

Я выполняю пакетную вставку в приведенном ниже коде, извлекаю данные из базы данных и вставляю в другой источник, данные вставляются полностью, за исключением последней строки. Вместо этого дублируется вторая последняя строка.
В журнале ошибок также отображаются фактические данные о количестве строк.
try {
// Connection to source database
$sourceDB = new PDO("mysql:host=$hostSource;dbname=$dbSource", $userSource, $passSource);
$sourceDB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Connection to target database
$targetDB = new PDO("mysql:host=$hostDest;dbname=$dbDest", $userDest, $passDest);
$targetDB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Step 1: Extract data from the source database
$query = "SELECT * FROM chapter_contributor_info"; // Fetch all records
$sourceData = $sourceDB->query($query)->fetchAll(PDO::FETCH_ASSOC);

// Log the number of records fetched
$fetchedCount = count($sourceData);
file_put_contents('fetch_count.log', "Fetched Records: $fetchedCount\n");

// Step 2: Check if the source data is empty
if ($fetchedCount === 0) {
file_put_contents('insert_count.log', "No records fetched from the source database.\n", FILE_APPEND);
exit("No records to insert.");
}

// Step 3: Transform data (e.g., lowercase email)
foreach ($sourceData as &$row) {
$row['email'] = strtolower($row['email']);
}

// Step 4: Clear existing data in the target table
$targetDB->exec("DELETE FROM chapter_contributor_info");

// Step 5: Insert data in batches
$batchSize = 1000; // Adjust batch size as needed
$dataCount = count($sourceData);
$targetDB->beginTransaction(); // Start transaction for performance
$insertedCount = 0; // Counter for successfully inserted rows

// Insert data in batches
for ($i = 0; $i < $dataCount; $i += $batchSize) {
// Handle the batch data
$batchData = array_slice($sourceData, $i, $batchSize);

// Build the query for batch insertion
$values = [];
$params = [];
foreach ($batchData as $row) {
$values[] = "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$params = array_merge($params, [
$row['bookcode'], $row['chapter_doi'], $row['chapter'], $row['chapter_title'],
$row['firstname'], $row['lastname'], $row['contributor_type'], $row['email'],
$row['affiliation'], $row['country'], $row['bookxml_year']
]);
}

// Prepare the batched insert query
$sql = "INSERT INTO chapter_contributor_info
(bookcode, chapter_doi, chapter, chapter_title, firstname, lastname, contributor_type, email, affiliation, country, bookxml_year)
VALUES " . implode(", ", $values);

try {
$stmt = $targetDB->prepare($sql);
$stmt->execute($params);
$insertedCount += $stmt->rowCount(); // Count inserted rows
file_put_contents('insert_batch.log', "Inserted batch starting at index $i: " . count($batchData) . " rows.\n", FILE_APPEND);
} catch (PDOException $e) {
// Log error message for debugging
file_put_contents('insert_error.log', "Error during batch insert at index $i: " . $e->getMessage() . "\nSQL: $sql\nData: " . print_r($params, true) . "\n", FILE_APPEND);
}
}

// Explicitly check and insert the last row if it wasn't included
$lastRowIndex = $dataCount - 1;
$lastRow = $sourceData[$lastRowIndex]; // Get the last row directly

// Verify if the last row was already inserted
if ($insertedCount < $dataCount) {
try {
$stmt = $targetDB->prepare("INSERT INTO chapter_contributor_info
(bookcode, chapter_doi, chapter, chapter_title, firstname, lastname, contributor_type, email, affiliation, country, bookxml_year)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([
$lastRow['bookcode'], $lastRow['chapter_doi'], $lastRow['chapter'], $lastRow['chapter_title'],
$lastRow['firstname'], $lastRow['lastname'], $lastRow['contributor_type'], $lastRow['email'],
$lastRow['affiliation'], $lastRow['country'], $lastRow['bookxml_year']
]);
$insertedCount += $stmt->rowCount(); // Count inserted rows
file_put_contents('insert_batch.log', "Inserted last row: " . print_r($lastRow, true) . "\n", FILE_APPEND);
} catch (PDOException $e) {
file_put_contents('last_row_error.log', "Error inserting last row: " . $e->getMessage() . "\nRow Data: " . print_r($lastRow, true) . "\n", FILE_APPEND);
}
}

// Log total rows inserted
file_put_contents('insert_count.log', "Total Inserted Rows: $insertedCount\n");

// Commit the transaction after all batches are processed
$targetDB->commit();
echo "Data successfully extracted, transformed, and inserted into chapter_contributor_info in batches. Total inserted rows: $insertedCount.";

} catch (PDOException $e) {
// Roll back the transaction in case of error
if ($targetDB->inTransaction()) {
$targetDB->rollBack();
}
echo "Error: " . $e->getMessage();
}

// Close connections
$sourceDB = null;
$targetDB = null;


Подробнее здесь: https://stackoverflow.com/questions/790 ... getting-du
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»