Android – Sqlite error code 1, msg syntax error

androidsqlite

From what I can tell, it seems like it is passing the addTask contentvalues in the wrong order, but I do not see why. The addUser method is implemented in the same way and works fine.

The error that I get and all relevant code is here. Any help is appreciated.

04-05 23:09:37.465: I/SqliteDatabaseCpp(606): sqlite returned: error code = 1, msg = near >"id": syntax error, db=/data/data/edu.flying.panda.taskmanager/databases/dbManager

04-05 23:09:37.545: E/SQLiteDatabase(606): Error inserting detail=
type=SCHOOL date= location= user id=1 description=sdfg completed=false

04-05 23:09:37.545: E/SQLiteDatabase(606):
android.database.sqlite.SQLiteException: near "id": syntax error: ,
while compiling: INSERT INTO tasks(detail,type,date,location,user
id,description,completed) VALUES (?,?,?,?,?,?,?)

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();     
}
    
    
    
    
    
    
    
    

}

Best Answer

The error appears to be in your TASK_KEY_USERID as you have a space in the value. Should it perhaps be an underscore? Another note to add is that android likes your primary key column name to end in "_id", which yours are just "id".