gpt4 book ai didi

mysql - 不在数据库中插入数据 - grails

转载 作者:行者123 更新时间:2023-11-29 22:08:31 25 4
gpt4 key购买 nike

我没有收到任何错误,但我的应用程序未在数据库中保存数据。

这里是profitTable.gsp

                <g:form controller="dailyProfit" action="save" >
<div class="fieldcontain ${hasErrors(bean: dailyProfitInstance, field: 'date', 'error')} required>
<label for="date" >Date</label>
<g:datePicker name="date" precision="day" value="${dailyProfitInstance?.date}" />
</div>
<br>
<div class="fieldcontain ${hasErrors(bean: dailyProfitInstance, field: 'profit', 'error')} required>
<label for="profit">Profit</label>
<span class="required-indicator">*</span>
<g:field name="profit" value="${fieldValue(bean: dailyProfitInstance, field: 'profit')}" required=""/>
</div>
<br>
<g:submitButton name="create" class="save btn btn-default " value="${message(code: 'default.button.create.label', default: 'Create')}" />
</g:form>

这是我的 Controller

打包示例grailsapp

class DailyProfitController {
def scaffold = DailyProfit
def index() {
render(view:"profitTable");
}

def save() {
// Date myDate = params.date('test', 'yyyy-MM-dd');
def dailyProfit = new DailyProfit(params)
params.date = params.date_year + "-0" + params.date_month + "-" + params.date_day
println params.toString();
dailyProfit.save()
render(view:"profitTable");
}
}

这是我的域类

package samplegrailsapp

import java.text.DateFormat
import java.text.SimpleDateFormat
import org.grails.databinding.BindingFormat
class DailyProfit {
@BindingFormat('yyyy-MM-dd')
Date date;
double profit;

static constraints = {
date (blank:false, nullable:false, validator: {value, object ->
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
if(!(cal.getTime().before(value) || cal.getTime().equals(value))){
return false;
}
})
profit (blank:false, nullable:false, validator: {value, object ->
if (!value.toString().matches(/^([1-9]{1}[0-9]*\.{0,1}\d{0,2}|0\.[0-9]{1,2}|0)$/) ) return false;
})
}

static mapping = {
version false
//id generator:'assigned', name:'date'
id column: 'date', name: 'date', generator: 'assigned'
}

}

这是 params 内的数据。

[date_day:10, profit:8, date_year:2015, date_month:8, date:2015-08-10, create:Create, action:save, format:null, controller:dailyProfit]

当我检查日志时,这就是我得到的:

Hibernate: select dailyprofi_.date, dailyprofi_.profit as profit0_ from daily_profit dailyprofi_ where dailyprofi_.date=? Hibernate: insert into daily_profit (profit, date) values (?, ?)

为什么不能从params读取数据?我可以做什么来修复它?

最佳答案

代码的问题是 dailyProfit.date 接受一个日期对象,但您提供了一个字符串日期。我也面临同样的问题。下面是代码:

def save() {
// Date myDate = params.date('test', 'yyyy-MM-dd');

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

def dailyProfit = new DailyProfit()
def updatedDate = params.date_year + "-0" + params.date_month + "-" + params.date_day
dailyProfit.date = sdf.parse(updatedDate);

dailyProfit.profit = params.profit

dailyProfit.save()
render(view:"profitTable");
}

请告诉我它是否有效。

祝你编码愉快。

关于mysql - 不在数据库中插入数据 - grails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31914310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com