I have this code, which according to me should delete a table that was created in a SQLite database with Swift, but when I execute the function it is not deleted, the first function is the creation function and the one that opens the connection to the database data, hence the delete table function that is not working for me.
init()
{
db = openDatabase()
}
let dbPath: String = "DataBase.sqlite"
var db:OpaquePointer?
//Creo la BD y abro la conexión
func openDatabase() -> OpaquePointer?
{
let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
.appendingPathComponent(dbPath)
var db: OpaquePointer? = nil
if sqlite3_open(fileURL.path, &db) != SQLITE_OK
{
print("Error al abrir la Base de Datos")
return nil
}
else
{
print("Se ha creado y abierto la BD: \(dbPath)")
return db
}
}
func eliminarTabla(tabla: String){
let dropStatementString = "DROP TABLE 'DataBase.\(tabla)';"
var dropStatement : OpaquePointer? = nil
sqlite3_prepare_v2(db, dropStatementString, -1, &dropStatement, nil)
if sqlite3_step(dropStatement) == SQLITE_DONE {
print("Se ha eliminado la tabla \(tabla)")
} else {
print("No se pudo eliminar la tabla.")
}
sqlite3_finalize(dropStatement)
}
Could you tell me where the error is. Thank you
In your code you need to open the connection to the database and in the one
prepare
you are usingdb
and not theOpaquePointer
one that opens the connection to the database.