ansible mysql_query UPDATE statement - mysql

I am trying to use the mysql_query statement to update records in my database.
Can anyone understand why this code would not work and how I would manage to add dynamically the key values to the UPDATE statements ?
...
{
"diff_records": {
"app_portfolio_manager": "New Manager"
}
}
- name: case 1b insert app_dict to db table app_info
community.mysql.mysql_query:
- UPDATE app_info
SET ( {{ diff_records.keys() | join(', ') }} ) VALUES ( {{ diff_records.values() | map('regex_replace', '^(.*)$', "'\1'") | join(', ') }} )
WHERE app_name = '{{ app_dict.app_name }}'
fatal: [localhost]: FAILED! => {
"changed": false,
"invocation": {
"module_args": {
"query": [
"UPDATE app_info SET ( app_portfolio_manager ) VALUES ( 'New Manager' ) WHERE app_name = 'My new App'"
],
"single_transaction": true
}
},
"msg": "Cannot execute SQL 'UPDATE app_info SET ( app_portfolio_manager ) VALUES ( 'New Manager' ) WHERE app_name = 'My new App'' args [None]: (1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '( app_portfolio_manager ) VALUES ( 'New Manager' ) WHERE app_name = 'My new...' at line 1\")"
}

The following only works with single key value variable no go for multiple
- name: case 1b insert app_dict to db table app_info community.mysql.mysql_query: - UPDATE app_info SET {{ diff_records.keys() | join(', ') }} = {{ diff_records.values() | map('regex_replace', '^(.*)$', "'\1'") | join(', ') }} WHERE app_name = '{{ app_dict.app_name }}'

update .
Adding a simple dict loop seems to have done the trick.
- name: case 1b insert app_dict to db table app_info
community.mysql.mysql_query:
- UPDATE app_info SET {{ item.key }} '{{ item.value }}'
WHERE app_name = '{{ app_dict.app_name }}'
loop: "{{ diff_records | dict2items }}"

Related

How to update a field by incrementing the value in batch update

So far, I have tried updating balance table like below. Its works fine.
$values = array(
array('id'=>10,'c1'=>101),
array('id'=>11,'c1'=>102)
);
$this->db->update_batch('balance',$values,'id');
I would like to know how to update field value = previous value + new value, means, c1 = c1 + 101 in update batch array. I found
$this->db->set('c1','c1 + 101',FALSE);
would be helpful. But I've no idea, how to use that in batch operation.
I have tried array('id'=>10,'c1'=>'c1 + 101') but it's not working.
yes you can do this with update_batch too.
$data = [
[
'id' => 10 ,
'c1' => '(c1+101)' ,
],
[
'id' => 11,
'c1' => '(c1+102)' ,
]
];
$this->db->set_update_batch($data,'id',FALSE);
$this->db->update_batch('mytable',null,'id');
generated query :
UPDATE `mytable` SET c1 = CASE
WHEN id = 10 THEN (c1+101)
WHEN id = 11 THEN (c1+102)
ELSE c1 END
WHERE id IN(10,11)
You try this
$this->db->set('c1', 'c1+101', FALSE)
to
$this->db->set('c1', '`c1+101`', FALSE)

MySQL error during installing Prestashop module

