我尝试用
swift在表中添加一列.
我的代码:
connect_db(); adddbfield("mynewfield","mytable");
.
func connect_db() -> Bool { let sDBPath=<is correctly set>; if sqlite3_open(sDBPath,&db) != sqlITE_OK { println("Failed to open db") return false; }else{ return true; } }
.
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{ println("field " + sFieldName + " added to " + sTable); bReturn=true; } return bReturn; }
解决方法
您只是准备声明,而不是执行它.您需要调用sqlite3_step()和sqlite3_finalize()方法来完成该过程.
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,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; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。