-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.php
More file actions
131 lines (116 loc) · 3.34 KB
/
Copy pathdatabase.php
File metadata and controls
131 lines (116 loc) · 3.34 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
class Database {
private $mysqli = NULL;
//Funktion som kopplar upp mot databasen med hjälp utav DBConfig.
public function Connect(DBConfig $config)
{
// Create (connect to) SQLite database in file
try
{
$this->sqlite3 = new \PDO("sqlite:".$config->DBName.".sqlite");
$this->sqlite3->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
//Skapar tabellen om den inte redan existerar
$sql = "CREATE TABLE IF NOT EXISTS tweet (
id INTEGER PRIMARY KEY,
tweetID TEXT,
videoURL TEXT)";
$prepSql = $this->Prepare($sql);
$this->runQuery($prepSql);
}
catch(PDOException $ex)
{
die("Följande fel har inträffat" . $ex->getMessage());
}
return true;
}
//Funktion som hämtar antalet i tabellen.
public function GetOne($sqlQuery) {
if ($sqlQuery === FALSE)
{
throw new \Exception($this->mysqli->error);
}
//execute the statement
if ($sqlQuery->execute() == FALSE)
{
throw new \Exception($this->mysqli->error);
}
$array = $sqlQuery->fetch(\PDO::FETCH_ASSOC);
if($array == null)
{
return false;
}
$tweet = new Tweet($array['tweetID']);
$sqlQuery = null;
return $tweet;
}
//Hämta allt i en vissa tabell.
public function GetAll($sqlQuery) {
if ($sqlQuery === FALSE)
{
throw new \Exception($this->mysqli->error);
}
//execute the statement
if ($sqlQuery->execute() == FALSE)
{
throw new \Exception($this->mysqli->error);
}
$tweetArray = array();
while ($array = $sqlQuery->fetch(\PDO::FETCH_ASSOC))
{
$tweet = new Tweet($array['tweetID'], $array['videoURL'], $array['id']);
$tweetArray[] = $tweet;
}
return $tweetArray;
}
//Funktion som lägger till.
public function runInsertQuery(\PDOStatement $stmt)
{
try
{
$stmt->execute();
}
catch (PDOException $ex)
{
print $ex->getMessage();
}
//Hämtar Id genererat ifrån det senaste insert och lägger i $ret variabeln.
$ret = $this->sqlite3->lastInsertId();
//Stänger queryn.
$stmt = null;
//Returnerar id från utförd operation.
return $ret;
}
//Funktion som tar bort.
public function runQuery(\PDOStatement $stmt)
{
try
{
$stmt->execute();
}
catch (PDOException $ex)
{
print $ex->getMessage();
}
$stmt = null;
return true;
}
//Funktion som förbereder en sträng för execute.
public function Prepare($sql)
{
try
{
$stmt = $this->sqlite3->prepare($sql);
}
catch (PDOException $ex)
{
print $ex->getMessage();
}
return $stmt;
}
//Funktion som stänger en databasanslutning.
public function Close()
{
// Close file db connection
$this->sqlite3 = null;
}
}