使用android和sqlite在数据库中保存值的最佳方法是什么?

时间:2013-06-22 09:45:27

标签: android database sqlite

我使用android和sqlite开发应用程序这里是我的数据库代码请帮我如何在数据库中保存值?我已经实现了这个功能但dnt知道它是正确的方法还是我需要使用 getter setters 来保存值,并指出如何添加 id < / strong>类中的属性。

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);
}

} databaseAdapter类:

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 变量,以便当我点击listview的任何listitem时,我可以获得该记录的“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 + "]";
    }



}

1 个答案:

答案 0 :(得分: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;
}