-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.php
63 lines (51 loc) · 1.64 KB
/
setup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
/**
* @author Fabio Cionini <[email protected]>
*
* Date: 05/04/15
* Time: 15:20
*/
try {
// Set default timezone
date_default_timezone_set('UTC');
$sqlite_db = new \PDO('sqlite:'.$config['database']['sqliteConnection']['filename']);
$sqlite_db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
// Create table addresses
$sqlite_db->exec("CREATE TABLE IF NOT EXISTS address (
id INTEGER PRIMARY KEY,
name TEXT,
phone TEXT,
street TEXT)");
$sqlite_db->exec("DELETE FROM address");
$sqlite_db->exec("VACUUM");
// Set initial data
$addresses = [];
$file = fopen('example.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
$addresses[] = [
'name' => $line[0],
'phone' => $line[1],
'street' => $line[2]
];
}
// Prepare INSERT statement to SQLite3 file db
$insert = "INSERT INTO address (name, phone, street) VALUES (:name, :phone, :street)";
$stmt = $sqlite_db->prepare($insert);
// Bind parameters to statement variables
$stmt->bindParam(':name', $name);
$stmt->bindParam(':phone', $phone);
$stmt->bindParam(':street', $street);
// execute prepared insert statement with every address
foreach ($addresses as $a) {
// Set values to bound variables
$name = $a['name'];
$phone = $a['phone'];
$street = $a['street'];
// Execute statement
$stmt->execute();
}
echo 'Database setup complete, '.count($addresses).' records created.';
}
catch(PDOException $e) {
echo $e->getMessage();
}