i made module for prestashop. Just basic administration form with write into DB. But when i try to install module at Prestashop, i have this error:
[PrestaShopDatabaseException]
Duplicate entry '0' for key 'PRIMARY'
INSERT INTO `ps_module` (`name`, `active`, `version`) VALUES ('apishippingtextsource', '1', '0.0.1')
at line 635 in file classes/db/Db.php
629. WebserviceRequest::getInstance()->setError(500, '[SQL Error] '.$this->getMsgError().'. From '.(isset($dbg[3]['class']) ? $dbg[3]['class'] : '').'->'.$dbg[3]['function'].'() Query was : '.$sql, 97);
630. }
631. else if (_PS_DEBUG_SQL_ && $errno && !defined('PS_INSTALLATION_IN_PROGRESS'))
632. {
633. if ($sql)
634. throw new PrestaShopDatabaseException($this->getMsgError().'<br /><br /><pre>'.$sql.'</pre>');
635. throw new PrestaShopDatabaseException($this->getMsgError());
636. }
637. }
638.
639. /**
my install.php for sql:
$sql = array();
$sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'api_shipping_text` (
`id_text` int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id_text`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;';
foreach ($sql as $query) {
if (Db::getInstance()->execute($query) == false) {
return false;
}
}
Version of Prestashop is 1.6.0.9.
Can you check the auto_increment value of your ps_module table structure? Maybe auto_increment is not defined or set to 0.
If that's the case, you probably had an error during Database migration or installed another module messing with auto_increment.

How to add default value for varchar value in xampp

i am trying to add a default value in Xampp.
i set it as in the picture, but when i add 1 more product, if i left i in blank, it will be blank, i mean it will not have a value as default.
Do you have any idea.
my code is:
if (isset($_POST["add"])) {
//lấy thông tin từ các form bằng phương thức POST
$tennsx = $_POST["tennsx"];
$diachi = $_POST["diachi"];
$sdt = $_POST["sdt"];
$mieuta = $_POST["mieuta"];
if ($tennsx == "" || $diachi == "" || $sdt == "" ) {
echo '<h4 align=center style="color: red;">Vui lòng nhập đầy đủ thông tin</h4>';
}else{
//thực hiện việc lưu trữ dữ liệu vào db
$sql = "INSERT INTO nhasx(
tennsx,
diachi,
sdt,
mieuta
) VALUES (
'$tennsx',
'$diachi',
'$sdt',
'$mieuta'
)";
// thực thi câu $sql với biến conn lấy từ file connection.php
mysqli_query($conn,$sql);
header('Location:manu_management.php');
}
}
This is because the Mysql assume you want to insert the empty values instead of default values. You need to omit the field with default values if no value at all to insert, see following code :
// simply check if variable $mieuta is empty
// goes here
if ( $mieuta === "" ) {
$sql = "INSERT INTO nhasx(
tennsx,
diachi,
sdt
) VALUES (
'$tennsx',
'$diachi',
'$sdt'
)";
}
else {
$sql = "INSERT INTO nhasx(
tennsx,
diachi,
sdt,
mieuta
) VALUES (
'$tennsx',
'$diachi',
'$sdt',
'$mieuta'
)";
}
Default values should work if you omit the field name and value from query string. This kind of method come in handy when we want to insert default date created column.
For better approach use this :
$sql = "INSERT INTO nhasx(
tennsx,
diachi,
sdt,
mieuta
) VALUES (
'$tennsx',
'$diachi',
'$sdt',
'".($mieuta ==='') ? 'MY DEFAULT VALUE' : $mieuta."'
)";
In your code you have set mieuta = $mieuta while inserting a new record so as per priority it would insert value from $mieuta. please remove and try again as of like below code.
if (isset($_POST["add"])) {
//lấy thông tin từ các form bằng phương thức POST
$tennsx = $_POST["tennsx"];
$diachi = $_POST["diachi"];
$sdt = $_POST["sdt"];
$mieuta = $_POST["mieuta"];
if ($tennsx == "" || $diachi == "" || $sdt == "" ) {
echo '<h4 align=center style="color: red;">Vui lòng nhập đầy đủ thông tin</h4>';
}else{
//thực hiện việc lưu trữ dữ liệu vào db
$sql = "INSERT INTO nhasx(
tennsx,
diachi,
sdt
) VALUES (
'$tennsx',
'$diachi',
'$sdt'
)";
// thực thi câu $sql với biến conn lấy từ file connection.php
mysqli_query($conn,$sql);
header('Location:manu_management.php');
}
}
CREATE TABLE IF NOT EXISTS `xxxx` ( `username` varchar(15) NOT NULL default 'abc')
try MySQL like this.
Example:
CREATE TABLE IF NOT EXISTS `test` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(10) NOT NULL DEFAULT 'abc', `test` varchar(12) NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;INSERT INTO `dbName`.`test` (`id` ,`test`)VALUES (NULL , '1');

Node Postgres querying for json

using node-pg I'm trying to lookup the existence of a string inside a JSON object.
Example (part of) row:
{ viq_id: '801583',
title: 'Blank, security key, lock system, and production method',
applicants: [ [Object], [Object] ],
cpc: [ [Object], [Object] ],
abstract: { value: [Object], language: 'en' } }
abstract is of type JSONB.
When querying for this
var query = 'SELECT viq_id, title, applicants, cpc, abstract ->> "value"' +
' FROM epo_patents' +
' WHERE title ILIKE $1';
or for this
var query = 'SELECT viq_id, title, applicants, cpc, abstract' +
' FROM epo_patents' +
' WHERE title ILIKE $1 OR abstract ->> "value" = $1';
or for this
var query = 'SELECT viq_id, title, applicants, cpc, abstract' +
' FROM epo_patents' +
' WHERE abstract.value = $1';
the answer is "errorMissingColumn", or in the latter case errorMissingRTE
How do I properly query against JSON in node pg?
change var query = 'SELECT viq_id, title, applicants, cpc, abstract ->> "value"' to var query = "SELECT viq_id, title, applicants, cpc, abstract ->> 'value'" , because double quotes used for db objects (table,column,etc) name...
look at Postgres JSON syntax

when i am going to update the db i am getting error like this:

I am getting this error:
A Database Error Occurred
Error Number: 0
UPDATE `announcement_detail` SET `announcement_title` = 'test', `announcement_desc` = 'test announcement\r\ntest announcement\r\ntest announcement\r\ntest announcement\r\ntest announcement\r\ntest announcement ', `announcement_date` = '2015-01-05' WHERE `announcement_id` = 101
Filename: C:\wamp\www\nid\system\database\DB_driver.php
Line Number: 330
Try this active record:
$data = array(
"announcement_title" => "test1",
"announcement_desc" => "some description",
"announcement_date" => "2015-01-05"
);
$this->db->update("announcement_detail",$data);
$this->db->where("announcement_id",101);
Are you escaping your '\' ? See http://dev.mysql.com/doc/refman/4.1/en/string-literals.html