8 global $BASE_DIR, $DB_HANDLE;
9 if($DB_HANDLE != false) return $DB_HANDLE;
11 $dbobject = new PDO("$DB_URL");
12 } catch(PDOException $exep) {
13 error_log("execpt on db open");
17 $DB_HANDLE = $dbobject;
22 function db_createDB()
24 // theres not much to do yet with only sqlite support
27 // all columns end up as text
28 function db_createTable($tablename)
32 if(db_tableExists($tablename)) return true;
34 $sql = "create table \"$tablename\" (\"".$tablename."_id\" INTEGER PRIMARY KEY AUTOINCREMENT";
35 for($i=1; $i < func_num_args(); $i++) {
36 $colname = func_get_arg($i);
37 $sql .= ", $colname text";
45 function db_insertData($tablename)
50 $sql = "insert into \"$tablename\" values (NULL";
51 for($i=1; $i < func_num_args(); $i++) {
52 $sql .= ",'".func_get_arg($i)."'";
59 function db_selectData($tablename, $column="", $value="")
63 if($column != "") $extra = " where $column like '%$value%'";
65 $sql = "select * from \"$tablename\"$extra";
66 $res = $db->query($sql);
67 if(!$res) return false;
68 $data = $res->fetchAll();
73 function db_deleteData($tablename, $column, $value)
77 $sql = "delete from \"$tablename\" where $column like '%$value%'";
78 //echo "Sql is $sql\n";
79 return $db->query($sql);
83 function db_updateData($tablename, $column, $newdata, $wherecol, $wheredata, $exact=true)
87 if($exact) $sql = "update \"$tablename\" set $column='$newdata' where $wherecol='$wheredata'";
88 else $sql = "update \"$tablename\" set $column='$newdata' where $wherecol like '$wheredata'";
89 return $db->query($sql);
92 function db_deleteTable($tablename)
96 $sql = "drop table $tablename";
98 return $db->query($sql);
101 function db_tableExists($tablename)
105 $sql = "select count(*) from sqlite_master where type='table' and name='$tablename'";
106 $res = $db->query($sql);
108 $value = $res->fetchColumn();
110 if($value == 1) return true;
114 function db_getTables()
118 $sql = "select name from sqlite_master where type='table' and name not like 'sqlite_%'";
119 $res = $db->query($sql);
121 return $res->fetchAll();