在sqlite中插入不起作用:(

时间:2017-05-13 16:47:57

标签: android sqlite android-sqlite

我在没有错误的情况下为数据添加了一行,但不明白为什么android studio上的每一次软件运行都不再存在数据。任何人都可以帮助我吗?

创建数据库

String sqlTask="CREATE TABLE [tblTask] (" +
                    "[TaskID] VARCHAR(15)  NOT NULL PRIMARY KEY," +
                    "[Comment] TEXT  NULL," +
                    "[Date] TEXT  NULL," +
                    "[DateTime] TEXT  NULL," +
                    "[prioritize] INTEGER  NULL," +
                    "[TaskCategoryID] VARCHAR(15)  NULL" +
                    ")";
            database.execSQL(sqlTask);

            String sqlBook="CREATE TABLE [tblTaskCategory] (" +
                    "[TaskCategoryID] VARCHAR(15)  NULL PRIMARY KEY," +
                    "[TaskCategoryName] TEXT  NULL," +
                    "[InActive] BOOLEAN  NULL" +
                    ")";
            database.execSQL(sqlBook);

            String sqlTrigger="create trigger fk_insert_task before insert on tblTask "
                    +" for each row "
                    +" begin "
                    +" select raise(rollback,'them du lieu tren bang tblBooks bi sai') "
                    +" where (select TaskID from tblTask where TaskID=new.authorid) is null ;"
                    +" end;";
            database.execSQL(sqlTrigger);

插入数据

if (database!=null){
                    database.beginTransaction();
                    try {
                        ContentValues values = new ContentValues();
                        values.put("TaskID",edt_TaskID.getText().toString());
                        values.put("Comment",edt_Comment.getText().toString());
                        values.put("Date",edt_Date.getText().toString());
                        values.put("DateTime",edt_Time.getText().toString());
                        //values.put("prioritize",Integer.parseInt(edt_Prioritize.getText()+""));
                        values.put("prioritize",edt_Prioritize.getText().toString());
                        values.put("TaskCategoryID",edt_TaskCategoryID.getText().toString());
                        database.insert("tblTask", null, values);
                        database.setTransactionSuccessful();
                        Toast.makeText(MainActivity.this, "Cập nhật thành công", Toast.LENGTH_LONG).show();
                        inforData d=new inforData();
                        d.setField1(edt_TaskID.getText().toString());
                        d.setField2(edt_Comment.getText().toString());
                        d.setField3(edt_Date.getText().toString());
                        d.setField4(edt_Time.getText().toString());
                        d.setField5(edt_Prioritize.getText().toString());
                        d.setField6(edt_TaskCategoryID.getText().toString());
                        listTask.add(d);
                        adapter.notifyDataSetChanged();

                    }
                    catch(Exception ex)
                    {
                        Toast.makeText(MainActivity.this, ex.getMessage(), Toast.LENGTH_LONG).show();
                    }
                    finally {
                        database.endTransaction();

                    }
                }

加载数据

if(database!=null)
    {
        Cursor cursor=database.query("tblTask", null, null, null, null, null, null);
        cursor.moveToFirst();
        while(cursor.isAfterLast()==false)
        {
            inforData d=new inforData();
            d.setField1(cursor.getString(0));
            d.setField2(cursor.getString(1));
            d.setField3(cursor.getString(2));
            d.setField4(cursor.getString(3));
            d.setField5(cursor.getString(4));
            d.setField6(cursor.getString(5));
            listTask.add(d);
            cursor.moveToNext();
        }
        cursor.close();
    }
    adapter=new MyArrayAdapter(this, R.layout.activity_my_task_layout,listTask);
    lv_Trans.setAdapter(adapter);
    adapter.notifyDataSetChanged();

我搜索了2天但没有发现任何错误。我希望每个人都帮助我

0 个答案:

没有答案