how to add column in a sqlite table in SWIFT

被刻印的时光 ゝ 提交于 2019-12-05 10:50:32

You are only preparing the statement, not executing it. You need to call sqlite3_step() and sqlite3_finalize() methods for completing the process.

func adddbfield(sFieldName:String, sTable:String) -> Bool
{
    var bReturn:Bool = false;
    var sSQL="ALTER TABLE " + sTable + " ADD COLUMN " + sFieldName + " INTEGER DEFAULT -1";
    var statement:COpaquePointer = nil
    if sqlite3_prepare_v2(db, sSQL, -1, &statement, nil) != SQLITE_OK
    {
        println("Failed to prepare statement")
    }
    else
    {
        if sqlite3_step(statement) == SQLITE_DONE
        {
           println("field " + sFieldName + " added  to  " + sTable);
        }
        sqlite3_finalize(statement);
        bReturn=true;
    }
    return bReturn;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!