在Android中无法更新SQLite中排而不是抛出任何错误 [英] Cannot update row in Sqlite in Android but not throwing any error

查看:149
本文介绍了在Android中无法更新SQLite中排而不是抛出任何错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绝对初学者到Android。现在,我开始与我的教程项目SQLite数据库工作。我试着插入和选择data.All正常工作。但现在我开始更新行作为我的第一次。但行实际上是不更新数据库。但它不是抛出错误。请有什么问题我的code?


  

我的数据库辅助类


 公共类DatabaseHelper扩展SQLiteOpenHelper {    私有静态最终诠释DATABASE_VERSION = 1;
    私有静态最后弦乐DATABASE_NAME =todo.db;
    私有静态最后弦乐TABLE_NAME =任务;
    私有静态最后弦乐COLUMN_ID =ID;
    私有静态最后弦乐COLUMN_DESCRIPTION =说明;
    私有静态最后弦乐COLUMN_DATE =日期;
    私有静态最后弦乐COLUMN_DONE =完成;
    私有静态最后弦乐CREATE_TABLE =CREATE TABLE+ TABLE_NAME +(+ COLUMN_ID +INTEGER PRIMARY KEY AUTOINCREMENT,+ COLUMN_DESCRIPTION +TEXT+
    COLUMN_DATE +号,+ COLUMN_DONE +布尔);
    SQLiteDatabase分贝;    公共DatabaseHelper(上下文的背景下)
    {
        超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
    }
    @覆盖
    公共无效的onCreate(SQLiteDatabase DB)
    {
        this.db = DB;
        db.execSQL(CREATE_TABLE);
    }    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
        查询字符串=DROP TABLE IF EXISTS+ TABLE_NAME;
        db.execSQL(查询);
        this.onCreate(DB);
    }    公共无效insertTask(任务任务)
    {
        DB = getWritableDatabase();
        ContentValues​​值=新ContentValues​​();
        values​​.put(COLUMN_DESCRIPTION,task.getDescription());
        values​​.put(COLUMN_DATE,task.getDate()的toString());
        values​​.put(COLUMN_DONE,Boolean.FALSE.toString());
        db.insert(TABLE_NAME,空,价值);
        db.close();
    }    公众的ArrayList<任务> getAllTask​​s()
    {
        ArrayList的<任务>项目=新的ArrayList<任务>();
        DB = getReadableDatabase();
        查询字符串=SELECT * FROM+ TABLE_NAME;
        光标光标= db.rawQuery(查询,NULL);
        如果(cursor.moveToFirst())
        {
            做{
                任务物品=新的任务();
                字符串日期= cursor.getString(2);
                日期parsedDate =新的日期();
                SimpleDateFormat的格式=新的SimpleDateFormat(MM / DD / YYYY);
                尝试{
                    parsedDate = format.parse(日期);
                }
                赶上(ParseException的E)
                {
                    parsedDate = NULL;
                }
                item.setId(cursor.getInt(0));
                item.setDescription(cursor.getString(1));
                item.setDate(parsedDate);
                item.setDone(通过Boolean.valueOf(cursor.getString(3)));
                items.add(项目);
            }
            而(cursor.moveToNext());
        }
        返回的物品;
    }    公共无效markAsDone(INT ID){
        DB = getWritableDatabase();
        ContentValues​​ updatedData =新ContentValues​​();
        updatedData.put(COLUMN_DONE,Boolean.TRUE);
        字符串,其中= COLUMN_ID +=+将String.valueOf(ID);
        db.update(TABLE_NAME,updatedData,其中,NULL);
    }
}


  

