7 if(!isset($IS_WEB_REQUEST)) {
8 // only the web requests are allowed to create dbs
14 global $BASE_DIR, $DB_HANDLE;
15 if($DB_HANDLE != false) return $DB_HANDLE;
17 $dbobject = new PDO("$DB_URL");
18 } catch(PDOException $exep) {
19 error_log("execpt on db open");
23 $DB_HANDLE = $dbobject;
28 function db_createDB()
30 // theres not much to do yet with only sqlite support
33 // all columns end up as text
34 function db_createTable($tablename)
38 if(!$db) return false;
40 if(db_tableExists($tablename)) return true;
42 $sql = "create table \"$tablename\" (\"".$tablename."_id\" INTEGER PRIMARY KEY AUTOINCREMENT";
43 for($i=1; $i < func_num_args(); $i++) {
44 $colname = func_get_arg($i);
45 $sql .= ", $colname text";
53 function db_insertData($tablename)
58 if(!$db) return false;
60 $sql = "insert into \"$tablename\" values (NULL";
61 for($i=1; $i < func_num_args(); $i++) {
62 $sql .= ",'".func_get_arg($i)."'";
69 function db_selectData($tablename, $column="", $value="")
73 if(!$db) return false;
75 if($column != "") $extra = " where $column like '%$value%'";
77 $sql = "select * from \"$tablename\"$extra";
78 $res = $db->query($sql);
79 if(!$res) return false;
80 $data = $res->fetchAll();
85 function db_deleteData($tablename, $column, $value)
89 if(!$db) return false;
91 $sql = "delete from \"$tablename\" where $column like '%$value%'";
92 //echo "Sql is $sql\n";
93 return $db->query($sql);
97 function db_updateData($tablename, $column, $newdata, $wherecol, $wheredata, $exact=true)
101 if(!$db) return false;
103 if($exact) $sql = "update \"$tablename\" set $column='$newdata' where $wherecol='$wheredata'";
104 else $sql = "update \"$tablename\" set $column='$newdata' where $wherecol like '$wheredata'";
105 return $db->query($sql);
108 function db_deleteTable($tablename)
112 if(!$db) return false;
114 $sql = "drop table $tablename";
116 return $db->query($sql);
119 function db_tableExists($tablename)
123 if(!$db) return false;
125 $sql = "select count(*) from sqlite_master where type='table' and name='$tablename'";
126 $res = $db->query($sql);
128 $value = $res->fetchColumn();
130 if($value == 1) return true;
134 function db_getTables()
138 if(!$db) return false;
140 $sql = "select name from sqlite_master where type='table' and name not like 'sqlite_%'";
141 $res = $db->query($sql);
143 return $res->fetchAll();