SQLiteException:表X没有名为Y的列

时间:2014-08-06 18:54:26

标签: java android sql

我正在尝试将SQLDatabase用作类Todo的对象(int id,String name,String date,String note)。通过以下代码创建帮助程序后:

package com.example.todoistodo;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class TodoSQLiteHelper extends SQLiteOpenHelper {

    public TodoSQLiteHelper(Context context) {
        // Databse: todos_db, Version: 1
        super(context, "todos_db", null, 1);
    }

    // todos
    //      _id     - key
    //      todo    - todo text
    //      date    - todo date
    //      note    - todo note

    @Override
    public void onCreate(SQLiteDatabase db) {
        // Execute create table SQL
        db.execSQL("CREATE TABLE todos (_id INTEGER PRIMARY KEY AUTOINCREMENT , todo TEXT NOT NULL , date TEXT NOT NULL , note TEXT NOT NULL);");
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
        db.execSQL("DROP TABLE IF EXISTS todos");
        onCreate(db);
    }

}

..并创建查询数据库的对象:

package com.example.todoistodo;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class TodoDAO {

    private SQLiteDatabase db;
    private TodoSQLiteHelper dbHelper;

    public TodoDAO(Context context) {
        dbHelper = new TodoSQLiteHelper(context);
        db = dbHelper.getWritableDatabase();
    }

    public void close() {
        db.close();
    }

    public void createTodo(String todoName, String todoDate, String todoNote) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("todo", todoName);
        contentValues.put("date", todoDate);
        contentValues.put("note", todoNote);
        db.insert("todos", null, contentValues); //lane 30
    }

    public void deleteTodo(int todoId) {
        db.delete("todos", "_id = " + todoId, null);
    }

    //get every single Todo
    public List<Todo> getTodos() {
        List<Todo> todoList = new ArrayList<Todo>();

        String[] tableColumns = new String[] {"_id","todo","date","note"};

        Cursor cursor = db.query("todos", null, null, null, null, null, null); // null as second param instead of tableColumns
        cursor.moveToFirst();

        while (!cursor.isAfterLast()) {
            Todo todo = new Todo();

            todo.setId(cursor.getInt(0));
            todo.setName(cursor.getString(1));
            todo.setDate(cursor.getString(2));
            todo.setNote(cursor.getString(3));

            todoList.add(todo);

            cursor.moveToNext();
        }

        return todoList;
    }

}

我在db.insert(“todos”,null,contentValues)中得到一个SQLiteException; createTodo方法。 我的代码出了什么问题? 这是相关的日志:

08-06 22:43:34.902: I/SqliteDatabaseCpp(23902): sqlite returned: error code = 1, msg = table todos has no column named date, db=/data/data/com.example.todoistodo/databases/todos_db
08-06 22:43:34.922: E/SQLiteDatabase(23902): Error inserting date=Tomorrow  note= todo=Sleep out
08-06 22:43:34.922: E/SQLiteDatabase(23902): android.database.sqlite.SQLiteException: table todos has no column named date: , while compiling: INSERT INTO todos(date,note,todo) VALUES (?,?,?)
08-06 22:43:34.922: E/SQLiteDatabase(23902):    at com.example.todoistodo.TodoDAO.createTodo(TodoDAO.java:30)

谢谢!

1 个答案:

答案 0 :(得分:0)

这一行出了点问题:

db.execSQL("CREATE TABLE todos (_id INTEGER PRIMARY KEY AUTOINCREMENT , todo TEXT NOT NULL , date TEXT NOT NULL , note TEXT NOT NULL);");

应该没有&#39 ;;&#39;

db.execSQL("CREATE TABLE todos (_id INTEGER PRIMARY KEY AUTOINCREMENT , todo TEXT NOT NULL , date TEXT NOT NULL , note TEXT NOT NULL)");