我试图让 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;
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);
}
}
}
这是我尝试在单击项目时更新优先级的 ToDoItem 对象类
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;
}
}
最后是我的 onListItemClickListener,它使用 ToDoItems 的 ArrayList,更改优先级并从我的数据类调用更新方法。但是,我尝试更新的行的值保持不变。我使用 Log.d 检查是否传递了正确的值,它们确实传递了。由于某种原因,更改没有发生。任何帮助将不胜感激。
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);
});
最佳答案
你会打印 db.update(TABLE_NAME, values, C_ID +"="+id, null) 函数返回值吗?
如果返回值为0,则DB中没有这样的“id”行记录。
关于android - SQLiteDatabase 更新不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14532859/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
我正在尝试为我的iOS应用程序设置cocoapods但是当我执行命令时:sudogemupdate--system我收到错误消息:当前已安装最新版本。中止。当我进入cocoapods的下一步时:sudogeminstallcocoapods我在MacOS10.8.5上遇到错误:ERROR:Errorinstallingcocoapods:cocoapods-trunkrequiresRubyversion>=2.0.0.我在MacOS10.9.4上尝试了同样的操作,但出现错误:ERROR:Couldnotfindavalidgem'cocoapods'(>=0),hereiswhy:U
这太简单了,太荒谬了,我在任何地方都找不到关于它的任何信息,包括API文档和Rails源代码:我有一个:belongs_to关联,我开始理解当您没有关联时您在Controller中调用的正常模型方法与您有关联时调用的方法略有不同。例如,我的关联在创建Controller操作时运行良好:@user=current_user@building=Building.new(params[:building])respond_todo|format|if@user.buildings.create(params[:building])#etcetera但我找不到关于更新如何工作的文档:@user
我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o
升级到OSXYosemite后,我现有的pow.cx安装不起作用。升级到最新的pow.cx无效。通过事件监视器重新启动它也没有成功。 最佳答案 卸载(!)并重新安装解决了这个问题。curlget.pow.cx/uninstall.sh|shcurlget.pow.cx|sh 关于ruby-on-rails-OSXYosemite更新破坏了pow.cx,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/q
我们在Ubuntu14.04和Gitlab9.3.7上运行,运行良好。我们正在尝试更新到Gitlabv9.3.8的最新安全补丁,但它给我们这个错误:Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension.currentdirectory:/home/git/gitlab/vendor/bundle/ruby/2.3.0/gems/re2-1.0.0/ext/re2/usr/local/bin/ruby-r./siteconf20170720-19622-15i0edf.rbextconf.rbcheckingformain(
这段代码似乎创建了一个范围从a到z的数组,但我不明白*的作用。有人可以解释一下吗?[*"a".."z"] 最佳答案 它叫做splatoperator.SplattinganLvalueAmaximumofonelvaluemaybesplattedinwhichcaseitisassignedanArrayconsistingoftheremainingrvaluesthatlackcorrespondinglvalues.Iftherightmostlvalueissplattedthenitconsumesallrvaluesw