5 global $DB_URL, $IS_WEB_REQUEST;
7 if(!isset($IS_WEB_REQUEST)) {
8 // only the web requests are allowed to create dbs
9 echo "no web request, no db\n";
15 global $BASE_DIR, $DB_HANDLE;
16 if($DB_HANDLE != false) return $DB_HANDLE;
18 $dbobject = new PDO("$DB_URL");
19 } catch(PDOException $exep) {
20 error_log("execpt on db open");
24 $DB_HANDLE = $dbobject;
29 function db_createDB()
31 // theres not much to do yet with only sqlite support
34 // all columns end up as text
35 function db_createTable($tablename)
39 if(!$db) return false;
41 if(db_tableExists($tablename)) return true;
43 $sql = "create table \"$tablename\" (\"".$tablename."_id\" INTEGER PRIMARY KEY AUTOINCREMENT";
44 for($i=1; $i < func_num_args(); $i++) {
45 $colname = func_get_arg($i);
46 $sql .= ", $colname text";
54 function db_insertData($tablename)
59 if(!$db) return false;
61 $sql = "insert into \"$tablename\" values (NULL";
62 for($i=1; $i < func_num_args(); $i++) {
63 $sql .= ",'".func_get_arg($i)."'";
67 //echo "sql is $sql\n";
72 function db_getMaxValue($tablename, $column, $columnsel="", $wheresel="")
76 if(!$db) return false;
78 if($columnsel != "") {
79 $extra = " where $columnsel = '$wheresel'";
82 $sql = "select max($column) from \"$tablename\"$extra";
83 $res = $db->query($sql);
84 if(!$res) return false;
85 $data = $res->fetchAll();
91 function db_selectData($tablename, $column="", $value="", $orderby = "")
95 if(!$db) return false;
98 if($column != "") $extra = " where $column like '%$value%'";
100 if($orderby != "") $extra .= " order by $orderby";
101 $sql = "select * from \"$tablename\"$extra";
102 $res = $db->query($sql);
103 if(!$res) return false;
104 $data = $res->fetchAll();
109 function db_deleteData($tablename, $column, $value)
113 if(!$db) return false;
115 $sql = "delete from \"$tablename\" where $column like '%$value%'";
116 //echo "Sql is $sql\n";
117 return $db->query($sql);
121 function db_updateData($tablename, $column, $newdata, $wherecol, $wheredata, $exact=true)
125 if(!$db) return false;
127 if($exact) $sql = "update \"$tablename\" set $column='$newdata' where $wherecol='$wheredata'";
128 else $sql = "update \"$tablename\" set $column='$newdata' where $wherecol like '$wheredata'";
129 return $db->query($sql);
132 function db_deleteTable($tablename)
136 if(!$db) return false;
138 $sql = "drop table $tablename";
140 return $db->query($sql);
143 function db_tableExists($tablename)
147 if(!$db) return false;
149 $sql = "select count(*) from sqlite_master where type='table' and name='$tablename'";
150 $res = $db->query($sql);
152 $value = $res->fetchColumn();
154 if($value == 1) return true;
158 function db_getTables()
162 if(!$db) return false;
164 $sql = "select name from sqlite_master where type='table' and name not like 'sqlite_%'";
165 $res = $db->query($sql);
167 return $res->fetchAll();