这是我如何更新片段级数据库。我的片段类


 公共类TaskListFragment扩展片段{
        私人DatabaseHelper dbHelper;
        私人TextView的taskTitle;
        私人的ListView taskListView;
        私人ArrayAdapter适配器;
        @Nullable
        @覆盖
        公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
            dbHelper =新DatabaseHelper(getActivity());
            查看查看= inflater.inflate(R.layout.task_list,集装箱,FALSE);
            taskTitle =(TextView中)view.findViewById(R.id.task_textview);
            taskListView =(ListView控件)view.findViewById(R.id.listViewTaskList);
            。整型= getArguments()调用getInt(类型);
            开关(类型){
                案例R.integer.task_list_all:
                    ArrayList的<任务>项= dbHelper.getAllTask​​s();
                    适配器=新TaskListAdapter(getActivity(),项目);
                    taskListView.setAdapter(适配器);
                    taskTitle.setText(所有任务);
                    打破;
            }
            taskListView.setOnItemLongClickListener(新AdapterView.OnItemLongClickListener(){
                @覆盖
                公共布尔onItemLongClick(适配器视图<>母公司,观景,INT位置,长的id){
                    INT TAGID = Integer.valueOf(view.getTag()的toString());
                    showOptionDialog(TAGID);
                    返回true;
                }
            });
            返回视图。
        }        公共无效showOptionDialog(最终诠释ID)
        {
            LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
            查看查看= layoutInflater.inflate(R.layout.row_option_dialog,NULL);            最后AlertDialog alertDialog =新AlertDialog.Builder(getActivity())创建()。
            按钮doneBtn =(按钮)view.findViewById(R.id.btn_row_option_done);
            doneBtn.setOnClickListener(新View.OnClickListener(){
                @覆盖
                公共无效的onClick(视图v){
                    dbHelper.markAsDone(ID);
                    Toast.makeText(getActivity()getBaseContext()标记为完成,Toast.LENGTH_SHORT).show();
//这是showig敬酒messagee标记为完成。但是数据实际上没有更新。请帮我为什么?
                }
            });
            按钮editBtn =(按钮)view.findViewById(R.id.btn_row_option_edit);
            editBtn.setOnClickListener(新View.OnClickListener(){
                @覆盖
                公共无效的onClick(视图v){                }
            });
            按钮deleteBtn =(按钮)view.findViewById(R.id.btn_row_option_delete);
            deleteBtn.setOnClickListener(新View.OnClickListener(){
                @覆盖
                公共无效的onClick(视图v){                }
            });
            按钮cancelBtn =(按钮)view.findViewById(R.id.btn_row_option_cancel);
            cancelBtn.setOnClickListener(新View.OnClickListener(){
                @覆盖
                公共无效的onClick(视图v){                }
            });
            alertDialog.setView(视图);
            alertDialog.show();
        }
    }

我使用markAsDone方法在我的片段更新一行。请有什么问题我的code?我不知道要解决它,因为它不抛出任何错误。是我更新code正确吗?


  

我只在logcat中得到了这个


  10月1日至25日:09:00.177 128-336 /? W / genymotion_audio:out_write()限制睡眠时间为26780 23219
10月1日至25日:09:02.509 128-336 /? W / genymotion_audio:out_write()限制睡眠时间为31155 23219
10月1日至25日:09:04.337 2622年至2622年/? I / dalvikvm:找不到方法android.content.res.Resources.getDrawable,从方法android.support.v7.widget.ResourcesWrapper.getDrawable引用
10月1日至25日:09:04.337 2622年至2622年/? W / dalvikvm:VFY:无法解析虚方法399:Landroid /内容/ RES /资源; .getDrawable(ILandroid /内容/ RES /资源$主题;)Landroid /图形/绘制/可绘制;
10月1日至25日:09:04.341 2622年至2622年/? D / dalvikvm:VFY:在更换运code 0x6e为0x0002
10月1日至25日:09:04.341 2622年至2622年/? I / dalvikvm:找不到方法android.content.res.Resources.getDrawableForDensity,从方法android.support.v7.widget.ResourcesWrapper.getDrawableForDensity引用
10月1日至25日:09:04.341 2622年至2622年/? W / dalvikvm:VFY:无法解析虚方法401:Landroid /内容/ RES /资源; .getDrawableForDensity(IILandroid /内容/ RES /资源$主题;)Landroid /图形/绘制/可绘制;
10月1日至25日:09:04.341 2622年至2622年/? D / dalvikvm:VFY:在更换运code 0x6e为0x0002
10月1日至25日:09:04.349 2622年至2626年/? D / dalvikvm:GC_CONCURRENT释放1457K,20%免费6388K / 7980K,暂停1毫秒+ 1毫秒,7毫秒总
10月1日至25日:09:08.705 128-336 /? W / genymotion_audio:out_write()限制睡眠时间30339至23219
10月1日至25日:09:14.709 2622年至2622年/? W / EGL_genymotion:eglSurfaceAttrib没有实现
01-25 10:09:19.653 407-991 /? W / InputMethodManagerService:窗口已经集中,忽视聚焦增益:com.android.internal.view.IInputMethodClient$Stub$Proxy@533e36b4属性= NULL,令牌= android.os.BinderProxy@53391868
01-25 10:09:21.509 2622年至2622年/? W / EGL_genymotion:eglSurfaceAttrib没有实现
01-25 10:09:22.957 128-336 /? W / genymotion_audio:out_write()限制睡眠时间61269至23219
01-25 10:09:22.977 128-336 /? W / genymotion_audio:out_write()限制睡眠时间52879至23219
10月1日至25日:09:23.005 128-336 /? W / genymotion_audio:out_write()限制睡眠时间44489至23219
10月1日至25日:09:23.029 128-336 /? W / genymotion_audio:out_write()限制睡眠时间36099至23219

