sql test
This commit is contained in:
parent
8d3b914433
commit
5c037ee8c5
4
import_csv.sql
Normal file
4
import_csv.sql
Normal file
@ -0,0 +1,4 @@
|
||||
CREATE TABLE IF NOT EXISTS løpere(id INTEGER PRIMARY KEY, navn TEXT);
|
||||
.separator ,
|
||||
.mode csv
|
||||
.import db.csv QNH
|
||||
91
oppdater_db.php
Normal file
91
oppdater_db.php
Normal file
@ -0,0 +1,91 @@
|
||||
<?php
|
||||
function import_csv_to_sqlite(&$pdo, $csv_path, $options = array())
|
||||
{
|
||||
extract($options);
|
||||
|
||||
if (($csv_handle = fopen($csv_path, "r")) === FALSE)
|
||||
throw new Exception('Cannot open CSV file');
|
||||
|
||||
if(!$delimiter)
|
||||
$delimiter = ',';
|
||||
|
||||
if(!$table)
|
||||
$table = preg_replace("/[^A-Z0-9]/i", '', basename($csv_path));
|
||||
|
||||
if(!$fields){
|
||||
$fields = array_map(function ($field){
|
||||
return strtolower(preg_replace("/[^A-Z0-9]/i", '', $field));
|
||||
}, fgetcsv($csv_handle, 0, $delimiter));
|
||||
}
|
||||
|
||||
$create_fields_str = join(', ', array_map(function ($field){
|
||||
return "$field TEXT NULL";
|
||||
}, $fields));
|
||||
|
||||
$pdo->beginTransaction();
|
||||
|
||||
$create_table_sql = "CREATE TABLE IF NOT EXISTS $table ($create_fields_str)";
|
||||
$pdo->exec($create_table_sql);
|
||||
|
||||
$insert_fields_str = join(', ', $fields);
|
||||
$insert_values_str = join(', ', array_fill(0, count($fields), '?'));
|
||||
$insert_sql = "INSERT INTO $table ($insert_fields_str) VALUES ($insert_values_str)";
|
||||
$insert_sth = $pdo->prepare($insert_sql);
|
||||
|
||||
$inserted_rows = 0;
|
||||
while (($data = fgetcsv($csv_handle, 0, $delimiter)) !== FALSE) {
|
||||
$insert_sth->execute($data);
|
||||
$inserted_rows++;
|
||||
}
|
||||
|
||||
$pdo->commit();
|
||||
|
||||
fclose($csv_handle);
|
||||
|
||||
return array(
|
||||
'table' => $table,
|
||||
'fields' => $fields,
|
||||
'insert' => $insert_sth,
|
||||
'inserted_rows' => $inserted_rows
|
||||
);
|
||||
|
||||
}
|
||||
import_csv_to_sqlite()
|
||||
|
||||
$password = $_POST['password'];
|
||||
|
||||
function send_response ($response, $code = 200) {
|
||||
http_response_code($code);
|
||||
die(json_encode($response));
|
||||
}
|
||||
|
||||
if ($method === 'POST') {
|
||||
if (!password_verify($password, $hash)) {
|
||||
http_response_code(response_code: 401);
|
||||
}
|
||||
else {
|
||||
$file = 'passering.csv';
|
||||
$current = file_get_contents($file);
|
||||
$current .= $control . "," . $runner_id . "," . $time . "\n";
|
||||
file_put_contents($file, $current);
|
||||
}
|
||||
if (empty($data['favorite'])) {
|
||||
send_response([
|
||||
'status' => 'failed',
|
||||
'message' => 'Please provide a favorite movie.',
|
||||
], 400);
|
||||
}
|
||||
|
||||
send_response([
|
||||
'status' => 'success',
|
||||
'message' => 'Fila er lasta opp',
|
||||
]);
|
||||
|
||||
}
|
||||
else {
|
||||
send_response([
|
||||
'status' => 'success',
|
||||
'message' => 'Fila er lasta opp',
|
||||
|
||||
], 400);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user