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');
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 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
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.
In BIRT, When i try to fetch the records from my localhost, its working fine. But when i try to work with remote connection i am getting error as specified below:
Error :
org.eclipse.birt.data.engine.odaconsumer.OdaDataException: Cannot get the result set metadata.
org.eclipse.birt.report.data.oda.jdbc.JDBCException: SQL statement does not return a ResultSet object.
SQL error #1:Table 'test.TBLUSERS' doesn't exist ... 63 more
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Table 'testbms.TBLUSERS' doesn't exist
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
Note:
Tablenames are automatically changing to capital letters, is that because of it.
Because client server is linux and is it acting with case sensitive.
But it displays column names but not the records. As soon as i click
on finish, i get the error as specified in the below images.
Reference Image:
As you can see in the above image, it has populated the table columns in the second row
Is their any special configurations need to be done for remote connection or am i doing anything wrong?
As you stated, it is probably a case of case-sensitivity:
http://dev.mysql.com/doc/refman/5.0/en/identifier-case-sensitivity.html
Although database and table names are not case sensitive on some
platforms, you should not refer to a given database or table using
different cases within the same statement. The following statement
would not work because it refers to a table both as my_table and as
MY_TABLE: mysql> SELECT * FROM my_table WHERE MY_TABLE.col=1;
If your development box isn't case sensitive then when you change the case of your tablename to match that on production you'll still be able to test. There might also be a way in MySQL using system tables. (See the following query for an example of querying to see if a table exists.):
SELECT count(*)
FROM information_schema.tables
WHERE table_schema = <schema-or-db-name>
AND table_name = <table-or-view-name>
but more realistically, your target database should be passed to your report through a variable that you can check in the scripting of the dataset. Set the "this.query" value to equal the appropriate query based on that variable's value.
E.G.:
if ( params["source_db"].value == "Server=myProductionAddress;Database=myProductionDB;Uid=myUsername;Pwd=myPassword;" )
{
this.query = "SELECT .... prodTableName";
}
else
{
this.query = "SELECT .... devTableName";
}
I can't deal with it. I'm experiencing big troubles with this very query:
UPDATE books
SET books.out = books.out + 1
WHERE id = 81813130;
UPDATE books
SET books.available = 0
WHERE books.in = books.out;
If I run it on my phpMyAdmin, everything's fine and everything completes, but in my CakePHP application this query doesn't work and when I perform a debug this is what I'm told:
Warning (512): SQL Error: 1064: 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 'UPDATE books SET books.available = 0 WHERE books.in = books.out' at line 1**
I'm calling my query from a controller:
$this->Lending->update_lendings($this->data['Lending']['book_id']);
and the actual query is of course into the model:
$query = "
UPDATE books
SET books.out = books.out + 1
WHERE id = ".$id.";
UPDATE books
SET books.available = 0
WHERE books.in = books.out;
";
I really can't say why this isn't working. It seems that error 1064 Mysql is a very common question in here but I didn't find anything useful about my very issue.
I steadfastly thank you for your support and help.
It looks like your problem might be due to PHP's lack of support for Multiple Statement Execution. Multiple Statement Execution allows you to run two queries in a single request and receive multiple result-sets in response.
MySQL DOES support it, but the default setup in PHP prevents this (that is, if you're using the deprecated mysql_connect() era functions). This is actually a nice default because there are some serious bugs that can be introduced by allowing multiple-queries (see SQL injection).
So, the solution could be to alter your code to request the data separately.
$query = "
UPDATE books
SET books.out = books.out + 1
WHERE id = ".$id.";";
mysql_query($db, $query);
$query = "UPDATE books
SET books.available = 0
WHERE books.in = books.out;";
mysql_query($db, $query);
That being said, if you think that it's safe enough to use multi-statements (that is, if all of the input values are sanitized), then go ahead and try to use the mysqli functions (there not even deprecated!).
mysqli_multi_query( $query ) should give you the flexibility you need.
aparently, it's because you use reserved words in your query, try and escape all table names and table columns in ``
list of reserved words in mysql available here
If the second Update statement is meant to change only the row that the first statement updated, then you could use a single Update:
UPDATE books
SET out = out + 1
, available = CASE WHEN in = out
THEN 0
ELSE available
END
WHERE id = 81813130