Skip to content
DeveloperMemos

Dropping a Table with SQLite(Android/iOS)

SQLite1 min read

When working with databases, it's common to encounter situations where you need to delete or remove a table from the database schema. SQLite, a self-contained and serverless database engine, offers a straightforward way to accomplish this task. In this article, we'll delve into the process of dropping a table using SQLite, providing examples in both Android development with Kotlin and iOS development with Swift.

Dropping a Table in SQLite

To drop a table in SQLite, you can use the DROP TABLE statement followed by the name of the table you want to delete. Here's the basic syntax:

1DROP TABLE table_name;

This statement removes the specified table from the database schema, including all associated data and indexes. It's important to note that dropping a table is a permanent action, so exercise caution when executing this command.

Example in Android (Kotlin)

In Android development using Kotlin, SQLite is commonly used as an embedded database. To drop a table in SQLite using Kotlin, you can leverage the execSQL() method available in the SQLiteDatabase class. Consider the following example:

1val tableName = "my_table"
2
3val dbHelper = MyDatabaseHelper(context)
4val db = dbHelper.writableDatabase
5
6db.execSQL("DROP TABLE IF EXISTS $tableName")

In this example, we first define the name of the table (my_table). Then, we obtain an instance of SQLiteDatabase using a custom MyDatabaseHelper class. Finally, we execute the SQL statement to drop the table by calling execSQL() with the DROP TABLE command.

Example in iOS (Swift)

In iOS development, working with SQLite involves utilizing the SQLite library directly or through an abstraction layer like Core Data. Let's see how to drop a table in SQLite using Swift:

1let tableName = "my_table"
2
3let fileURL = try! FileManager.default
4 .url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
5 .appendingPathComponent("database.sqlite")
6
7var db: OpaquePointer?
8if sqlite3_open(fileURL.path, &db) == SQLITE_OK {
9 let dropTableQuery = "DROP TABLE IF EXISTS \(tableName)"
10 if sqlite3_exec(db, dropTableQuery, nil, nil, nil) == SQLITE_OK {
11 print("Table dropped successfully.")
12 } else {
13 print("Failed to drop table.")
14 }
15 sqlite3_close(db)
16}

In this iOS example, we first define the table name as my_table. Then, we obtain the file URL of the SQLite database and open a connection to it using the sqlite3_open() function. Afterward, we construct the SQL query to drop the table and execute it with sqlite3_exec(). Finally, we close the database connection using sqlite3_close().


Dropping a table is a fundamental operation when managing databases. In this article, we explored how to drop a table using SQLite in both Android development with Kotlin and iOS development with Swift. Remember to exercise caution when dropping tables, as it permanently removes the table and its associated data from the database schema. By familiarizing yourself with these concepts, you'll be better equipped to handle table removals when working with SQLite databases.