Check Value from DBI Query Without Consuming - mysql

I have a table in my DB which I just added a new column to, and when I query this table in Perl I want to check only once if this field is empty or not. I need to check the value of this column in just the first row. Usually to go through the query results I use
while (my $row = $sthmm->fetchrow_hashref()) {
# do stuff
}
I could check each row in the while loop but since I only need to do it once, I don't want to waste time doing that.
How can I retrieve one row and check the column's value without consuming it, meaning that I can still go through the whole result in the while loop after checking this condition.

if ( my $row = $sth->fetchrow_hashref() ) {
# Do stuff with first row
do {
# Do stuff to be done to each row
} while $row = $sth->fetchrow_hashref();
}

Related

Laravel update multiple records at once

I have a mysql database table with 1000000 records. I wanted to update one column in each row.
I tried:
public function methodWorking()
{
$properties = Property::with('requests')->get();
foreach ($properties as $property) {
$property->number_of_request = count($property->requests);
$property->save();
}
}
This one is working but, it's very bad in performance wise.
I wanted to write a code like this:
public function methodExpect()
{
$properties = Property::with('requests')->get();
$property_array = [];
foreach ($properties as $property) {
$property->number_of_request = count($property->requests);
$property_array[] = $property;
}
Property::save($property_array);
}
Is it possible with laravel ?
Thanks.
as I know, No way to do bulk updates in one query with MySQL. You can use something inside a loop like putting Property::save($property_array); inside your foreach.
for more details see this
Changing all (or most) rows often indicates a poor schema design. Would you like to tell us what the column contains and why it needs a mass change?
Consider removing that column from the table -- then provide the value some other way.
Yes, UPDATE of a million row table takes a long time. This is because the old copy of each row is held on to, just in case there needs to be a ROLLBACK.
To chunk the table, doing it in manageable pieces, see http://mysql.rjweb.org/doc.php/deletebig#deleting_in_chunks
You can use LaravelBatch it's very helpful (tested on 10 000 records )

How to identify if on update query executed with no changes

Maybe the title is not that clear but assume this situation:
I have a form to update a specific row of a table. When I submit the form an UPDATE query is performed.
I count the number of affected rows to determine if the query went right or not.
$row->execute();
$count = $row->rowCount();
if($count==0){
http_response_code(500);
}else{
http_response_code(200);
}
This works well if the user submit the form by changing any data. If the form is left unchanged the query will affect 0 results and my code will return 500.
But the point is that the query was properly executed.
So my question is: is there a way mysql will tell me that 0 row where affected because nothing changed in the data instead of any other case (eg. wrong value in a field or so on?). So that my if can become something like this:
if($count==0||$no_field_changed==false){
Gordon already gave you the answer in his comment,
You should check the error conditions to see if the query succeeded
but just to make it obvious I will expand upon it for you.
$status = $row->execute();
if ( ! $status ) {
// the query errored
$arr = $row->errorInfo();
error_log(print_r($arr,1), 3, 'db_error.log'));
http_response_code(500);
exit;
}
// So if we get here the query ran successfully
if($row->rowCount() == 0){
// Nothing was changed by our query this time but it ran successfully
http_response_code(200);
}else{
// Something was changed by our query
http_response_code(200);
}

Mystic MySql Laravel issue

I have gotten into an issue, which is kind of, what?
1. In my controller I get the input values from form.
2. I create row in database table and fill in the red values.
The problem is that one of these input values is an integer and when I put in DB table, its always 0, nothing else. I returned the just the issued value, its not 0.
The DB table column names are all correct, data type is also correct.
The weirdest part is that in other function (the same controller), I do the same from other input values, and it puts in the correct values, also the one with the same type... code is identical, but in one case it puts in zeros...
I am trying to add an integer value to DB table with
Projects::create([
'name'=>$name,
'region'=>$region,
'needs'=>$amount, <-the issued value
'ready'=>false
]);
I am 100% sure that this table has column "needs" and that the $amount variable is holding the correct value (not 0).
This has bean a real head cracker for few hours, and nothing, cant find anything that would solve this...
The code of retrieving Input data is:
$name = Input::get('name');
$region = Input::get('region');
$amount = Input::get('projectAmount');
Projects::create([
'name'=>$name,
'region'=>$region,
'needs'=>$amount,
'ready'=>false
]);

How to display multiple records in java?

I have connected to one MySql table to fetch 1000 records that need to be displayed using JSP. I want to split my table into multiple tables (of size 50) which can be viewed by using next button on jsp page. How can I implement it?
String sql = "select * from people";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet result = statement.executeQuery();
while(result.next()) { // here i get 1000 records. how can i display these records on mulitple jsp pages?
// ... get column values from this record
}
Above gives 1000 records and I use a PreparedStatement.
Assuming you can fetch the complete table in JSP.
Just add one more param to the function returning the ROWS COUNTER
Add COUNTER to LIMIT clause in your QUERY.
Now you need to keep track of the COUNTER so Update the URL on click on next as
First Click - URL?pageCOUNTER=1
Second Click - URL?pageCOUNTER=2
Fetch the pageCOUNTER varaible from URL and pass it to your function as COUNTER
And so on. Keep getting you result.
OR USE JAVASCRIPT TABLE - http://www.datatables.net/

perl CGI radio_group: populate values from DB query

Im trying to dynmically generate a table that has a radio button per row whose value is set to the ID field of a SQL table. Im not sure how I can reference this value using CGI radio_group. In my research radio_group uses an associate array, however if I convert the SQL query to an associative array the values wont match up since there are more than 2 tables in the query. If possible I'd like to avoid a second SQL call:
use CGI;
use DateTime::Format::MySQL;
$epoch = DateTime->now(time_zone=>"America/New_York");
$fmtnow = DateTime::Format::MySQL->format_datetime($epoch);
$iasql = qq { select * from alert where endtime > '$fmtnow'};
$iaqry = $iadb->prepare($iasql);
$iaqry->execute() || die "Fail $DBI::errstr"
print $query->start_table({-border=>1, -cellpadding =>1});
while(#iarows = $iaqry->fetchrow_array()) {
print $query->Tr(print $query->td([print $query->radio_group('iaselect',\#iarows[0]),'#iarows[1]','#iarows[2]','#iarows[3]','#iarows[4]','#iarows[5]']));
}
print $query->end_table();
Yeah I think Im gonna nix the the CGI radio button method for this part and just have it print print "<input type="radio">;, it works that way. I was only attempting to use this method because I hadn't realized there are start_div and end_div methods. Prior to finding those I thought this was my only way of achieving the radio buttons inside of the q->div() container.
I excluded the Div portion of the code because it didn't seem relevent.