0

据我所知,似乎它以错误的顺序传递了 addTask 内容值,但我不明白为什么。addUser 方法以相同的方式实现并且工作正常。

我得到的错误和所有相关代码都在这里。任何帮助表示赞赏。

04-05 23:09:37.465: I/SqliteDatabaseCpp(606): sqlite 返回: 错误代码 = 1, msg = near >"id": 语法错误, db=/data/data/edu.flying.panda.taskmanager/数据库/dbManager

04-05 23:09:37.545:E/SQLiteDatabase(606):插入细节时出错=类型=学校日期=位置=用户 ID=1 描述=sdfg 完成=假

04-05 23:09:37.545: E/SQLiteDatabase(606): android.database.sqlite.SQLiteException: 在“id”附近: 语法错误: , 编译时: INSERT INTO tasks(detail,type,date,location,user id ,description,completed) 值 (?,?,?,?,?,?,?)

package edu.flying.panda.taskmanager;

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;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHandler extends SQLiteOpenHelper {
    // current user id
    private static int CURRENT_USER_ID;
    // db version
    private static final int DATABASE_VERSION = 1;
    // db name
    private static final String DATABASE_NAME = "dbManager";
    // table name
    private static final String TABLE_USERS = "users";
    private static final String TABLE_TASKS = "tasks";
    // user table column names
    private static final String USER_KEY_ID = "id";
    private static final String USER_KEY_USERNAME = "username";
    private static final String USER_KEY_PASSWORD = "password";
    private static final String USER_KEY_NAME = "name";
    private static final String USER_KEY_EMAIL = "email";
    // task table column names
    private static final String TASK_KEY_ID = "id";
    private static final String TASK_KEY_USERID = "user id";
    private static final String TASK_KEY_DESCRIPTION = "description";
    private static final String TASK_KEY_LOCATION = "location";
    private static final String TASK_KEY_DATE = "date";
    private static final String TASK_KEY_DETAIL = "detail";
    private static final String TASK_KEY_TYPE = "type";
    private static final String TASK_KEY_COMPLETED = "completed";
    
    
    // constructor
    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_USERS_TABLE = 
                String.format("CREATE TABLE %s (%s INTEGER PRIMARY KEY, %s TEXT, %s TEXT, %s TEXT, %s TEXT);",
                        TABLE_USERS, USER_KEY_ID, USER_KEY_USERNAME, USER_KEY_PASSWORD,
                        USER_KEY_NAME, USER_KEY_EMAIL);
        String CREATE_TASKS_TABLE =
                String.format("CREATE TABLE %s (%s INTEGER PRIMARY KEY, %s INTEGER, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s INTEGER);",
                        TABLE_TASKS, TASK_KEY_ID, TASK_KEY_USERID, TASK_KEY_DESCRIPTION, TASK_KEY_LOCATION, TASK_KEY_DATE, TASK_KEY_DETAIL, TASK_KEY_TYPE, TASK_KEY_COMPLETED);
        
        db.execSQL(CREATE_TASKS_TABLE);
        db.execSQL(CREATE_USERS_TABLE);
        
    }
    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_TASKS);
        // Create tables again
        onCreate(db);
    }
    public void addUser(User user) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(USER_KEY_USERNAME, user.getUsername());
        values.put(USER_KEY_PASSWORD, user.getPassword());
        values.put(USER_KEY_NAME, user.getName());
        values.put(USER_KEY_EMAIL, user.getEmail());

        // inserting row
        db.insert(TABLE_USERS, null, values);
        db.close();
    }
    
    public void addTask(Task task){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    
    values.put(TASK_KEY_USERID, getCURRENT_USERID());
    values.put(TASK_KEY_DESCRIPTION, task.getDescription() );
    values.put(TASK_KEY_LOCATION, task.getLocation() );
    values.put(TASK_KEY_DATE, task.getDueDate());
    values.put(TASK_KEY_DETAIL, task.getDetailedDescription() );
    values.put(TASK_KEY_TYPE, task.getType() );
    values.put(TASK_KEY_COMPLETED,  task.isCompleted());
    
    db.insert(TABLE_TASKS, null, values);
    db.close();     
}
    
    
    
    
    
    
    
    

}
4

1 回答 1

1

该错误似乎在您的 TASK_KEY_USERID 中,因为您的值中有一个空格。它应该是一个下划线吗?要添加的另一个注意事项是,android 喜欢您的主键列名称以“_id”结尾,而您的只是“id”。

于 2012-04-06T00:35:47.077 回答