I try to add a column in a table with swift.
my code :
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;
}
With this Code no field is added and no error occurs. Any help ? (I would like to use the native access to sqlite and no additional library)
You are only preparing the statement, not executing it. You need to call
sqlite3_step()
andsqlite3_finalize()
methods for completing the process.