据我所知,似乎它以错误的顺序传递addTask内容值,但我不明白为什么。 addUser方法以相同的方式实现并且工作正常。
我得到的错误和所有相关代码都在这里。任何帮助表示赞赏。
04-05 23:09:37.465:I / SqliteDatabaseCpp(606):sqlite返回:错误代码= 1,msg = near>“id”:语法错误,db = / data / data / edu.flying。 panda.taskmanager / databases / dbManager
04-05 23:09:37.545:E / SQLiteDatabase(606):插入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”:语法错误:, 编译时:INSERT INTO任务(详细信息,类型,日期,位置,用户 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();
}
}
答案 0 :(得分:1)
错误似乎在您的TASK_KEY_USERID中,因为您在值中有空格。它应该是一个下划线吗?要添加的另一个注意事项是,android喜欢你的主键列名以“_id”结尾,你的只是“id”。