SQlite Crud
SQlite Crud
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
@Override
db.execSQL(createTableQuery);
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
SQLiteDatabase db = getWritableDatabase();
String insertQuery = "INSERT INTO " + TABLE_NAME + " (name, age, salary) VALUES ('" + name +
"', " + age + ", " + salary + ");";
db.execSQL(insertQuery);
db.close();
SQLiteDatabase db = getWritableDatabase();
String updateQuery = "UPDATE " + TABLE_NAME + " SET salary = " + newSalary + " WHERE id = "
+ id + ";";
db.execSQL(updateQuery);
db.close();
}
SQLiteDatabase db = getWritableDatabase();
String deleteQuery = "DELETE FROM " + TABLE_NAME + " WHERE id = " + id + ";";
db.execSQL(deleteQuery);
db.close();
}
}
• The class SQLiteExample extends SQLiteOpenHelper to handle database creation and
upgrades.
• The onCreate method is overridden to create the "employees" table if it doesn't already
exist using db.execSQL.
Override the onCreate method to create tables and define their schema using db.execSQL().
Override the onUpgrade method to handle database upgrades if needed.
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@Override
String createTableQuery = "CREATE TABLE IF NOT EXISTS mytable (_id INTEGER PRIMARY KEY,
name TEXT, age INTEGER);";
db.execSQL(createTableQuery);
}
@Override
return getWritableDatabase();
db.close();
}
}
2. The DATABASE_NAME variable holds the name of the database file (mydatabase.db in this
example).
5. The onCreate() method is called when the database is created for the first time. We execute
an SQL query to create the table mytable if it doesn't already exist.
6. The onUpgrade() method is called when the database needs to be upgraded to a higher
version. You can implement your upgrade logic here.
7. The openDatabase() method returns a writable instance of the database. You can call this
method to obtain an instance and perform database operations.
8. The closeDatabase() method is used to close the database instance when you're done using
it.
To use this DatabaseHelper class, you can instantiate it in your activity or fragment and call the
openDatabase() method to get a reference to the database. Remember to call the closeDatabase()
method when you're finished with the database to release resources.
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
performSQLiteOperations();
// Insert an employee
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex("id"));
Toast.makeText(this, "Employee: " + id + ", " + name + ", " + age + ", " + salary,
Toast.LENGTH_SHORT).show();
} while (cursor.moveToNext());
}
cursor.close();
// Delete an employee
dbHelper.deleteEmployee(2, this);
@Override
super.onDestroy();
dbHelper.close();