Check if a column exists in SQLite

前端 未结 15 499
我寻月下人不归
我寻月下人不归 2020-12-08 07:02

I need to check to see if a column exists and if it doesn\'t exist add it. From my research it looks like sqlite doesn\'t support IF statements and case statement should be

15条回答
  •  北海茫月
    2020-12-08 07:14

      public static bool columExsist(string table, string column)
        {
            string dbPath = Path.Combine(Util.ApplicationDirectory, "LocalStorage.db");
    
            connection = new SqliteConnection("Data Source=" + dbPath);
            connection.Open();
    
            DataTable ColsTable = connection.GetSchema("Columns");
    
            connection.Close();
    
            var data = ColsTable.Select(string.Format("COLUMN_NAME='{1}' AND TABLE_NAME='{0}1'", table, column));
    
            return data.Length == 1;
        }
    

提交回复
热议问题