当我登录更新语句返回值。它返回1


解决方案

我得到了答案。现在我更新行的布尔值数据库中像这样markAsDone方法DatabaseHelper。

  updatedData.put(COLUMN_DONE,Boolean.TRUE);

然后我把它改为

  updatedData.put(COLUMN_DONE,将String.valueOf(Boolean.TRUE));

我需要解析的布尔值的字符串。

I am absolute beginner to Android. Now I start working with Sqlite database in my tutorial project. I tried insert and selecting data.All worked fine. But now I start updating row as my first time. But row is actually not updated in database. But it is not throwing error. Please what is wrong with my code?

My database helper class

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "todo.db";
    private static final String TABLE_NAME = "task";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_DESCRIPTION = "description";
    private static final String COLUMN_DATE ="date";
    private static final String COLUMN_DONE = "done";
    private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+" ("+COLUMN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+COLUMN_DESCRIPTION+" TEXT,"+
    COLUMN_DATE+" DATE,"+COLUMN_DONE+" BOOLEAN)";
    SQLiteDatabase db;

    public DatabaseHelper(Context context)
    {
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db)
    {
        this.db = db;
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String query = "DROP TABLE IF EXISTS "+TABLE_NAME;
        db.execSQL(query);
        this.onCreate(db);
    }

    public  void insertTask(Task task)
    {
        db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_DESCRIPTION,task.getDescription());
        values.put(COLUMN_DATE,task.getDate().toString());
        values.put(COLUMN_DONE,Boolean.FALSE.toString());
        db.insert(TABLE_NAME, null, values);
        db.close();
    }

    public ArrayList<Task> getAllTasks()
    {
        ArrayList<Task> items = new ArrayList<Task>();
        db = getReadableDatabase();
        String query = "SELECT * FROM "+TABLE_NAME;
        Cursor cursor = db.rawQuery(query,null);
        if(cursor.moveToFirst())
        {
            do{
                Task item = new Task();
                String date = cursor.getString(2);
                Date parsedDate = new Date();
                SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
                try{
                    parsedDate = format.parse(date);
                }
                catch (ParseException e)
                {
                    parsedDate = null;
                }
                item.setId(cursor.getInt(0));
                item.setDescription(cursor.getString(1));
                item.setDate(parsedDate);
                item.setDone(Boolean.valueOf(cursor.getString(3)));
                items.add(item);
            }
            while (cursor.moveToNext());
        }
        return items;
    }

    public void markAsDone(int id){
        db = getWritableDatabase();
        ContentValues updatedData = new ContentValues();
        updatedData.put(COLUMN_DONE, Boolean.TRUE);
        String where = COLUMN_ID+" = "+String.valueOf(id);
        db.update(TABLE_NAME,updatedData,where,null);
    }
}

