【问题标题】:insert in table with Laravel with period (speckeld)使用 Laravel 插入带有句点的表格(斑点)
【发布时间】:2017-04-09 11:51:02
【问题描述】:

大家早上好。

我有一个我无法解决的问题

我正在尝试使用 Laravel 在数据库中保存一条记录,问题是其中一个字段的名称中有一个点。

这是结构: struct table

我尝试过这样做:

$emqu_accountavg = DB::table('emqu_accountavg_resptime')->insert([
                    'company_id' => $company_id,
                    '#Month' => $item['#Month'],
                    'System' => $item['System'],
                    'APPLID' => $item['APPLID'],
                    'GUI/NoGUI' => $item['GUI/NoGUI'],
                    'Resptime avg. (ms)' => $item['Resptime avg. (ms)'],
                ]);

但这是我得到的错误:

QueryException in Connection.php line 647:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Resptime avg. (ms)' in 'field list' (SQL: insert into `emqu_accountavg_resptime` (`company_id`, `#Month`, `System`, `APPLID`, `GUI/NoGUI`, `Resptime avg`.` (ms)`) values (1, 12017, BWP, APPL358552, GUI, 1581,4))

我也尝试过这样做:

emqu_accountavg_resptime::create([
                    'company_id' => $company_id,
                    '#Month' => $item['#Month'],
                    'System' => $item['System'],
                    'APPLID' => $item['APPLID'],
                    'GUI/NoGUI' => $item['GUI/NoGUI'],
                    'Resptime avg. (ms)' => $item['Resptime avg. (ms)'],
                ]);

没有错误发生并且记录被保存,但是有点的保持它为空。 我检查了这个值:

$item['Resptime avg. (ms)']

好吧,问题是数据库中带有斑点(点)的那个字段的名称

【问题讨论】:

  • 不确定这是否可行。试试DB:raw('`Resptime avg. (ms)`') => $item['Resptime avg. (ms)']

标签: php mysql laravel orm eloquent


【解决方案1】:

您可以在第一个错误中看到 laravel 尝试插入的列是 `Resptime avg`。` (ms)` 它应该是 `Resptime avg。 (毫秒)`。

您可以在此处找到如何正确转义该值:

Updating a MySQL column that contains dot (.) in its name

【讨论】:

  • 我做了这个:emqu_accountavg_resptime::create([ 'company_id' => $company_id, '#Month' => $item['#Month'], 'System' => $item[' System'], 'APPLID' => $item['APPLID'], 'GUI/NoGUI' => $item['GUI/NoGUI'], Resptime avg.(ms) => $item['Resptime avg. (ms)' ], ]);但是,值被保存为 null
【解决方案2】:

我得到了答案,没有指定表的字段名称,但尊重与存储相同的顺序,因此:

DB::insert('insert into emqu_accountavg_resptime values (?, ?, ?, ?, ?, ?)', [$company_id, $item['#Month'],$item['System'],$item['APPLID'],$item['GUI/NoGUI'],$item['Resptime avg. (ms)']]);

感谢大家的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 2011-09-23
    • 2023-03-12
    相关资源
    最近更新 更多