- Jan 11, 2024
- 2
- 0
- 1
I'm doing a lab session on SQLite but there's no error shows in my databaselper.java but still when i click the button view entry i can view any data.Can anyone let me know what wrong with my coding and what i should do?
package com.example.sqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mylist.db";
private static final String TABLE_NAME = "mylist_data";
private static final String COL1 = "ID";
public static final String COL2 = "ITEM1";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 + " TEXT)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// In a real application, you might want to perform data migration or other actions here.
// For simplicity, we are just dropping the table and recreating it.
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData (String item1) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item1);
try {
long result = db.insert(TABLE_NAME, null, contentValues);
return result != -1;
} catch (SQLException e) {
// Handle the exception (e.g., log it or show an error message)
e.printStackTrace();
return false;
}
}
public Cursor getListContents() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
// Don't forget to close the Cursor when you're done using it
// data.close(); // Uncomment this line if you are not returning the Cursor directly
return data;
}
}
package com.example.sqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mylist.db";
private static final String TABLE_NAME = "mylist_data";
private static final String COL1 = "ID";
public static final String COL2 = "ITEM1";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 + " TEXT)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// In a real application, you might want to perform data migration or other actions here.
// For simplicity, we are just dropping the table and recreating it.
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData (String item1) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item1);
try {
long result = db.insert(TABLE_NAME, null, contentValues);
return result != -1;
} catch (SQLException e) {
// Handle the exception (e.g., log it or show an error message)
e.printStackTrace();
return false;
}
}
public Cursor getListContents() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
// Don't forget to close the Cursor when you're done using it
// data.close(); // Uncomment this line if you are not returning the Cursor directly
return data;
}
}