This is how I update database in fragment class. My fragment class

    public class TaskListFragment extends Fragment {
        private DatabaseHelper dbHelper;
        private TextView taskTitle;
        private ListView taskListView;
        private ArrayAdapter adapter;
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            dbHelper = new DatabaseHelper(getActivity());
            View view= inflater.inflate(R.layout.task_list, container, false);
            taskTitle = (TextView)view.findViewById(R.id.task_textview);
            taskListView = (ListView)view.findViewById(R.id.listViewTaskList);
            int type = getArguments().getInt("type");
            switch (type){
                case R.integer.task_list_all:
                    ArrayList<Task> items = dbHelper.getAllTasks();
                    adapter = new TaskListAdapter(getActivity(),items);
                    taskListView.setAdapter(adapter);
                    taskTitle.setText("All tasks");
                    break;
            }
            taskListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                    int tagId = Integer.valueOf(view.getTag().toString());
                    showOptionDialog(tagId);
                    return true;
                }
            });
            return view;
        }

        public void showOptionDialog(final int id)
        {
            LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
            View view = layoutInflater.inflate(R.layout.row_option_dialog, null);

            final AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
            Button doneBtn = (Button)view.findViewById(R.id.btn_row_option_done);
            doneBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dbHelper.markAsDone(id);
                    Toast.makeText(getActivity().getBaseContext(),"Marked as done",Toast.LENGTH_SHORT).show();
//This is showig toast messagee "Mark as done". But data is not actually updated. Please help me why?
                }
            });
            Button editBtn = (Button)view.findViewById(R.id.btn_row_option_edit);
            editBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            Button deleteBtn = (Button)view.findViewById(R.id.btn_row_option_delete);
            deleteBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            Button cancelBtn = (Button)view.findViewById(R.id.btn_row_option_cancel);
            cancelBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            alertDialog.setView(view);
            alertDialog.show();
        }
    }

I am updating row using markAsDone method in my Fragment. Please what is wrong with my code? I have no idea to solve it cause it is not throwing any error. Is my update code correct please?

I only got this in logcat

01-25 10:09:00.177 128-336/? W/genymotion_audio: out_write() limiting sleep time 26780 to 23219
01-25 10:09:02.509 128-336/? W/genymotion_audio: out_write() limiting sleep time 31155 to 23219
01-25 10:09:04.337 2622-2622/? I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable
01-25 10:09:04.337 2622-2622/? W/dalvikvm: VFY: unable to resolve virtual method 399: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
01-25 10:09:04.341 2622-2622/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
01-25 10:09:04.341 2622-2622/? I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity
01-25 10:09:04.341 2622-2622/? W/dalvikvm: VFY: unable to resolve virtual method 401: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
01-25 10:09:04.341 2622-2622/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
01-25 10:09:04.349 2622-2626/? D/dalvikvm: GC_CONCURRENT freed 1457K, 20% free 6388K/7980K, paused 1ms+1ms, total 7ms
01-25 10:09:08.705 128-336/? W/genymotion_audio: out_write() limiting sleep time 30339 to 23219
01-25 10:09:14.709 2622-2622/? W/EGL_genymotion: eglSurfaceAttrib not implemented
01-25 10:09:19.653 407-991/? W/InputMethodManagerService: Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@533e36b4 attribute=null, token = android.os.BinderProxy@53391868
01-25 10:09:21.509 2622-2622/? W/EGL_genymotion: eglSurfaceAttrib not implemented
01-25 10:09:22.957 128-336/? W/genymotion_audio: out_write() limiting sleep time 61269 to 23219
01-25 10:09:22.977 128-336/? W/genymotion_audio: out_write() limiting sleep time 52879 to 23219
01-25 10:09:23.005 128-336/? W/genymotion_audio: out_write() limiting sleep time 44489 to 23219
01-25 10:09:23.029 128-336/? W/genymotion_audio: out_write() limiting sleep time 36099 to 23219

When I log return value of update statement. It is returning 1

解决方案

I got the answer. Now I am updating boolean value of row in database like this in markAsDone method in DatabaseHelper.

updatedData.put(COLUMN_DONE, Boolean.TRUE);

Then I just changed it to

updatedData.put(COLUMN_DONE, String.valueOf(Boolean.TRUE));

I needed to parse boolean value to string.

这篇关于在Android中无法更新SQLite中排而不是抛出任何错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