I created a sqlit database and I want to add the feature of exporting the database in a specific folder and importing it again later in life. I have not found a formula that actually works properly.
this DBDatabaseHelper
<code>import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
import androidx.annotation.Nullable;
class MyDatabaseHelper extends SQLiteOpenHelper {
private final Context context;
private static final String DATABASE_NAME = "myDataBase.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "my_library";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_TITLE = "book_title";
private static final String COLUMN_AUTHOR = "book_author";
private static final String COLUMN_PAGES = "book_pages";
MyDatabaseHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME +
" (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_TITLE + " TEXT, " +
COLUMN_AUTHOR + " TEXT, " +
COLUMN_PAGES + " TEXT);";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
void addBook(String title, String author, String pages){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_TITLE, title);
cv.put(COLUMN_AUTHOR, author);
cv.put(COLUMN_PAGES, pages);
long result = db.insert(TABLE_NAME,null, cv);
if(result == -1){
Toast.makeText(context, "Save failed" + ("❌ "),Toast.LENGTH_LONG).show();
}else {
/* Toast.makeText(context, "saved " + ("✅"),Toast.LENGTH_LONG).show();*/
}
}
Cursor readAllData(){
String query = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
if(db != null){
cursor = db.rawQuery(query, null);
}
return cursor;
}
void updateData(String row_id, String title, String author, String pages){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_TITLE, title);
cv.put(COLUMN_AUTHOR, author);
cv.put(COLUMN_PAGES, pages);
long result = db.update(TABLE_NAME, cv, "_id=?", new String[]{row_id});
if(result == -1){
Toast.makeText(context, "Update failed" + ("❌ "),Toast.LENGTH_LONG).show();
}else {
Toast.makeText(context, "Updated" + ("✍uFE0F"),Toast.LENGTH_LONG).show();
}
}
void deleteOneRow(String row_id){
SQLiteDatabase db = this.getWritableDatabase();
long result = db.delete(TABLE_NAME, "_id=?", new String[]{row_id});
if(result == -1){
Toast.makeText(context, "Failed to Delete.", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "Deleted " + ("uD83DuDDD1uFE0F"),Toast.LENGTH_LONG).show();
}
}
void deleteAllData(){
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_NAME);
}
}
</code>
<code>import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
import androidx.annotation.Nullable;
class MyDatabaseHelper extends SQLiteOpenHelper {
private final Context context;
private static final String DATABASE_NAME = "myDataBase.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "my_library";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_TITLE = "book_title";
private static final String COLUMN_AUTHOR = "book_author";
private static final String COLUMN_PAGES = "book_pages";
MyDatabaseHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME +
" (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_TITLE + " TEXT, " +
COLUMN_AUTHOR + " TEXT, " +
COLUMN_PAGES + " TEXT);";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
void addBook(String title, String author, String pages){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_TITLE, title);
cv.put(COLUMN_AUTHOR, author);
cv.put(COLUMN_PAGES, pages);
long result = db.insert(TABLE_NAME,null, cv);
if(result == -1){
Toast.makeText(context, "Save failed" + ("❌ "),Toast.LENGTH_LONG).show();
}else {
/* Toast.makeText(context, "saved " + ("✅"),Toast.LENGTH_LONG).show();*/
}
}
Cursor readAllData(){
String query = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
if(db != null){
cursor = db.rawQuery(query, null);
}
return cursor;
}
void updateData(String row_id, String title, String author, String pages){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_TITLE, title);
cv.put(COLUMN_AUTHOR, author);
cv.put(COLUMN_PAGES, pages);
long result = db.update(TABLE_NAME, cv, "_id=?", new String[]{row_id});
if(result == -1){
Toast.makeText(context, "Update failed" + ("❌ "),Toast.LENGTH_LONG).show();
}else {
Toast.makeText(context, "Updated" + ("✍uFE0F"),Toast.LENGTH_LONG).show();
}
}
void deleteOneRow(String row_id){
SQLiteDatabase db = this.getWritableDatabase();
long result = db.delete(TABLE_NAME, "_id=?", new String[]{row_id});
if(result == -1){
Toast.makeText(context, "Failed to Delete.", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "Deleted " + ("uD83DuDDD1uFE0F"),Toast.LENGTH_LONG).show();
}
}
void deleteAllData(){
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_NAME);
}
}
</code>
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
import androidx.annotation.Nullable;
class MyDatabaseHelper extends SQLiteOpenHelper {
private final Context context;
private static final String DATABASE_NAME = "myDataBase.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "my_library";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_TITLE = "book_title";
private static final String COLUMN_AUTHOR = "book_author";
private static final String COLUMN_PAGES = "book_pages";
MyDatabaseHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME +
" (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_TITLE + " TEXT, " +
COLUMN_AUTHOR + " TEXT, " +
COLUMN_PAGES + " TEXT);";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
void addBook(String title, String author, String pages){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_TITLE, title);
cv.put(COLUMN_AUTHOR, author);
cv.put(COLUMN_PAGES, pages);
long result = db.insert(TABLE_NAME,null, cv);
if(result == -1){
Toast.makeText(context, "Save failed" + ("❌ "),Toast.LENGTH_LONG).show();
}else {
/* Toast.makeText(context, "saved " + ("✅"),Toast.LENGTH_LONG).show();*/
}
}
Cursor readAllData(){
String query = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
if(db != null){
cursor = db.rawQuery(query, null);
}
return cursor;
}
void updateData(String row_id, String title, String author, String pages){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_TITLE, title);
cv.put(COLUMN_AUTHOR, author);
cv.put(COLUMN_PAGES, pages);
long result = db.update(TABLE_NAME, cv, "_id=?", new String[]{row_id});
if(result == -1){
Toast.makeText(context, "Update failed" + ("❌ "),Toast.LENGTH_LONG).show();
}else {
Toast.makeText(context, "Updated" + ("✍uFE0F"),Toast.LENGTH_LONG).show();
}
}
void deleteOneRow(String row_id){
SQLiteDatabase db = this.getWritableDatabase();
long result = db.delete(TABLE_NAME, "_id=?", new String[]{row_id});
if(result == -1){
Toast.makeText(context, "Failed to Delete.", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "Deleted " + ("uD83DuDDD1uFE0F"),Toast.LENGTH_LONG).show();
}
}
void deleteAllData(){
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_NAME);
}
}
I want to add the feature of exporting the database in a specific file and importing it again later Or exporting the database as CVS File in a specific file and importing it again.
New contributor
Ougmosta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.