douzao9845 2019-06-11 12:20 采纳率: 0%
浏览 225

建立SQL查询以从map [string] interface {}添加新的列和值

I am new to Go & CQL and I need some help with the following (couldn't find an answer to my question on the platform): I have a map[string]interface{} and for each k,v pair in this map, I need to check if the column (represented by k) exists in the field slice...and if not - add it as a new column to the table (with the specific type, based on the value type), as well as the value. I am struggling finding a way to get the value type, convert it to a Cassandra data type & include it in the query string as a column type. My code so far looks like this:

//takes as arguments the map and a slice that holds the columns that are already in the table. Is there 
//A fucntion that returns a string to be executed later by //session.Query(...).Exec() to add columns that are missing in the table
func AddColumns(myInterfaceMap map[string]string, fields []string) string {

    var columnsToBeAdded []string

    var sb strings.Builder
    sb.WriteString("ALTER TABLE sample_demo.main ADD (")
    for k := range myInterfaceMap {

        if !contains(fields, k) {
            columnsToBeAdded = append(columnsToBeAdded, k)
            fields = append(Fields, k)
        }
    }
    for idx, column := range columnsToBeAdded {
        if idx == (len(columnsToBeAdded) - 1) {
            sb.WriteString(column)
            //I need to dynamically specify the column type, depending on value   for the 
            //concrete key...need help how to do it
            sb.WriteString(" text)") 

        } else {
            sb.WriteString(column)
            sb.WriteString(" text, ")
        }
    }
    log.Println(sb.String())
    log.Println(fields)
    return sb.String()
}

If someone can help, that would be indeed appreciated :)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据