I am trying to update a database column field with raw SQL in laravel. It's important to mention that the update code was written to MySQL drive but now I use Postgres. The column name is dayID. So the update code is:
DB::update("update table set travel = ... WHERE dayID = {$this->dayID}");
I must use raw SQL because I make some updates to polygon types.
The problem is that laravel automatically transforms the dayID to dayid so I get an error:
column "dayid" does not exist
I tried to set a variable in order to use it in update query but it also failed with the same error:
$var = "dayID";
DB::update("update table set travel = ... WHERE ".$var." = {$this->dayID}");
How can I fix it?
Please try DB::table with update below:
DB::table('table_name')
->where('dayID', $this->dayID)
->update(['travel' => '...']);
Laravel document :
https://laravel.com/docs/5.3/queries#updates
Related
I am trying to update a record using Laravel.
I have gone through lot of StackOverflow Questions to check whether this question is already raised.
mysql query : UPDATE students SET total_marks = total_marks + 80 WHERE id = 1
I have to translate this mysql query into Laravel query builder, but couldn't get a solution yet.
Instead of getting the early value from DB before update, Can we update the table with one update query using Laravel Query Builder.
2 Queries way:
$student_marks = Students::select('total_marks')->where('id','=',1);
$current_mark = $student_marks['total_marks']+80;
$update_student_marks = Students::where('id','=',1)->update('total_marks','=',$current_mark);
How to update a record like this with single query builder in Laravel.
I think you need to make a few adjustments to your query.
First, you need to select the student correctly and than use Eloquent to call save method on it after setting the property to the correct value. I assume you are on Laravel 6.
$student_marks = Students::find($id);
$student_marks->total_marks += 80;
$student_marks->save();
Please, take a look at Laravel docs:
https://laravel.com/docs/6.x/eloquent
The reading takes some time but its definitely worth it. You will learn how to deal with eloquent and make your code better by using the most appropriate techniques.
You can use the save function for this.
$student_marks = Students::select('total_marks')->where('id','=',1);
$student_marks->total_marks += 80; //or $student_marks->total_marks = $student_marks->total_marks + 80;
$student_marks->save();
Pass update data as array
Try this way
$update = array('total_marks'=>$current_mark);
$update_student_marks = Students::where('id','=',1)->update($update);
I want to update a column enum_value of tableB with the values of another column enum_value in tableA using MySQL update query as follows:
UPDATE tableB t1
INNER JOIN TableA t2 ON t1.sig_name = t2.sig_name
SET t1.enum_value = t2.enum_value
WHERE t1.dbc_Version = t2.dbc_version
The above SQL query runs fine in the MYSQL workbench, but I want to execute this query (perform this Update dynamically using NiFi PUTSQL (SQL Statement) property. When I write this in NiFi PutSQL, I get an error. I have attached the screenshots below:
Is there I way I can achieve dynamic(on the fly update in the DB) using NiFi? If yes, Then how and what controller services needThanks in advance! to be set?
Thanks in advance!
If you have the query directly in PutSQL, what is the purpose of the ConvertJSONToSQL processor right before that? That processor generates attributes from the JSON data, which PutSQL looks for when trying to populate a prepared statement with parameters (which you don't have as you're using an explicit statement with no ?s).
See this SO post for more information, basically you should use UpdateAttribute to remove any attributes created by ConvertJSONToSQL.
I want make package with list of query.
It is update query like as:
UPDATE table
SET column = XXX
WHERE column = XXX AND column2 = XXX;
UPDATE table
SET column = XXX
WHERE column = XXX AND column2 = XXX;
UPDATE table
SET column = XXX
WHERE column = XXX AND column2 = XXX;
I have these 1000 queries in one package. Now I want make do queries.
I try do it, but unsuccessfully, because every time I get an error like that:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near...
I copy this query and past to phpmyadmin and I do it. These queries work!
I have a variable with prepared queries and I trying to do it in different way.
Database::instance()->query(NULL,$sql);
DB::query(5,$sql)->execute();
DB::query(Database::UPDATE,DB::expr($sql))->execute();
DB::query(Database::UPDATE,$sql)->execute();
but it does not work ;/
Anyone know how do it?
Just call method query from instance of database:
/** #var Database $db */
$db = Database::instance();
$db->query(Database::UPDATE, 'UPDATE table SET column = XXX WHERE column = XXX AND column2 = XXX;');
But if you want execute multiple SQL statements in one query it's impossible out of box. By default Kohana use old mysql API and mysql_query do not support multiple queries in a single call.
It you want use multiple SQL statements i know 3 way:
Some times ago for Kohana i saw module for mysqli support, you can try to find him and modify: add to it new method which will be use http://php.net/manual/en/mysqli.multi-query.php mysqli::multi_query can execute multiple SQL statements in one query.
Or you can switch to using PDO. For this you must make changes in your database config file, according Kohana's documentation, add map for columns in models.
Or create new PDO connection in set up it only for this query (if you don't use transaction it will be more easy, than variant 2).
Also for variant 2 and 3 you must set up some PDO settings: https://stackoverflow.com/a/6461110/419665
P.S. For call database instance by config name:
Database::instance('config_name');
This should be the simplest thing but for some reason it's eluding me completely.
I have a Sequel connection to a database named DB. It's using the Mysql2 engine if that's important.
I'm trying to update a single record in a table in the database. The short loop I'm using looks like this:
dataset = DB["SELECT post_id, message FROM xf_post WHERE message LIKE '%#{match}%'"]
dataset.each do |row|
new_message = process_message(row[:message])
# HERE IS WHERE I WANT TO UPDATE THE ROW IN THE DATABASE!
end
I've tried:
dataset.where('post_id = ?', row[:post_id]).update(message: new_message)
Which is what the Sequel cheat sheet recommends.
And:
DB["UPDATE xf_post SET message = ? WHERE post_id = ?", new_message, row[:post_id]]
Which should be raw SQL executed by the Sequel connector. Neither throws an error or outputs any error message (I'm using a logger with the Sequel connection). But both calls fail to update the records in the database. The data is unchanged when I query the database after running the code.
How can I make the update call function properly here?
Your problem is you are using a raw SQL dataset, so the where call isn't going to change the SQL, and update is just going to execute the raw SQL. Here's what you want to do:
dataset = DB[:xf_post].select(:post_id, :message).
where(Sequel.like(:message, "%#{match}%"))
That will make the where/update combination work.
Note that your original code has a trivial SQL injection vulnerability if match depends on user input, which this new code avoids. You may want to consider using Dataset#escape_like if you want to escape metacharacters inside match, otherwise if match depends on user input, it's possible for users to use very complex matching syntax that the database may execute slowly or not handle properly.
Note that the reason that
DB["UPDATE xf_post SET message = ? WHERE post_id = ?", new_message, row[:post_id]]
doesn't work is because it only creates a dataset, it doesn't execute it. You can actually call update on that dataset to run the query and return number of affected rows.
I am attempting to use the update command on an ASP.NET website and my logic of SQL follows:
UPDATE (SELECT `Event Name`,`Date`,`Time`,`Location`,`Goal`,`ID` FROM Calendar)
but, MySQL returns an error:
#1248 - Every derived table must have its own alias
Any ideas to fix this would be appreciated. Also, I have seen this alternative to plain SQL commands: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.updatecommand.aspx
All solutions would be helpful in structuring this ASP site (C#). Please keep in mind I am new to this technology.
The update statement looks a bit weird. Shouldn't it something like this:
UPDATE Calendar
SET [Event Name] = #Event, Date = #date, Time = #time,
Location = #location, Goal = #goal
WHERE ID = #id
Watch the spaces in column names, you should surround them with square braces, i.e. [Event name].