2013-07-15 22 views
1

可否請您告訴我如何檢查表名是否存在於數據庫或現在。我需要統計表中的元素數量。但如果沒有表存在在分貝然後它會出錯。?如何檢查sqlite數據庫中存在的表名(手機間隙)

如果數據庫中沒有表(名稱= caseName_h),那麼它會給出錯誤。所以我需要檢查。如果存在這個表,那麼我需要計算元素的數量。 我嘗試這樣

db.transaction(function (tx) { 
    tx.executeSql('PRAGMA table_info(a)', [], 
       function(tx, results) { 
       alert("hh"); 
    if (results.rows.length == 0) { 
     alert("No") 
    } else { 
     tx.executeSql('SELECT COUNT(*) FROM a' , [], 
         function(tx, results) { 
         alert("Yes"); 

        }); 
    } 
}); 
}); 

回答

0

執行PRAGMA table_info,並檢查是否你得到任何東西:

tx.executeSql('PRAGMA table_info(' + caseName_h + ')', [], 
       function(tx, results) { 
    if (results.rows.length == 0) { 
     // no table 
    } else { 
     tx.executeSql('SELECT COUNT(*) FROM ' + caseName_h, [], 
         function(tx, results) { 
      // ... 
     }); 
    } 
}); 
+0

能否請您解釋一下深..? –

+0

Ehis不工作.. :( –

+1

你的錯誤描述沒有用 –

1

您還可以查詢到數據庫檢查該表是否存在

tx.executeSql("SELECT name FROM sqlite_master WHERE type='table' AND name='tableName'", [], function (tx, result) { 
    if (result.rows.length == 0) { 
     //Your logic if table doesnt exist 
    } 
}); 

這裏如果result.rows.length == 0表示該表不存在,否則存在。

2

檢查:

this.db.transaction(
    function (tx) { 
     tx.executeSql("SELECT name FROM sqlite_master WHERE type='table'", [], function (tx, result) { 
      if (result.rows.length == 1) { 
       //here are no your tables currently 
      } else { 

      } 
     }); 
    } 
); 
相關問題