什么是保存在数据库中的值的最佳方法?采用了android和SQLite [英] what is the best approach to save values in database?using android and sqlite
问题描述
I M开发使用Android应用程序和SQLite这里是我的code数据库请帮助我如何值保存在数据库中?我已经实现的功能,但二硝基甲苯知道是不是正确的方法或可能我需要使用类的getter 和制定者以保存值,也指出其如何添加 ID 的类属性。
公共类数据库处理器扩展SQLiteOpenHelper {
私有静态最终诠释db_Version = 1;
私有静态最后弦乐tb_Name =category_List;
私有静态最后弦乐tb_Name_Task =task_List;私有静态最后弦乐DB_NAME =taskInformation;公共数据库处理器(上下文的背景下,字符串名称,CursorFactory工厂,诠释的版本){
超(背景下,DB_NAME,工厂,db_Version);
}公共无效的onCreate(SQLiteDatabase DB){ 尝试{
db.execSQL(DatabaseAdapter.CREATE_TABLE); db.execSQL(DatabaseAdapter.CREATE_TABLE_TASK);
//db.execSQL(\"DROP表+ DatabaseAdapter.CREATE_TABLE_TASK);
Log.d(信息表,表创建);
}赶上(SQLiteException E){
e.printStackTrace();
}
}公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
db.execSQL(DROP TABLE IF EXISTS+ tb_Name);
db.execSQL(DROP TABLE IF EXISTS+ tb_Name_Task);
的onCreate(DB);
}
}
databaseAdapter类:
公共类DatabaseAdapter {静态最终诠释db_Version = 1;私有静态最后弦乐DB_NAME =taskInformation;私有静态最后弦乐tb_Name =category_List;
公共静态最后弦乐KEY_ID =ID;
公共静态最后弦乐KEY_CATEGORY =类别;静态最后弦乐CREATE_TABLE =CREATE TABLE+ tb_Name +(+ KEY_ID +INTEGER PRIMARY KEY AUTOINCREMENT,+ KEY_CATEGORY +TEXT NOT NULL UNIQUE+);私有静态最后弦乐tb_Name_Task =task_List;
公共静态最后弦乐KEY_TITLE =称号;
公共静态最后弦乐KEY_DESCRIPTION =说明;
公共静态最后弦乐KEY_TASKDATE =task_Date;
公共静态最后弦乐KEY_TASKCATEGORY =task_Category;
公共静态最后弦乐KEY_STATUS =task_Status;静态最后弦乐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 +文本中未NULL,+ KEY_TASKDATE +TEXT NOT NULL,+ KEY_STATUS +INTEGER NOT NULL+);DatabaseAdapter DataAdapter的;
公共SQLiteDatabase分贝;
私人最终上下文的背景下;
私人数据库处理器dbHandler;公共DatabaseAdapter(上下文_context){
上下文= _context;
dbHandler =新数据库处理器(背景下,DB_NAME,空,db_Version);
}公共DatabaseAdapter openToWrite()抛出SQLiteException {
DB = dbHandler.getWritableDatabase();
返回此;
}
公共DatabaseAdapter openToRead()抛出SQLiteException
{
DB = dbHandler.getReadableDatabase();
返回此;}公共无效的close()
{
db.close();
}
公共SQLiteDatabase getDatabaseInstance()
{
返回分贝;
}
就是这个方法正确吗?也可能我需要通过MyClass的实例,并使用类的getter和setter方法保存的值。
公共无效addTaskList(字符串名称,描述字符串,字符串日期,字符串categorytask,整数状态)
{
ContentValues值=新ContentValues();
values.put(KEY_TITLE,职称);
values.put(KEY_DESCRIPTION,说明);
values.put(KEY_TASKCATEGORY,categorytask);
values.put(KEY_TASKDATE,日期);
values.put(KEY_STATUS,地位);
尝试
{长VAL = db.insertWithOnConflict(tb_Name_Task,空,价值观,SQLiteDatabase.CONFLICT_IGNORE); }
赶上(例外五){
// TODO:处理异常
e.printStackTrace();
} }
请告诉我如何添加 ID 在taskClass变量,这样当我在列表视图中的任何列表项点击我能得到这个纪录的身份证;
公共类TaskClass { // INT TASK_ID; //可能我需要如果是加那怎么请精心制作
INT状态;
字符串描述;
字符串称号;
串taskCategory;
串taskDate;
公共TaskClass(){
//超();
// TODO自动生成构造函数存根
}
公共TaskClass(INT状态,描述字符串,字符串title,字符串taskCategory,字符串taskDate){ this.status =状态;
this.description =描述;
this.title =称号;
this.taskCategory = taskCategory;
this.taskDate = taskDate;
}
公众诠释的getStatus(){
返回状态;
}
公共无效setStatus(INT状态){
this.status =状态;
}
公共字符串getDescription(){
返回描述;
}
公共无效setDescription(字符串描述){
this.description =描述;
}
公共字符串的getTitle(){
返回称号;
}
公共无效的setTitle(字符串名称){
this.title =称号;
}
公共字符串getTaskCategory(){
返回taskCategory;
}
公共无效setTaskCategory(字符串taskCategory){
this.taskCategory = taskCategory;
}
公共字符串getTaskDate(){
返回taskDate;
}
公共无效setTaskDate(字符串taskDate){
this.taskDate = taskDate;
}
@覆盖
公共字符串的toString(){
返回TaskClass [状态=+状态+,说明=+说明+,标题=+标题+,taskCategory =+ taskCategory +,taskDate =+ taskDate +];
}}
我不知道你在哪里得到你的 TaskClass
对象从数据库中。您需要实现查询数据库,并返回 TaskClass名单
供你使用的方法。然后,您可以在适配器中使用。
事情是这样的:
公开名单< TaskClass> getAllTasks(){
清单< TaskClass>任务=新的ArrayList< TaskClass>();
光标光标= db.query(tb_Name,NULL,NULL,NULL,NULL,NULL,ID ASC);
Log.d(TAG,获得任务:+ cursor.getCount());
而(cursor.moveToNext()){
TaskClass任务=新TaskClass();
task.setId(cursor.getLong(0));
task.setTitle(cursor.getString(1));
//因此填充字段休息
tasks.add(任务);
}
Log.d(TAG,返回任务:+任务);
返回任务;
}
i m developing app using android and sqlite here is my code for database pls help me how to save values in database? i have implemented the function but dnt know is it correct method or may i need to save values using class getter and setters and also point it how to add id attribute in class.
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 Class:
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;
}
is this method correct ? or may i need to pass myclass instance and save values using getters and setters of class.
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();
}
}
pls tell me how to add id variable in taskClass so that when i click on any listitem of listview i can get "id" of that record;
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 + "]";
}
}
I don't see where you are getting your TaskClass
objects out of the database. You need to implement a method that queries the database and returns a list of TaskClass
for you to use. You can then use this in your adapter.
Something like this:
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;
}
这篇关于什么是保存在数据库中的值的最佳方法?采用了android和SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!