代码之家  ›  专栏  ›  技术社区  ›  dan-gph

SQLite drop column例程

  •  8
  • dan-gph  · 技术社区  · 14 年前

    我需要编写一个用于操作SQLite数据库的DROP COLUMN例程。

    我们可以这样称呼它:

    dropColumn("SomeTable", "SomeColumn");
    

    把它封装成一个例程应该不难。但看起来写起来有点烦人。

    1 回复  |  直到 14 年前
        1
  •  5
  •   Sparafusile    13 年前

    下面是一些伪代码:

    columnNameList = ""
    newTableStr = "CREATE TABLE tempMyTable ("
    execute statement: "PRAGMA table_info('MyTable')"
    While looping through RecordSet
      If RecordSet.name != tableRowToDeleteName
        If columnNameList != "" Then columnNameList += ","
        columnNameList += RecordSet.name
    
        newTableStr += RecordSet.name + " " + RecordSet.type
        If RecordSet.notnull Then
          newTableStr += " NOT NULL"
        End If
        If RecordSet.dflt_value != "" Then
          newTableStr += " DEFAULT(" + RecordSet.dflt_value + ")"
        End If
        If Not Last Record in RecordSet
          newTableStr += ","
        End If
      End If
    End Loop
    newTableStr += ");"
    
    execute statement: newTableStr
    execute statement: "INSERT INTO tempMyTable (" + columnNameList + ")" + 
                       "SELECT " + columnNameList + " FROM MyTable;"
    
    Delete table: MyTable
    Rename Table: tempMyTable to MyTable