SQLiteDatabase更新不工作? [英] SQLiteDatabase update not working?
问题描述
我想让db.update用新的值更新我的数据库的一行,但似乎没有保存。我已经查看了我的语法,但我似乎无法获取数据保存。插入功能正在工作,但不更新。任何帮助将不胜感激。下面是我的数据类,它使用一个DbHelper对象。
public class Data {
static final String TAG =Data;
public static final String DB_NAME =data.db;
public static final String TABLE_NAME =tasks;
public static final String C_ID =_id;
public static final String C_TASK =taskname;
public static final String C_DUE_DATE =due_date;
public static final String C_PRIORITY =priority;
public static final int DB_VERSION = 1;
上下文上下文;
DbHelper dbHelper;
SQLiteDatabase db;
public数据(上下文上下文){
this.context = context;
dbHelper = new DbHelper();
}
public void insert(ToDoItem task){
db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(C_TASK,task.getTask());
values.put(C_PRIORITY,task.getPriority());
db.insert(TABLE_NAME,null,values);
}
public void update(int id,ToDoItem task){
ContentValues values = new ContentValues();
values.put(C_ID,id);
values.put(C_TASK,task.getTask());
values.put(C_PRIORITY,task.getPriority());
String [] whereArgs = {String.valueOf(id)};
db.update(TABLE_NAME,values,C_ID +=?,whereArgs);
System.out.println(db.update(TABLE_NAME,values,C_ID +=?,whereArgs));
Log.d(TAG,更新任务+ db.toString()++ id +优先级+ task.getPriority
}
public void delete(int id){
db.delete(TABLE_NAME,C_ID +=+ id,null);
}
public Cursor queueAll(){
db = dbHelper.getWritableDatabase();
String [] columns = new String [] {C_ID,C_TASK,C_DUE_DATE,C_PRIORITY};
游标cursor = db.query(TABLE_NAME,columns,
null,null,null,null,null);
return cursor;
}
class DbHelper extends SQLiteOpenHelper {
public DbHelper(){
super(context,DB_NAME,null,DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
String sql = String.format(create table%s+
(%s int primary key,%s text,%s text,%s int),
TABLE_NAME,C_ID,C_TASK,C_DUE_DATE,C_PRIORITY);
Log.d(TAG,onCreate with SQL:+ sql);
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
db.execSQL(drop if exists+ TABLE_NAME);
onCreate(db);
}
}
}
ToDoItem对象类我试图更新项目被点击时的优先级
public class ToDoItem {
private String任务;
private String dueDate;
private int priority;
public ToDoItem(String task){
this.task = task;
this.priority = 1;
}
public ToDoItem(String task,String dueDate){
this.task = task;
this.dueDate = dueDate;
this.priority = 1;
}
public ToDoItem(String task,int priority){
this.task = task;
if(priority< = 3&& amp; priority> 0){
this.priority = priority;
} else
this.priority = 1;
}
public ToDoItem(String task,String dueDate,int priority){
this.task = task;
this.dueDate = dueDate;
if(priority< = 3&& amp; priority> 0){
this.priority = priority;
} else
this.priority = 1;
}
public String getTask(){
return task;
}
public void setTask(String task){
this.task = task;
}
public String getDueDate(){
return dueDate;
}
public void setDueDate(String dueDate){
this.dueDate = dueDate;
}
public int getPriority(){
return priority;
}
public void setPriority(int priority){
if(priority< = 3&& priority> 0){
this.priority =优先;
} else
this.priority = 1;
}
}
最后这里是我的onListItemClickListener它使用一个ArrayList ToDoItems ,更改优先级并从我的数据类调用update方法。但是,我试图更新的行的值保持不变。我使用Log.d检查是否正确的值被传递和他们是。由于某些原因,更改不会发生。任何帮助将不胜感激。
todoList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@覆盖
public void onItemClick(AdapterView<?> arg0,View arg1,final int arg2,
long arg3){
if(soundEnabled){
playSound );
}
//更改arrayList中项目的优先级todoItems
todoItems.get(arg2).setPriority(todoItems.get(arg2).getPriority + 1);
String name = todoItems.get(arg2).getTask();
int priority = todoItems.get(arg2).getPriority();
final ToDoItem task = new ToDoItem (name,priority);
//获取数据库中任务的位置
int id = arg2 + 1;
data.update(id,task);
});
是否打印db.update ,C_ID +=+ id,null)函数返回值?
如果返回值为0,那么在DB中没有这样的id行记录。
I am trying to get db.update to update one of the rows of my database with new values but it does not seem to be saving. I have looked over my syntax but I cannot seem to get the data to save. The insert function is working but not update. Any help would be appreciated. Below is my data class which uses a DbHelper object.
public class Data {
static final String TAG = "Data";
public static final String DB_NAME = "data.db";
public static final String TABLE_NAME = "tasks";
public static final String C_ID = "_id";
public static final String C_TASK = "taskname";
public static final String C_DUE_DATE = "due_date";
public static final String C_PRIORITY = "priority";
public static final int DB_VERSION = 1;
Context context;
DbHelper dbHelper;
SQLiteDatabase db;
public Data(Context context) {
this.context = context;
dbHelper = new DbHelper();
}
public void insert(ToDoItem task) {
db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(C_TASK, task.getTask());
values.put(C_PRIORITY, task.getPriority());
db.insert(TABLE_NAME, null, values);
}
public void update(int id, ToDoItem task) {
ContentValues values = new ContentValues();
values.put(C_ID, id);
values.put(C_TASK, task.getTask());
values.put(C_PRIORITY, task.getPriority());
String[] whereArgs = {String.valueOf(id)};
db.update(TABLE_NAME, values, C_ID +" =?", whereArgs);
System.out.println(db.update(TABLE_NAME, values, C_ID +" =?", whereArgs));
Log.d(TAG, "updating task " + db.toString() +" "+ id + " to priority " + task.getPriority());
}
public void delete(int id){
db.delete(TABLE_NAME, C_ID+"="+id, null);
}
public Cursor queueAll(){
db = dbHelper.getWritableDatabase();
String[] columns = new String[]{C_ID, C_TASK, C_DUE_DATE, C_PRIORITY};
Cursor cursor = db.query(TABLE_NAME, columns,
null, null, null, null, null);
return cursor;
}
class DbHelper extends SQLiteOpenHelper {
public DbHelper() {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = String.format("create table %s" +
"(%s int primary key, %s text, %s text, %s int)",
TABLE_NAME, C_ID, C_TASK, C_DUE_DATE, C_PRIORITY);
Log.d(TAG, "onCreate with SQL:"+sql);
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop if exists " + TABLE_NAME);
onCreate(db);
}
}
}
Here is the ToDoItem object class I am trying to update the priority when an item is clicked
public class ToDoItem {
private String task;
private String dueDate;
private int priority;
public ToDoItem(String task) {
this.task = task;
this.priority = 1;
}
public ToDoItem(String task, String dueDate) {
this.task = task;
this.dueDate = dueDate;
this.priority = 1;
}
public ToDoItem(String task, int priority) {
this.task = task;
if(priority <=3 && priority > 0) {
this.priority = priority;
} else
this.priority = 1;
}
public ToDoItem(String task, String dueDate, int priority) {
this.task = task;
this.dueDate = dueDate;
if(priority <=3 && priority > 0) {
this.priority = priority;
} else
this.priority = 1;
}
public String getTask() {
return task;
}
public void setTask(String task) {
this.task = task;
}
public String getDueDate() {
return dueDate;
}
public void setDueDate(String dueDate) {
this.dueDate = dueDate;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
if(priority <=3 && priority > 0) {
this.priority = priority;
} else
this.priority = 1;
}
}
Finally here is my onListItemClickListener it uses an ArrayList of ToDoItems, changes the priority and calls the update method from my data class. However the values of the row that I am trying to update remain unchanged. I used Log.d to check if the correct values were being passed and they were. For some reason the changes are not happening. Any help would be appreciated.
todoList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2,
long arg3) {
if(soundEnabled) {
playSound();
}
// Change the priority of the item inside the arrayList todoItems
todoItems.get(arg2).setPriority(todoItems.get(arg2).getPriority() + 1);
String name = todoItems.get(arg2).getTask();
int priority = todoItems.get(arg2).getPriority();
final ToDoItem task = new ToDoItem(name, priority);
// Get the position of the task in the database
int id = arg2 + 1;
data.update(id, task);
});
Would you print the db.update(TABLE_NAME, values, C_ID +"="+id, null) function return value?
If the return value is 0, so there is no such "id" row record in DB.
这篇关于SQLiteDatabase更新不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!