general function to check if a table exists
authorAndreas Unterkircher <unki@netshadow.at>
Sat, 9 Jun 2007 06:37:44 +0000 (06:37 +0000)
committerAndreas Unterkircher <unki@netshadow.at>
Sat, 9 Jun 2007 06:37:44 +0000 (06:37 +0000)
general function to execute a query without handling results

git-svn-id: file:///var/lib/svn/phpfspot/trunk@64 fa6a889d-dae6-447d-9e79-4ba9a3039384

phpfspot.class.php
phpfspot_db.php

index de2adc086f001265f2d53123c7048493f10f187d..d62be697f2c0677878dff847f7e72edb255d846c 100644 (file)
@@ -19,7 +19,10 @@ class PHPFSPOT {
       $this->cfg = new PHPFSPOT_CFG;
 
       $this->db = new PHPFSPOT_DB(&$this, $this->cfg->fspot_db);
+
       $this->cfg_db = new PHPFSPOT_DB(&$this, $this->cfg->phpfspot_db);
+      $this->check_config_table();
+
       $this->tmpl = new PHPFSPOT_TMPL($this);
 
       $this->get_tags();
@@ -343,6 +346,20 @@ class PHPFSPOT {
 
    } // get_meta_informations()
 
+   public function check_config_table()
+   {
+      // if the config table doesn't exist yet, create it
+      if(!$this->cfg_db->db_check_table_exists("images")) {
+         $this->cfg_db->db_exec("
+            CREATE TABLE images (
+               img_idx int primary key,
+               img_md5 varchar(32)
+            )
+            ");
+      }
+
+   } // check_config_table
+
 }
 
 ?>
index 6c86563ed962ca8b813aae3ff3b1bdb4a42048a4..585e5592d3ba70caae80411afe04cf2680a4c4c3 100644 (file)
@@ -110,6 +110,24 @@ class PHPFSPOT_DB {
 
    } // db_query()
 
+   /**
+    * PHPFSPOT_DB database query & execute
+    *
+    * This function will execute a SQL query and return nothing.
+    */
+   public function db_exec($query = "")
+   {
+      if($this->getConnStatus()) {
+
+         if(($result = sqlite3_exec($this->db, $query)) === false)
+            $this->ThrowError($this->getLastError());
+
+      }
+      else 
+         $this->ThrowError("Can't execute query - we are not connected!");
+
+   } // db_exec()
+
    public function db_fetch_object(&$resource)
    {
       return sqlite3_fetch_array($resource);
@@ -178,11 +196,10 @@ class PHPFSPOT_DB {
    public function db_check_table_exists($table_name = "")
    {
       if($this->getConnStatus()) {
-         $result = $this->db_query("SHOW TABLES");
-         $tables_in = "Tables_in_". MYSQL_DB;
-       
-         while($row = $result->fetchRow()) {
-            if($row->$tables_in == $table_name)
+
+         $result = $this->db_query("SELECT name FROM sqlite_master WHERE type='table'");
+         while($table = $this->db_fetch_object($result)) {
+            if($table['name'] == $table_name)
                return true;
          }
          return false;