Android:值不存储在表中

时间:2015-01-09 13:08:06

标签: android sqlite

在我的应用程序中,我有任务片段。我有3个字段,如员工姓名,项目名称和任务。我正在获取所有员工姓名和项目名称,并在微调器中显示它。因此,在选择该员工姓名和项目名称后,我正在获取员工ID和项目ID,并将这些值存储在任务表中。

创建按钮代码。

tsave.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String name = tname.getText().toString();
            task.setName(name);
            task.setEid(selectedManager);
            task.setPid(selectedProject);
            db.addTask(task);
            tname.setText(" ");

            Toast.makeText(ctx, "value added", Toast.LENGTH_SHORT).show();
        }
    });

DataBaseHandler类中的addTask方法

    public void addTask(Task task) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_TASKNAME, task.getName()); 
        values.put(KEY_TASKEID, task.getEid()); 
        values.put(KEY_TASKPID, task.getPid());
        // Inserting Row
        db.insert(TABLE_TASKS, null, values);
        db.close(); // Closing database connection
    }

此处员工ID的值是存储员工ID和项目ID两个字段。但是当我调试应用程序时,我可以在ContentValues中获取员工ID和项目ID。但我不知道为什么项目ID的值没有存储在表格中。

在以下行中,我可以看到正确的项目ID值

values.put(KEY_TASKPID, task.getPid());
db.insert(TABLE_TASKS, null, values);

请帮帮我。提前谢谢。

DatabaseHandler代码

// Database Name
private static final String DATABASE_NAME = "TaskManager";
//  table name
private static final String TABLE_EMPLOYEES = "Employee";
private static final String TABLE_PROJECTS = "project";
private static final String TABLE_TASKS = "Task";
// employee Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_DESIG = "designation";

//Project Table

private static final String KEY_ID1 = "id1";
private static final String KEY_NAME1 = "name1";
private static final String KEY_EID1 = "eid";
private static final String KEY_DESIG1 = "designation1";

// Task table

private static final String KEY_TASKID = "idtask";
private static final String KEY_TASKNAME = "taskname";
private static final String KEY_TASKEID = "taskeid";
private static final String KEY_TASKPID = "taskpid";
    @Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_EMPLOYEES_TABLE = "CREATE TABLE " + TABLE_EMPLOYEES + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_DESIG + " TEXT" + ")";
    String CREATE_PROJECTS_TABLE = "CREATE TABLE " + TABLE_PROJECTS + "("
            + KEY_ID1 + " INTEGER PRIMARY KEY," + KEY_NAME1 + " TEXT,"
            + KEY_EID1 + " TEXT," + KEY_DESIG1 + " TEXT" + ")";
    String CREATE_TASKS_TABLE = "CREATE TABLE " + TABLE_TASKS + "("
            + KEY_TASKID + " INTEGER PRIMARY KEY," + KEY_TASKNAME + " TEXT,"
            + KEY_TASKEID + " TEXT," + KEY_TASKPID + " TEXT" + ")";
    db.execSQL(CREATE_EMPLOYEES_TABLE);
    db.execSQL(CREATE_PROJECTS_TABLE);
    db.execSQL(CREATE_TASKS_TABLE);
}

0 个答案:

没有答案