Last active
August 29, 2015 14:18
-
-
Save rodrigopedra/274d55e4397a525200ad to your computer and use it in GitHub Desktop.
CSV -> banco
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Matemática;;;;; | |
Nome;RA;B1;B2;B3;B4 | |
Gabriel Nasciemento;654321;10;10;10;10 | |
Gabriel Nascimento;12345678999;;;; | |
Matheus Menezes;223344;7;8;6;5 | |
Thiel Caique;123456;;;; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//***** CONEXAO COM O BANCO DE DADOS | |
$servidor = 'localhost'; | |
$porta = 3306; | |
$banco = 'gabriel'; | |
$usuario = 'root'; | |
$senha = '123456'; | |
$connectionString = sprintf( 'mysql:host=%s;port=%s;dbname=%s', $servidor, $porta, $banco ); | |
$pdo = new PDO( $connectionString, $usuario, $senha ); | |
$pdo->setAttribute( PDO::ATTR_EMULATE_PREPARES, TRUE ); | |
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); | |
$pdo->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC ); | |
//***** CONSULTAS | |
$stmtMateria = $pdo->prepare( 'SELECT idMat FROM materias WHERE nome = :nome LIMIT 1;' ); | |
$stmtMateria->bindParam( ':nome', $nomeMateria ); | |
$stmtUsuario = $pdo->prepare( 'SELECT idUser FROM usuarios WHERE numRegistro = :numRegistro LIMIT 1;' ); | |
$stmtUsuario->bindParam( ':numRegistro', $numRegistro ); | |
$stmtNota = $pdo->prepare( | |
'INSERT INTO notas (matId, bimId, valor, userId) VALUES (:matId, :bimId, :valor, :userId);' | |
); | |
$stmtNota->bindParam( ':matId', $matId ); | |
$stmtNota->bindParam( ':bimId', $bimId ); | |
$stmtNota->bindParam( ':valor', $valor ); | |
$stmtNota->bindParam( ':userId', $userId ); | |
//***** PROCESSAR ARQUIVO | |
$arquivo = fopen( 'dados.csv', 'r' ); | |
$linha = 0; | |
while ( ( $dados = fgetcsv( $arquivo, 1024, ";" ) ) !== FALSE ) | |
{ | |
// soma 1 na linha | |
$linha++; | |
// 1a. linha = nome da mateira | |
if ( $linha === 1 ) | |
{ | |
$nomeMateria = $dados[ 0 ]; | |
$stmtMateria->execute(); | |
if ( $stmtMateria->rowCount() < 1 ) | |
{ | |
throw new \Exception( 'Matéria não encontrada' ); | |
} | |
$matId = $stmtMateria->fetchColumn( 0 ); | |
continue; // vai para a próxima linha | |
} | |
// 2a. linha = nome das colunas | |
if ( $linha === 2 ) | |
{ | |
continue; // vai para a próxima linha | |
} | |
// 3a. linha em diante = dados | |
// Busca userId | |
$numRegistro = $dados[1]; | |
$stmtUsuario->execute(); | |
if ( $stmtMateria->rowCount() < 1 ) | |
{ | |
throw new \Exception( 'Usuário não encontrado' ); | |
} | |
$userId = $stmtUsuario->fetchColumn( 0 ); | |
// 1o. bimestre | |
$bimId = 1; | |
$valor = $dados[2] ?: NULL; | |
$stmtNota->execute(); | |
// 2o. bimestre | |
$bimId = 2; | |
$valor = $dados[3] ?: NULL; | |
$stmtNota->execute(); | |
// 3o. bimestre | |
$bimId = 3; | |
$valor = $dados[4] ?: NULL; | |
$stmtNota->execute(); | |
// 4o. bimestre | |
$bimId = 4; | |
$valor = $dados[5] ?: NULL; | |
$stmtNota->execute(); | |
} | |
fclose( $arquivo ); | |
echo 'FIM', PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment