2012-07-04 33 views
-1

我正在尋找一種方法來添加列一個表,只有當它尚未在PHP腳本中使用sqlite3的存在。它看起來像SQL本身並不能幫助我在這裏,例如喜歡的東西使用PHP/sqlite3的,我怎麼能增加一列,如果它沒有在表中存在嗎?

ALTER TABLE items ADD COLUMN IF NOT EXISTS new_col INTEGER; 

嗯,我想就像試圖運行一個查詢,如果它失敗了,總結列尚不存在黑客:

if ($db->querySingle('SELECT new_col FROM items') === FALSE) { 
    // False means the query failed 
    $db->exec('ALTER TABLE items ADD COLUMN new_col INTEGER'); 
} 

update_stuff_with_new_column($db); 

我想它應該工作,但如果查詢因爲其他原因(例如,事務正在運行)失敗會怎麼樣?

我能想到的還有其他一些解決方案,但它們看起來都一樣:「如果查詢失敗,該列可能不存在,請創建它」。有沒有更好的方法來做到這一點?

+3

爲什麼啊,爲什麼你想在運行時更改的模式? –

+0

這是一個將醜陋的數據庫轉換爲更好的數據庫的腳本。嗯,其實,我有一些不同的數據庫文件,但他們沒有完全一樣的模式,他們中的一些已經有new_col列,但過時的價值觀,而其他沒有列在所有。我試圖將它們全部合併。是的,那是相當難看,但,這就是生活:/ – Fabien

回答

1

您可以找到sqlite_masterCREATE TABLE語句的SQL;你需要分析它,看是否new_col定義尚未:

sqlite> create table items (col1, col2); 
sqlite> select sql from sqlite_master where type = 'table' and name = 'items'; 
CREATE TABLE items (col1, col2) 
sqlite> alter table items add column new_col; 
sqlite> select sql from sqlite_master where type = 'table' and name = 'items'; 
CREATE TABLE items (col1, col2, new_col) 

附錄:

其他方法,看看是否列在一個表中已經存在,包括:

試圖準備( sqlite3_prepare_v2)形式

select ColumnToCheck from TableToCheck; 

的聲明,看看它是否給出了一個錯誤(感謝伊戈爾Tandetnik!)。

或者使用

PRAGMA table_info(myTable) 

記錄here

+0

嗯,我會嘗試這種方式,似乎是有前途的。謝謝 :) – Fabien

0
php file 
=========== 
<pre> 
<?php 


define(DB_USER,'root'); 
define(DB_PASS,''); 
define(DB_NAME,'DATABASENAME'); 

//Load the class 

/* Creating Connection for database where we want to make changes using sql file */ 

$con = mysql_connect('localhost',DB_USER,DB_PASS) or die ("error in connecting $sitename "); 
mysql_select_db(DB_NAME,$con) or die("error in databse selction in $sitename"); 



/* getting content from sql file where we have updated fields */ 
$fp= file_get_contents('blank_hotel_version_1.0.sql', true); 




/* getting list of create table query from sql file content */ 

$pattern_gettable = '/CREATE TABLE IF NOT EXISTS (.*?) ;/is'; 

preg_match_all($pattern_gettable,$fp,$tables); 

foreach($tables[0] as $value): 
    mysql_query($value,$con) or die(mysql_error()); 
endforeach; 





//exit; 


/* getting Database table list */ 

$tables_db ="*"; 
if($tables_db == '*') 
{ 
    $tables_db = array(); 
    $result = mysql_query('SHOW TABLES'); 
    while($row = mysql_fetch_row($result)) 
    { 
     $tables_db[] = $row[0]; 
    } 
} 
else 
{ 
    $tables_db = is_array($tables_db) ? $tables_db : explode(',',$tables_db); 
} 



foreach($tables_db as $tablename): 
    $pattern = "CREATE TABLE IF NOT EXISTS `{$tablename}`"; 

    foreach($tables[0] as $value): 

     if(strpos($value,$pattern)=== false): 

     else: 

      /*getting columns list from perticular table query of sql file */ 

      $pattern_cloumn="/CREATE TABLE IF NOT EXISTS `{$tablename}` \((.*?) PRIMARY KEY/is"; 
      preg_match_all($pattern_cloumn,$value,$columns); 


      /*Replacing comma with # so that while we exploade we can easily make array of columns othere wise it make problem for enum(..) datatype or comment */ 
      $pattern_cm="/\n/ims"; 
      $columns[1][0]= preg_replace($pattern_cm,"#",$columns[1][0]); 
      $colmn=explode("#",$columns[1][0]); 
      array_shift($colmn); 


      /*Getting name of columns currently we have columns with sql query like "`id` int(11) not null," and we get column name like 'id' into $columnnames array */ 
      $columnnames=array(); 
      foreach($colmn as $cols): 
       $pattern = '/`(.*?)`/is'; 
       preg_match_all($pattern, $cols ,$namecols); 

       array_push($columnnames,$namecols[1][0]); 
      endforeach; 


      /*Getting column names from database table */ 
      $column_query="SHOW COLUMNS FROM {$tablename}"; 
      $result_clm=mysql_query($column_query); 
      $colum_db_array=array(); 
      while($row_clm=mysql_fetch_array($result_clm)) 
      { 
       array_push($colum_db_array,$row_clm['Field']); 
      } 


      foreach($columnnames as $key=>$value_cols) 
      { 
       /*if column not exists in database table then we add column into table */ 

       if(!in_array($value_cols,$colum_db_array) && $value_cols!="") 
       { 

        if(substr($colmn[$key],strlen($colmn[$key])-2)=="',") 
        { 
         $query_cl="ALTER TABLE {$tablename} ADD ".substr($colmn[$key],0,strlen($colmn[$key])-1); 
        } 
        else 
        { 
         $query_cl="ALTER TABLE {$tablename} ADD ".substr($colmn[$key],0,strlen($colmn[$key])-1); 
        } 

        mysql_query($query_cl) or die(mysql_error()); 
       } 
      } 
     endif; 


    endforeach; 


endforeach; 


exit; 

?> 
</pre> 

Sql File 
======== 
<pre> 



-- -------------------------------------------------------- 

-- 
-- Table structure for table `advertising` 
-- 

CREATE TABLE IF NOT EXISTS `advertising` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `name` varchar(255) NOT NULL, 
    `image` varchar(255) NOT NULL, 
    `title_tag` varchar(255) NOT NULL, 
    `alt_tag` varchar(255) NOT NULL, 
    `d_order` int(11) NOT NULL, 
    `is_visible` varchar(11) NOT NULL DEFAULT 'Y', 
    PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; 

-- 
-- Dumping data for table `advertising` 
-- 


-- -------------------------------------------------------- 

-- 
-- Table structure for table `advertising_enquiry` 
-- 

CREATE TABLE IF NOT EXISTS `advertising_enquiry` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `advertising_id` varchar(255) NOT NULL, 
    `name` varchar(255) NOT NULL, 
    `address` varchar(255) NOT NULL, 
    `city` varchar(255) NOT NULL, 
    `state` varchar(255) NOT NULL, 
    `country` varchar(255) NOT NULL, 
    `phone` varchar(255) NOT NULL, 
    `mobile` varchar(255) NOT NULL, 
    `email` varchar(255) NOT NULL, 
    `website` varchar(255) NOT NULL, 
    `enquiry` text NOT NULL, 
    `budget` varchar(255) NOT NULL, 
    `create_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 
    PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ; 

-- 
-- Dumping data for table `advertising_enquiry` 
-- 


-- -------------------------------------------------------- 

-- 
-- Table structure for table `agents` 
-- 

CREATE TABLE IF NOT EXISTS `agents` (
    `id` int(3) NOT NULL AUTO_INCREMENT, 
    `agent_name` varchar(150) NOT NULL, 
    `contact` varchar(1000) NOT NULL, 
    `cat_id` int(3) NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ; 

-- 
-- Dumping data for table `agents` 
-- 


-- -------------------------------------------------------- 

-- 
-- Table structure for table `amenities` 
-- 

CREATE TABLE IF NOT EXISTS `amenities` (
    `id` int(3) NOT NULL AUTO_INCREMENT, 
    `am_name` varchar(200) NOT NULL, 
    `type` enum('H','R','RE') DEFAULT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=234 ; 

-- 
-- Dumping data for table `amenities` 
-- 



</pre> 

這可能會幫助你的感謝

相關問題