Android SQLite DB数据类型不匹配

时间:2016-04-20 06:06:34

标签: android sqlite android-sqlite sqliteopenhelper

我试图使用SQLite数据库在我的应用中存储一些待办事项数据。当我试图输入一些数据时失败了。我搜索过StackOverflow,更改数据库或表名似乎对我的情况不起作用。 logcat中的错误是

  

语句在5处中止:[INSERT INTO Todos(完整,描述,标题,截止日期)VALUES(?,?,?,?)]数据类型不匹配

我的SQLHelper类:

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    // Database name
    private static final String DATABASE_NAME = "todoManager";
    // Todos table name
    private static final String TABLE_TODOS = "Todos";
    // Todos table columns names
    private static final String KEY_TITLE = "Title";
    private static final String KEY_DDL = "Deadline";
    private static final String KEY_DES = "Description";
    private static final String KEY_COM = "Complete";

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        //String for creating table TODOS.
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_TODOS + " ("
                + KEY_TITLE + " TEXT, "
                + KEY_DDL + " TEXT, "
                + KEY_DES + " TEXT, "
                + KEY_COM + " TEXT)";
        //Run string
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //Drop old table, if it existed.
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_TODOS);
        //Create tables again.
        onCreate(db);
    }

    public void delete(TodoItem item) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_TODOS, KEY_TITLE + " = " + item.getTITLE(), null);
    }

    public void addTodo(TodoItem item) {
        SQLiteDatabase db = this.getWritableDatabase();

        //Prepare and save all values for insertion.
        ContentValues values = new ContentValues();
        values.put(KEY_TITLE, item.getTITLE());
        values.put(KEY_DDL, item.getDEADLINE());
        values.put(KEY_DES, item.getDESCRIPTION());
        values.put(KEY_COM, item.getCOMPLETED());

        db.insert(TABLE_TODOS, null, values);
        db.close();
    }

    public void updateTodo(TodoItem item) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        //Update rows
        values.put(KEY_TITLE, item.getTITLE());
        values.put(KEY_DDL, item.getDEADLINE());
        values.put(KEY_DES, item.getDESCRIPTION());
        values.put(KEY_COM, item.getCOMPLETED());

        db.update(TABLE_TODOS, values, KEY_TITLE + " = ?", new String[]{String.valueOf(item.getTITLE())});

    }

    public ArrayList<TodoItem> getAllTodos() {
        ArrayList<TodoItem> list = new ArrayList<TodoItem>();
        SQLiteDatabase db = this.getWritableDatabase();

        //Select all query
        String query = "SELECT * FROM " + TABLE_TODOS;
        Cursor cursor = db.rawQuery(query, null);

        //Add to list
        if(cursor.moveToFirst()) {
            do {
                TodoItem todo = new TodoItem(cursor.getString(0), cursor.getString(1),
                        cursor.getString(2),cursor.getString(3),cursor.getString(4));
                todo.setTITLE(cursor.getString(0));
                list.add(todo);
            } while (cursor.moveToNext());
        }
        return list;
    }

}

我的TodoItem类(我没有把最后一个字段放在我的数据库中,可以吗?):

public class TodoItem {
private String TITLE = TodoListProvider.TaskEntry.COL_TITLE;
private String DESCRIPTION = TodoListProvider.TaskEntry.COL_DESCRIPTION;
private String DEADLINE = TodoListProvider.TaskEntry.COL_DEADLINE;
private String COMPLETED = TodoListProvider.TaskEntry.COL_COMPLETED;
private String TIME_CREATED = TodoListProvider.TaskEntry.COL_TIME_CREATED;

    public TodoItem(String tt, String dl,String dt,String cm,String tc) {
        this.TITLE= tt;
        this.DESCRIPTION = dt;
        this.DEADLINE = dl;
        this.COMPLETED = cm;
        this.TIME_CREATED = tc;
    }
public String getTITLE() {
    return TITLE;
}

public void setTITLE(String TITLE) {
    this.TITLE = TITLE;
}

public String getDESCRIPTION() {
    return DESCRIPTION;
}

public void setDESCRIPTION(String DETAILS) {
    this.DESCRIPTION = DETAILS;
}

public String getDEADLINE() {
    return DEADLINE;
}

public void setDEADLINE(String DEADLINE) {
    this.DEADLINE = DEADLINE;
}

public String getCOMPLETED() {
    return COMPLETED;
}

public void setCOMPLETED(String COMPLETED) {
    this.COMPLETED = COMPLETED;
}

public String getTIME_CREATED() {
    return TIME_CREATED;
}

public void setTIME_CREATED(String TIME_CREATED) {
    this.TIME_CREATED = TIME_CREATED;
}

}

这就是我在片段中调用数据库的方式(通过按钮点击):

public class EditItemFragment extends Fragment {
private View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_edit_item, container, false);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    EditText title = (EditText) view.findViewById(R.id.editTitle);
    EditText deadline = (EditText) view.findViewById(R.id.editDDL);
    EditText description = (EditText) view.findViewById(R.id.editDescription);
    EditText complete = (EditText) view.findViewById(R.id.editComplete);

    String Title = title.getText().toString();
    String Deadline = deadline.getText().toString();
    String Description = description.getText().toString();
    String Complete = complete.getText().toString();

    final TodoItem item = new TodoItem(Title, Deadline, Description, Complete, "now");

    Button saveBtn = (Button) getActivity().findViewById(R.id.btnSave);
    saveBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            DatabaseHelper helper = new DatabaseHelper(getActivity());
            helper.addTodo(item);
        }
    });

如果你能给我一些关于这个的想法,万分感谢你!

3 个答案:

答案 0 :(得分:0)

我已经执行了你的代码,它基本上工作正常,可写的数据库无法在你的应用程序中工作

所以我在你的代码中做了一些修改,见下文

添加这些代码 DatabaseHelper.class:

  private static DatabaseHelper instance;
    public static synchronized DatabaseHelper getHelper(Context context) {
        if (instance == null)
            instance = new DatabaseHelper(context);
        return instance;
    }


****`Add these In Ur Fragment Activity:`****


//Declare in private
    DatabaseHelper helper;
    SQLiteDatabase database;

    //In on createView:

      if (helper == null) {
        helper = DatabaseHelper.getHelper(getActivity());
        database = helper.getWritableDatabase();
    }

清单文件中的权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

注意:每当你在dbhelper类的ur代码中添加/修改更改时:请卸载并重新安装app。

答案 1 :(得分:0)

好的,我认为你有错误的onCreate()方法。请尝试使用next:

private static final String  KEY_ID = "_id";

@Override
    public void onCreate(SQLiteDatabase db) {
        //String for creating table TODOS.
        String sql = "CREATE TABLE " + TABLE_TODOS + " ("
                + KEY_ID + " INTEGER PRIMARY KEY," +
                + KEY_TITLE + " TEXT,"
                + KEY_DDL + " TEXT,"
                + KEY_DES + " TEXT,"
                + KEY_COM + " TEXT )";
        //Run string
        db.execSQL(sql);
    }  

KEY_ID - 是必需参数。

答案 2 :(得分:-1)

如果要自动重新创建方案,请增加DATABASE_VERSION

private static final int DATABASE_VERSION = 2;

如果您不必卸载该应用以重新创建该方案。