【问题标题】:what is the best approach to save values in database?using android and sqlite在数据库中保存值的最佳方法是什么?使用 android 和 sqlite
【发布时间】:2013-06-22 18:15:44
【问题描述】:

我正在使用 android 和 sqlite 开发应用程序,这是我的数据库代码,请帮助我如何在数据库中保存值?我已经实现了该功能,但不知道它是正确的方法还是我可能需要使用类 gettersetters 保存值,并指出如何添加 id 类中的属性。

public class DatabaseHandler extends SQLiteOpenHelper {
private static final int db_Version = 1;
private static final String tb_Name = "category_List";
private static final String tb_Name_Task = "task_List";

private static final String db_Name = "taskInformation";

public DatabaseHandler(Context context, String name, CursorFactory factory, int version) {
    super(context, db_Name, factory, db_Version);
}

public void onCreate(SQLiteDatabase db) {

    try {
        db.execSQL(DatabaseAdapter.CREATE_TABLE);

        db.execSQL(DatabaseAdapter.CREATE_TABLE_TASK);
        //db.execSQL("DROP TABLE " + DatabaseAdapter.CREATE_TABLE_TASK);
        Log.d("table message", "table created");
    } catch (SQLiteException e) {
        e.printStackTrace();
    }
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + tb_Name);
    db.execSQL("DROP TABLE IF EXISTS " + tb_Name_Task);
    onCreate(db);
}

} 数据库适配器类:

public class DatabaseAdapter {

static final int db_Version = 1;

private static final String db_Name = "taskInformation";

private static final String tb_Name = "category_List";
public static final String KEY_ID ="id";
public static final String KEY_CATEGORY ="category";

static final String CREATE_TABLE = "CREATE TABLE " + tb_Name + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_CATEGORY + " TEXT NOT NULL UNIQUE" +")";

private static final String tb_Name_Task ="task_List";
public static final String KEY_TITLE ="title";
public static final String KEY_DESCRIPTION ="description";
public static final String KEY_TASKDATE ="task_Date";
public static final String KEY_TASKCATEGORY = "task_Category";
public static final String KEY_STATUS ="task_Status";

static final String CREATE_TABLE_TASK = "CREATE TABLE " + tb_Name_Task + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_TITLE + " TEXT NOT NULL UNIQUE, " + KEY_DESCRIPTION + " TEXT NOT NULL, " + KEY_TASKCATEGORY + " TEXT NOT NULL, " + KEY_TASKDATE + " TEXT NOT NULL, " + KEY_STATUS + " INTEGER NOT NULL" +")"; 

DatabaseAdapter dataAdapter;
public SQLiteDatabase db;
private final Context context;
private DatabaseHandler dbHandler;

public DatabaseAdapter(Context _context) {
    context = _context;
    dbHandler = new DatabaseHandler(context,db_Name, null, db_Version);
}

public DatabaseAdapter openToWrite() throws SQLiteException {
    db = dbHandler.getWritableDatabase();
    return this;
}
public DatabaseAdapter openToRead() throws SQLiteException
{
  db = dbHandler.getReadableDatabase();
  return this;

}

public void close() 
{
    db.close();
}
 public  SQLiteDatabase getDatabaseInstance()
 {
         return db;
 }

这个方法正确吗?或者我可能需要传递 myclass 实例并使用类的 getter 和 setter 保存值。

public void addTaskList(String title,String description,String date,String categorytask,Integer status)
{
    ContentValues values = new ContentValues();
    values.put(KEY_TITLE,title);
    values.put(KEY_DESCRIPTION,description);
    values.put(KEY_TASKCATEGORY,categorytask);
    values.put(KEY_TASKDATE,date);
    values.put(KEY_STATUS,status);
    try
    {long val = db.insertWithOnConflict(tb_Name_Task,null,values,SQLiteDatabase.CONFLICT_IGNORE);

    }
    catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

 }

请告诉我如何在 taskClass 中添加 id 变量,以便当我点击列表视图的任何列表项时,我可以获得该记录的“id”;

public class TaskClass {

    //int task_id; //may i need to add if yes then how pls elaborate
    int status;
    String description;
    String title;
    String taskCategory;
    String taskDate;
    public TaskClass() {
        //super();
        // TODO Auto-generated constructor stub
    }
    public TaskClass(int status, String description, String title, String taskCategory, String taskDate) {

        this.status = status;
        this.description = description;
        this.title = title;
        this.taskCategory = taskCategory;
        this.taskDate = taskDate;
    }
    public int getStatus() {
        return status;
    }
    public void setStatus(int status) {
        this.status = status;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getTaskCategory() {
        return taskCategory;
    }
    public void setTaskCategory(String taskCategory) {
        this.taskCategory = taskCategory;
    }
    public String getTaskDate() {
        return taskDate;
    }
    public void setTaskDate(String taskDate) {
        this.taskDate = taskDate;
    }
    @Override
    public String toString() {
        return "TaskClass [status=" + status + ", description=" + description + ", title=" + title + ", taskCategory=" + taskCategory + ", taskDate=" + taskDate + "]";
    }



}

【问题讨论】:

    标签: android database sqlite


    【解决方案1】:

    我看不到您从数据库中获取TaskClass 对象的位置。您需要实现一个查询数据库并返回TaskClass 列表供您使用的方法。然后,您可以在适配器中使用它。

    类似这样的:

    public List<TaskClass> getAllTasks() {
        List<TaskClass> tasks = new ArrayList<TaskClass>();
        Cursor cursor = db.query(tb_Name, null, null, null, null, null, "id asc");
        Log.d(TAG, "Getting tasks: " + cursor.getCount());
        while (cursor.moveToNext()) {
            TaskClass task = new TaskClass();
            task.setId(cursor.getLong(0));
            task.setTitle(cursor.getString(1));
            // Populate rest of fields accordingly
            tasks.add(task); 
        }
        Log.d(TAG, "Returning tasks: " + tasks);
        return tasks;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-19
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      • 2019-07-30
      • 2021-06-28
      • 2014-08-04
      • 1970-01-01
      相关资源
      最近更新 更多