<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ZtoloGame</title>
    <description>The latest articles on DEV Community by ZtoloGame (@ztologame).</description>
    <link>https://dev.to/ztologame</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F902790%2F5b7137ef-e5b5-4d5f-855a-49b7d48adfcb.png</url>
      <title>DEV Community: ZtoloGame</title>
      <link>https://dev.to/ztologame</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ztologame"/>
    <language>en</language>
    <item>
      <title>sql - Mysql Calculate rank of teams from different rows</title>
      <dc:creator>ZtoloGame</dc:creator>
      <pubDate>Wed, 07 Sep 2022 07:28:44 +0000</pubDate>
      <link>https://dev.to/ztologame/sql-mysql-calculate-rank-of-teams-from-different-rows-534n</link>
      <guid>https://dev.to/ztologame/sql-mysql-calculate-rank-of-teams-from-different-rows-534n</guid>
      <description>&lt;p&gt;Question:&lt;br&gt;
I'm trying to build a kind of peddy paper. For that I have the following tables:&lt;/p&gt;

&lt;p&gt;teams&lt;/p&gt;

&lt;p&gt;CREATE TABLE &lt;code&gt;teams&lt;/code&gt; (&lt;br&gt;
  &lt;code&gt;id&lt;/code&gt; int(11) NOT NULL AUTO_INCREMENT,&lt;br&gt;
  &lt;code&gt;creator_id&lt;/code&gt; int(11) NOT NULL,&lt;br&gt;
  &lt;code&gt;friend_id&lt;/code&gt; int(11) DEFAULT NULL,&lt;br&gt;
  &lt;code&gt;team_name&lt;/code&gt; varchar(128) NOT NULL,&lt;br&gt;
  PRIMARY KEY (&lt;code&gt;id&lt;/code&gt;)&lt;br&gt;
);&lt;br&gt;
team_log&lt;/p&gt;

&lt;p&gt;CREATE TABLE IF NOT EXISTS &lt;code&gt;progress_tracker&lt;/code&gt; (&lt;br&gt;
  &lt;code&gt;id&lt;/code&gt; int(8) NOT NULL AUTO_INCREMENT,&lt;br&gt;
  &lt;code&gt;user_id&lt;/code&gt; int(8) NOT NULL,&lt;br&gt;
  &lt;code&gt;team_id&lt;/code&gt; int(11) NOT NULL,&lt;br&gt;
  &lt;code&gt;date&lt;/code&gt; date NOT NULL,&lt;br&gt;
  &lt;code&gt;clues_found&lt;/code&gt; int(11) NOT NULL,&lt;br&gt;
  &lt;code&gt;clues_to_find&lt;/code&gt; int(11) NOT NULL,&lt;br&gt;
  PRIMARY KEY (&lt;code&gt;id&lt;/code&gt;)&lt;br&gt;
);&lt;br&gt;
Each team is composed by two users;&lt;br&gt;
Each user starts out with a variable number of clues found;&lt;br&gt;
clues_found can either increase or decrease. No guarantee that the highest number is the latest;&lt;br&gt;
I need to get a rank of the teams (in percentage) based on the average of the number of clues the user found since they joined (for both users in a team) - clues_found on the row with biggest date minus clues_found on the record with lowest date).&lt;/p&gt;

&lt;p&gt;For instance if I have the following data for each table:&lt;/p&gt;

&lt;p&gt;teams table data&lt;/p&gt;

&lt;p&gt;+--------+------------+------------+---------------+&lt;br&gt;
|     id | creator_id | friend_id  |   team_name   |&lt;br&gt;
+--------+------------+------------+---------------+&lt;br&gt;
|      1 |         25 |         28 |         Test1 |&lt;br&gt;
|      2 |         31 |          5 |         Test2 |&lt;br&gt;
+--------+------------+------------+---------------+&lt;br&gt;
team_log table data&lt;/p&gt;

&lt;p&gt;+--------+---------+---------+------------+-------------+---------------+&lt;br&gt;
|     id | user_id | team_id |    date    | clues_found | clues_to_find |&lt;br&gt;
+--------+---------+---------+------------+-------------+---------------+&lt;br&gt;
|      1 |      25 |       1 | 2013-01-6  |           3 |            24 |&lt;br&gt;
|      2 |      25 |       1 | 2013-01-8  |           7 |            24 |&lt;br&gt;
|      3 |      25 |       1 | 2013-01-10 |          10 |            24 |&lt;br&gt;
|      4 |      28 |       1 | 2013-01-8  |           5 |            30 |&lt;br&gt;
|      5 |      28 |       1 | 2013-01-14 |          20 |            30 |&lt;br&gt;
|      6 |      31 |       2 | 2013-01-11 |           6 |            14 |&lt;br&gt;
|      7 |       5 |       2 | 2013-01-9  |           2 |            20 |&lt;br&gt;
|      8 |       5 |       2 | 2013-01-10 |          10 |            20 |&lt;br&gt;
|      9 |       5 |       2 | 2013-01-12 |          14 |            20 |&lt;br&gt;
+--------+---------+---------+------------+-------------+---------------+&lt;br&gt;
Desired Result&lt;/p&gt;

&lt;p&gt;+-------------+---------------------+&lt;br&gt;
|     team_id |   team_percentage   |&lt;br&gt;
+-------------+---------------------+&lt;br&gt;
|           1 |         39,58333333 |&lt;br&gt;
|           2 |         30          |&lt;br&gt;
+-------------+---------------------+&lt;br&gt;
As a reference this is an intermediate representation which might help to understand:&lt;/p&gt;

&lt;p&gt;+-------------+---------+---------------------+&lt;br&gt;
|     user_id | team_id | precentage_per_user |&lt;br&gt;
+-------------+---------+---------------------+&lt;br&gt;
|          25 |       1 | 29,16666667         |&lt;br&gt;
|          28 |       1 | 50                  |&lt;br&gt;
|          31 |       2 | 0                   |&lt;br&gt;
|           5 |       2 | 60                  |&lt;br&gt;
+-------------+---------+---------------------+&lt;br&gt;
So far I have the following sql:&lt;/p&gt;

&lt;p&gt;SELECT STRAIGHT_JOIN&lt;br&gt;
      tl2.team_id, (tl2.weight - tl1.weight)*100/tl2.clues_to_find&lt;br&gt;
   from &lt;br&gt;
       ( select&lt;br&gt;
               team_id,user_id,clues_found&lt;br&gt;
            FROM &lt;br&gt;
               &lt;code&gt;team_log&lt;/code&gt; &lt;br&gt;
            where 1&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        group by
           team_id, user_id
        order by
           `date` ) base
   join (select team_id, user_id, clues_found, clues_to_find from `team_log` where user_id = base.user_id and team_id = base.team_id group by team_id, user_id order by `date` desc) tl2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;But this returns an error as I'm not allowed to use base.user_id inside the second query. I'm also not very sure I'm heading in the right direction.&lt;/p&gt;

&lt;p&gt;Can anyone help please?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://sqlerrors.com/howto/20272/sql---Mysql-Calculate-rank-of-teams-from-different-rows"&gt;Solution &lt;/a&gt;1:&lt;/p&gt;

&lt;p&gt;Here's another query that will produce the correct result:&lt;/p&gt;

&lt;p&gt;SELECT calc.team_id, AVG((calc.end_clues - calc.start_clues)/calc.total_clues*100) as team_percentage&lt;br&gt;
FROM&lt;br&gt;
    (SELECT log1.user_id, log1.team_id, log1.clues_found as start_clues, log2.clues_found as end_clues, log2.clues_to_find as total_clues FROM team_log log1&lt;br&gt;
    JOIN&lt;br&gt;
    (SELECT MIN(id) as start_id, MAX(id) as end_id FROM team_log GROUP BY user_id) ids&lt;br&gt;
    ON ids.start_id = log1.id&lt;br&gt;
    JOIN team_log log2 ON ids.end_id = log2.id) calc&lt;br&gt;
GROUP BY team_id&lt;br&gt;
ORDER BY team_id;&lt;br&gt;
And the SQL Fiddle-link...&lt;/p&gt;

&lt;p&gt;Solution 2:&lt;/p&gt;

&lt;p&gt;Please take a look at this and comment:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://sqlerrors.com/"&gt;SQL&lt;/a&gt;FIDDLE DEMO&lt;br&gt;
Team pct:&lt;/p&gt;

&lt;p&gt;select z.team_id, avg(z.pct) as teampct&lt;br&gt;
from (&lt;br&gt;
select x.user_id, y.team_id, x.mndate,&lt;br&gt;
y.mxdate, x.mnclues_found,&lt;br&gt;
y.mxclues_found, &lt;br&gt;
(((y.mxclues_found - x.mnclues_found)*100)&lt;br&gt;
/y.mxclues_tofind) pct&lt;br&gt;
from &lt;br&gt;
(select user_id, team_id, min(date) mndate, &lt;br&gt;
min(clues_found) as mnclues_found&lt;br&gt;
from team_log&lt;br&gt;
group by user_id, team_id) x&lt;br&gt;
left join &lt;br&gt;
(select user_id, team_id, max(date) mxdate, &lt;br&gt;
max(clues_found) as mxclues_found, &lt;br&gt;
 max(clues_to_find) as mxclues_tofind&lt;br&gt;
from team_log&lt;br&gt;
group by user_id, team_id) y&lt;br&gt;
on x.user_id = y.user_id and&lt;br&gt;
x.team_id = y.team_id) z&lt;br&gt;
group by z.team_id&lt;br&gt;
;&lt;br&gt;
Results 1:&lt;/p&gt;

&lt;h2&gt;
  
  
  | USER_ID | TEAM_ID |   MNDATE |   MXDATE | MNCLUES_FOUND | MXCLUES_FOUND |     PCT |
&lt;/h2&gt;

&lt;p&gt;|       5 |       2 | 13-01-09 | 13-01-12 |             2 |            14 |      60 |&lt;br&gt;
|      25 |       1 | 13-01-06 | 13-01-10 |             3 |            10 | 29.1667 |&lt;br&gt;
|      28 |       1 | 13-01-08 | 13-01-14 |             5 |            20 |      50 |&lt;br&gt;
|      31 |       2 | 13-01-11 | 13-01-11 |             6 |             6 |       0 |&lt;br&gt;
Results final:&lt;/p&gt;

&lt;h2&gt;
  
  
  | TEAM_ID |  TEAMPCT |
&lt;/h2&gt;

&lt;p&gt;|       1 | 39.58335 |&lt;br&gt;
|       2 |       30 |&lt;br&gt;
Solution 3:&lt;/p&gt;

&lt;p&gt;This is a bit ugly, but should work:&lt;/p&gt;

&lt;p&gt;select&lt;br&gt;
   team_id,&lt;br&gt;
   AVG(percentage_per_user) as team_percentage&lt;br&gt;
from (select&lt;br&gt;
  team_id,&lt;br&gt;
  user_id,&lt;br&gt;
  ((select clues_found from progress_tracker as x&lt;br&gt;
      where x.user_id = m.user_id order by x.date desc limit 0, 1)&lt;br&gt;
    - (select clues_found from progress_tracker as y&lt;br&gt;
      where y.user_id = m.user_id order by y.date asc limit 0, 1))&lt;br&gt;
  / MAX(clues_to_find)&lt;br&gt;
  as percentage_per_user&lt;br&gt;
from progress_tracker as m&lt;br&gt;
group by team_id, user_id&lt;br&gt;
) as userScore&lt;br&gt;
group by team_id&lt;br&gt;
order by team_percentage desc;&lt;/p&gt;

</description>
      <category>sql</category>
    </item>
    <item>
      <title>sql — Select Records multiple times from table</title>
      <dc:creator>ZtoloGame</dc:creator>
      <pubDate>Fri, 02 Sep 2022 09:33:53 +0000</pubDate>
      <link>https://dev.to/ztologame/sql-select-records-multiple-times-from-table-12g7</link>
      <guid>https://dev.to/ztologame/sql-select-records-multiple-times-from-table-12g7</guid>
      <description>&lt;p&gt;Question:&lt;br&gt;
I have a &lt;a href="https://sqlerrors.com/"&gt;SQL&lt;/a&gt; statement like this:&lt;/p&gt;

&lt;p&gt;EDIT :&lt;/p&gt;

&lt;p&gt;SELECT &lt;br&gt;
    location as Location &lt;br&gt;
FROM&lt;br&gt;
    Table1 &lt;br&gt;
WHERE &lt;br&gt;
    OnsiteOffshore = 'Offshore' AND Acc_Code = 'ABC' &lt;/p&gt;

&lt;p&gt;UNION &lt;/p&gt;

&lt;p&gt;SELECT &lt;br&gt;
    Country &lt;br&gt;
FROM &lt;br&gt;
    Table1 &lt;br&gt;
WHERE&lt;br&gt;
    OnsiteOffshore = 'Onsite' AND Acc_Code = 'ABC'&lt;br&gt;
This SQL query gives these results:&lt;/p&gt;

&lt;p&gt;Chennai&lt;br&gt;
Bangalore&lt;br&gt;
USA&lt;br&gt;
NewZealand&lt;br&gt;
But due to some requirement I need the output like this:&lt;/p&gt;

&lt;p&gt;Chennai&lt;br&gt;
Chennai&lt;br&gt;
Chennai&lt;br&gt;
Chennai&lt;br&gt;
Bangalore&lt;br&gt;
Bangalore&lt;br&gt;
Bangalore&lt;br&gt;
Bangalore&lt;br&gt;
USA&lt;br&gt;
USA&lt;br&gt;
USA&lt;br&gt;
USA&lt;br&gt;
NewZealand&lt;br&gt;
NewZealand&lt;br&gt;
NewZealand&lt;br&gt;
NewZealand&lt;br&gt;
Mean to say each location needs to be output 4 times.&lt;/p&gt;

&lt;p&gt;Pls help how to get the same.&lt;/p&gt;

&lt;p&gt;Solution 1:&lt;/p&gt;

&lt;p&gt;SELECT Location &lt;br&gt;
FROM Table1&lt;br&gt;
  CROSS JOIN&lt;br&gt;
    ( VALUES (1),(2),(3),(4)&lt;br&gt;
    ) AS four(dummy)&lt;br&gt;
If the 4 is not a constant but (as @xQbert noticed/asked) is the number of rows of the table, you can use this:&lt;/p&gt;

&lt;p&gt;SELECT a.Location &lt;br&gt;
FROM Table1 AS a&lt;br&gt;
  CROSS JOIN&lt;br&gt;
     Table1 AS b&lt;br&gt;
If you don't have Table1 but any (however complex) query, you could use this for 4 copies:&lt;/p&gt;

&lt;p&gt;SELECT Location &lt;br&gt;
FROM (&lt;br&gt;
       SELECT Location       --- complex query here&lt;br&gt;
       ...                   --- inside parenthesis&lt;br&gt;
     UNION &lt;br&gt;
       SELECT Country&lt;br&gt;
       ...&lt;br&gt;
     ) AS Table1&lt;br&gt;
  CROSS JOIN&lt;br&gt;
    ( VALUES (1),(2),(3),(4)&lt;br&gt;
    ) AS four(dummy)&lt;br&gt;
or this for n copies:&lt;/p&gt;

&lt;p&gt;WITH cte AS&lt;br&gt;
  ( SELECT Location       --- complex query here&lt;br&gt;
    ...                   --- inside parenthesis&lt;br&gt;
    UNION &lt;br&gt;
    SELECT Country&lt;br&gt;
    ...&lt;br&gt;
  )&lt;br&gt;
SELECT a.Location &lt;br&gt;
FROM cte AS a&lt;br&gt;
  CROSS JOIN&lt;br&gt;
     cte AS b&lt;br&gt;
Solution 2:&lt;/p&gt;

&lt;p&gt;Simplest and (probably) fully accepted in any RDBMS ;-):&lt;/p&gt;

&lt;p&gt;select location from (&lt;br&gt;
    Select Location From Table1 union all&lt;br&gt;
    Select Location From Table1 union all&lt;br&gt;
    Select Location From Table1 union all&lt;br&gt;
    Select Location From Table1&lt;br&gt;
) t&lt;br&gt;
order by location&lt;br&gt;
And better way with CTE (Common Table Expressions):&lt;/p&gt;

&lt;p&gt;;with cte (id) as (&lt;br&gt;
    select 1 union all&lt;br&gt;
    select id + 1 from cte where id &amp;lt; 4&lt;br&gt;
)&lt;br&gt;
select location from Table1&lt;br&gt;
cross join cte&lt;br&gt;
Solution 3:&lt;/p&gt;

&lt;p&gt;You can create a wrapper procedure for this... where u first create a cursor for Select Location From Table1 than you can loop through this cursor and extract the data whatever way want.&lt;/p&gt;

&lt;p&gt;optionally you can use some front end technology to do this. This is not a tough task in front end languages i.e. Java, C++, .NET or in any other popular language.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://sqlerrors.com/"&gt;SQL SELECT from multiple tables&lt;/a&gt;&lt;br&gt;
Question:&lt;br&gt;
How can I get all products from customers1 and customers2 include their customer names?&lt;/p&gt;

&lt;p&gt;customer1 table&lt;br&gt;
cid name1&lt;br&gt;
1   john&lt;br&gt;
2   joe&lt;/p&gt;

&lt;p&gt;customer2 table&lt;br&gt;
cid name2&lt;br&gt;
p1  sandy&lt;br&gt;
p2  linda&lt;/p&gt;

&lt;p&gt;product table&lt;br&gt;
pid cid pname&lt;br&gt;
1   1   phone&lt;br&gt;
2   2   pencil&lt;br&gt;
3   p1  pen&lt;br&gt;
4   p2  paper&lt;br&gt;
Result should be like this&lt;/p&gt;

&lt;p&gt;pid  cid  pname  name1 name2&lt;br&gt;
1    1    phone  john  NULL&lt;br&gt;
2    2    pencil joe   NULL&lt;br&gt;
3    p1   pen    NULL  sandy&lt;br&gt;
4    p2   paper  NULL  linda&lt;/p&gt;

&lt;p&gt;Solution 1:&lt;/p&gt;

&lt;p&gt;SELECT p.pid, p.cid, p.pname, c1.name1, c2.name2&lt;br&gt;
FROM product p&lt;br&gt;
LEFT JOIN customer1 c1 ON p.cid = c1.cid&lt;br&gt;
LEFT JOIN customer2 c2 ON p.cid = c2.cid&lt;br&gt;
Solution 2:&lt;/p&gt;

&lt;p&gt;SELECT pid, cid, pname, name1, name2 &lt;br&gt;
FROM customer1 c1, product p &lt;br&gt;
WHERE p.cid=c1.cid &lt;br&gt;
UNION SELECT pid, cid, pname, name1, name2 &lt;br&gt;
FROM customer2 c2, product p &lt;br&gt;
WHERE p.cid=c2.cid;&lt;br&gt;
Solution 3:&lt;/p&gt;

&lt;p&gt;SELECT &lt;code&gt;product&lt;/code&gt;.*, &lt;code&gt;customer1&lt;/code&gt;.&lt;code&gt;name1&lt;/code&gt;, &lt;code&gt;customer2&lt;/code&gt;.&lt;code&gt;name2&lt;/code&gt;&lt;br&gt;
FROM &lt;code&gt;product&lt;/code&gt;&lt;br&gt;
LEFT JOIN &lt;code&gt;customer1&lt;/code&gt; ON &lt;code&gt;product&lt;/code&gt;.&lt;code&gt;cid&lt;/code&gt; = &lt;code&gt;customer1&lt;/code&gt;.&lt;code&gt;cid&lt;/code&gt;&lt;br&gt;
LEFT JOIN &lt;code&gt;customer2&lt;/code&gt; ON &lt;code&gt;product&lt;/code&gt;.&lt;code&gt;cid&lt;/code&gt; = &lt;code&gt;customer2&lt;/code&gt;.&lt;code&gt;cid&lt;/code&gt;&lt;/p&gt;

</description>
      <category>sql</category>
    </item>
    <item>
      <title>python - How to Count bugs in an image?</title>
      <dc:creator>ZtoloGame</dc:creator>
      <pubDate>Fri, 02 Sep 2022 09:25:15 +0000</pubDate>
      <link>https://dev.to/ztologame/python-how-to-count-bugs-in-an-image-4idh</link>
      <guid>https://dev.to/ztologame/python-how-to-count-bugs-in-an-image-4idh</guid>
      <description>&lt;p&gt;Question:&lt;br&gt;
I have a picture like the below and I would like to count the number of bugs (continuous blobs of color/grey) that show up on it with &lt;a href="https://pythonhowto.org/"&gt;Python&lt;/a&gt;. How could I do this best?&lt;/p&gt;

&lt;p&gt;Bugs on a noisy background&lt;/p&gt;

&lt;p&gt;I've so far looked at ImageChops, SciPy and PIL but I'm unsure what I can/should use...&lt;/p&gt;

&lt;p&gt;I think I can use ndimage.gaussian_filter() and then scipy.ndimage.measurements.label() am just not sure yet how I use latter to count my blue dots in the gaussian-ized image...... it looks something likeenter image description here&lt;/p&gt;

&lt;p&gt;Okay,&lt;/p&gt;

&lt;p&gt;With above image I now got this code:&lt;/p&gt;

&lt;h1&gt;
  
  
  ! /usr/bin/python
&lt;/h1&gt;

&lt;p&gt;import numpy as np&lt;br&gt;
import scipy&lt;br&gt;
import pylab&lt;br&gt;
import pymorph&lt;br&gt;
import mahotas&lt;br&gt;
from PIL import Image&lt;br&gt;
import PIL.ImageOps&lt;br&gt;
from scipy import ndimage&lt;/p&gt;

&lt;p&gt;image = Image.open('bugs.jpg')&lt;br&gt;&lt;br&gt;
inverted_image = PIL.ImageOps.invert(image)&lt;br&gt;&lt;br&gt;
inverted_image.save('in_bugs.jpg')&lt;br&gt;
dna = mahotas.imread('in_bugs.jpg')&lt;/p&gt;

&lt;h1&gt;
  
  
  pylab.imshow(dna)
&lt;/h1&gt;

&lt;p&gt;pylab.gray()&lt;/p&gt;

&lt;h1&gt;
  
  
  pylab.show()
&lt;/h1&gt;

&lt;p&gt;T = mahotas.thresholding.otsu(dna)&lt;br&gt;
pylab.imshow(dna &amp;gt; T)&lt;/p&gt;

&lt;h1&gt;
  
  
  pylab.show()
&lt;/h1&gt;

&lt;p&gt;dnaf = ndimage.gaussian_filter(dna, 8)&lt;br&gt;
T = mahotas.thresholding.otsu(dnaf)&lt;br&gt;
pylab.imshow(dnaf &amp;gt; T)&lt;/p&gt;

&lt;h1&gt;
  
  
  pylab.show()
&lt;/h1&gt;

&lt;p&gt;labeled,nr_objects = ndimage.label(dnaf &amp;gt; T)&lt;br&gt;
print nr_objects&lt;br&gt;
pylab.imshow(labeled)&lt;br&gt;
pylab.jet()&lt;br&gt;
pylab.show()&lt;br&gt;
the problem is, this returns me a number of 5 which isn'rt that bad but I need to have it more accurate, I want to see two. How can I do this? Will it help to blur the image before applying the gaussian filter?&lt;/p&gt;

&lt;p&gt;Thanks for help!&lt;/p&gt;

&lt;p&gt;Ron&lt;/p&gt;

&lt;p&gt;Solution 1:&lt;/p&gt;

&lt;p&gt;Your gaussian filtering is mostly fine already, but you are considering a radius way bigger than needed for the task. For instance, let us consider a kernel of radius 15 as an example. Here is a representation of what we get:&lt;/p&gt;

&lt;p&gt;enter image description here&lt;/p&gt;

&lt;p&gt;There are two clear valleys (yes, shown as peaks instead) and the histogram of the filtered image shows that most of the data available is now close to the maximum value possible.&lt;/p&gt;

&lt;p&gt;enter image description here&lt;/p&gt;

&lt;p&gt;Considering only a part of the histogram we can better see the part of the data we are interested: the darker spots.&lt;/p&gt;

&lt;p&gt;enter image description here&lt;/p&gt;

&lt;p&gt;So, with a simple threshold at 0.5 this is the result (which matches where the bugs are):&lt;/p&gt;

&lt;p&gt;enter image description here&lt;/p&gt;

&lt;p&gt;Depending on how you implement (or the libraries you use implement) the related functions, this threshold value will vary. But by looking at the histogram you will be able to spot a good threshold. Now, if you don't want to guess this threshold by looking at the histogram then you need to preprocess your image beyond the gaussian filtering. By doing this step in a good manner, your image gets simple enough that methods like the one given by Otsu can automatically find the threshold you are after. Performing a morphological closing followed by a morphological opening, and then binarizing by Otsu, this is the result we get:&lt;/p&gt;

&lt;p&gt;enter image description here&lt;/p&gt;

&lt;p&gt;The shapes are closer to the initial ones, since we didn't rely on a linear low-pass filter which at best will blur contours.&lt;/p&gt;

&lt;p&gt;EDIT:&lt;/p&gt;

&lt;p&gt;Since the question now includes some code, I felt the need to explain why it is wrong to use Otsu as the code there is doing. The method given by Otsu for thresholding actually expects the data to be bimodal but, as the histogram plots above shows, this is not the case here. Otsu will provide a threshold that is too close to the huge peak at the right, while the good point at 0.5 is very far from there. To replicate the first result shown in this answer, here is some basic code:&lt;/p&gt;

&lt;p&gt;import sys&lt;br&gt;
import numpy&lt;br&gt;
from PIL import Image&lt;br&gt;
from scipy.ndimage import gaussian_filter, label&lt;/p&gt;

&lt;p&gt;img = Image.open(sys.argv[1]).convert('L')&lt;br&gt;
im = numpy.array(img)&lt;/p&gt;

&lt;p&gt;im_g = gaussian_filter(im, 3)&lt;br&gt;
im_norm = (im_g - im_g.min()) / (float(im_g.max()) - im_g.min())&lt;br&gt;
im_norm[im_norm &amp;lt; 0.5] = 0&lt;br&gt;
im_norm[im_norm &amp;gt;= 0.5] = 1&lt;/p&gt;

&lt;p&gt;result = 255 - (im_norm * 255).astype(numpy.uint8)&lt;br&gt;
print u"Objects: %d" % label(result)[1]&lt;/p&gt;

&lt;p&gt;Image.fromarray(result).save(sys.argv[2])&lt;br&gt;
Note that this code is using sigma = 3 (while originally 7.5 was used) for the gaussian kernel, from which scipy internally builds a window with a radius 4 times larger than it. For this particular image, a great range of sigma works just as well, starting from 2 up to 10 should give the same results -- 2 objects detected.&lt;/p&gt;

&lt;p&gt;Solution 2:&lt;/p&gt;

&lt;p&gt;This is a task of computer vision, which you could solve with the popular OpenCV.&lt;/p&gt;

&lt;p&gt;You might want to consider processing the image with some morphological operations (e.g. opening) to remove the noise. Perhaps then you could count the number of contiguous blobs with area above some threshold.&lt;/p&gt;

&lt;p&gt;Some resources to look into:&lt;/p&gt;

&lt;p&gt;Object recognition (Wikipedia)&lt;br&gt;
Computer vision (Wikipedia)&lt;br&gt;
OpenCV Cookbook&lt;/p&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>git — Fatal: Not possible to fast-forward, aborting</title>
      <dc:creator>ZtoloGame</dc:creator>
      <pubDate>Fri, 02 Sep 2022 09:13:18 +0000</pubDate>
      <link>https://dev.to/ztologame/git-fatal-not-possible-to-fast-forward-aborting-19ek</link>
      <guid>https://dev.to/ztologame/git-fatal-not-possible-to-fast-forward-aborting-19ek</guid>
      <description>&lt;p&gt;&lt;a href="https://www.thecodeteacher.com/question/17556/git---Fatal:-Not-possible-to-fast-forward,-aborting"&gt;Top 5 Answer for git — Fatal: Not possible to fast-forward, aborting&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;1&lt;/p&gt;

&lt;p&gt;git pull --rebase. Unlike the other solution, you don't need to know the name of your destination branch.&lt;/p&gt;

&lt;p&gt;If your upstream branch is not set, try git pull origin  --rebase (credit to &lt;a class="mentioned-user" href="https://dev.to/rick"&gt;@rick&lt;/a&gt; in the comments)&lt;/p&gt;

&lt;p&gt;2&lt;/p&gt;

&lt;p&gt;Your branch is no longer directly based off of the branch you’re trying to merge it into — e.g. another commit was added to the destination branch that isn’t in your branch. Thus, you can’t fast-forward into it (because fast-forward requires your branch to completely contain the destination branch).&lt;/p&gt;

&lt;p&gt;You can rebase your branch on top of the destination branch (git rebase ) to rework the commits such that they will fast forward into it, or you can do a regular merge.&lt;/p&gt;

&lt;p&gt;If&lt;br&gt;
git pull&lt;br&gt;
does not do the trick and if you want to merge both the current changes and the changes that’d come from the pull of the branch from origin then do this:-&lt;/p&gt;

&lt;p&gt;git merge origin/BRANCH_NAME&lt;br&gt;
After that, resolve the merge conflicts if any and done for the day.&lt;br&gt;
3&lt;/p&gt;

&lt;p&gt;This is because you have enabled the fast-forward only option. The thing here is your pull from the branch will create a merge commit in your local git and the fast-forward only option doesn’t allow creating a merge commit at the time of pull.&lt;/p&gt;

&lt;p&gt;In the case of a big team, You will end up rebasing and resolving conflicts lots of the time and for each and every commit coming from the pull.&lt;/p&gt;

&lt;p&gt;I suggest you remove ff = only line from git local config file.&lt;/p&gt;

&lt;p&gt;$ cd to-my-project-root-dir&lt;/p&gt;

&lt;p&gt;$ nano .git/config&lt;/p&gt;

&lt;p&gt;[pull]         ff = only // remove this line         rebase = false&lt;br&gt;
5&lt;/p&gt;

&lt;p&gt;git pull --no-ff -&amp;gt; make fast-forwarding to be off by --no-ff&lt;/p&gt;

</description>
      <category>github</category>
      <category>git</category>
    </item>
    <item>
      <title>android - Get root view from current activity</title>
      <dc:creator>ZtoloGame</dc:creator>
      <pubDate>Thu, 18 Aug 2022 10:19:20 +0000</pubDate>
      <link>https://dev.to/ztologame/android-get-root-view-from-current-activity-4995</link>
      <guid>https://dev.to/ztologame/android-get-root-view-from-current-activity-4995</guid>
      <description>&lt;p&gt;Question from &lt;a href="https://androidandrey.com/"&gt;androidandrey&lt;/a&gt;:&lt;br&gt;
I know how to get the root view with View.getRootView(). I am also able to get the view from a button's onClick event where the argument is a View. But how can I get the view in an activity?&lt;/p&gt;

&lt;p&gt;Solution 1:&lt;/p&gt;

&lt;p&gt;If you need root view of your activity (so you can add your contents there) use&lt;/p&gt;

&lt;p&gt;findViewById(android.R.id.content).getRootView()&lt;br&gt;
Also it was reported that on some devices you have to use&lt;/p&gt;

&lt;p&gt;getWindow().getDecorView().findViewById(android.R.id.content)&lt;br&gt;
instead.&lt;/p&gt;

&lt;p&gt;Please note that as Booger reported, this may be behind navigation bar (with back button etc.) on some devices (but it seems on most devices it is not).&lt;/p&gt;

&lt;p&gt;If you need to get view that you added to your activity using setContentView() method then as pottedmeat wrote you can use&lt;/p&gt;

&lt;p&gt;final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this&lt;br&gt;
            .findViewById(android.R.id.content)).getChildAt(0);&lt;br&gt;
But better just set id to this view in your xml layout and use this id instead.&lt;/p&gt;

&lt;p&gt;Solution 2:&lt;/p&gt;

&lt;p&gt;This is what I use to get the root view as found in the XML file assigned with setContentView:&lt;/p&gt;

&lt;p&gt;final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this&lt;br&gt;
            .findViewById(android.R.id.content)).getChildAt(0);&lt;br&gt;
Solution 3:&lt;/p&gt;

&lt;p&gt;I tested this in android 4.0.3, only:&lt;/p&gt;

&lt;p&gt;getWindow().getDecorView().getRootView()&lt;br&gt;
give the same view what we get from&lt;/p&gt;

&lt;p&gt;anyview.getRootView();&lt;/p&gt;

&lt;p&gt;com.android.internal.policy.impl.PhoneWindow$DecorView@#########&lt;br&gt;
and&lt;/p&gt;

&lt;p&gt;getWindow().getDecorView().findViewById(android.R.id.content)&lt;br&gt;
giving child of its&lt;/p&gt;

&lt;p&gt;android.widget.FrameLayout@#######&lt;br&gt;
Please confirm &lt;a href="https://androidandrey.com/howto/57/android---Get-root-view-from-current-activity"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>android</category>
    </item>
    <item>
      <title>mysql — How to get the count of each distinct value in a column?</title>
      <dc:creator>ZtoloGame</dc:creator>
      <pubDate>Wed, 17 Aug 2022 09:56:06 +0000</pubDate>
      <link>https://dev.to/ztologame/mysql-how-to-get-the-count-of-each-distinct-value-in-a-column-4l4i</link>
      <guid>https://dev.to/ztologame/mysql-how-to-get-the-count-of-each-distinct-value-in-a-column-4l4i</guid>
      <description>&lt;p&gt;Question:&lt;br&gt;
This question already has answers here : &lt;a href="https://sqlerrors.com/howto/392/mysql---How-to-get-the-count-of-each-distinct-value-in-a-column?#mysql---How-to-get-the-count-of-each-distinct-value-in-a-column?"&gt;https://sqlerrors.com/howto/392/mysql---How-to-get-the-count-of-each-distinct-value-in-a-column?#mysql---How-to-get-the-count-of-each-distinct-value-in-a-column?&lt;/a&gt;&lt;br&gt;
Count the occurrences of DISTINCT values (3 answers)&lt;br&gt;
Closed last year at : &lt;a href="https://sqlerrors.com/"&gt;https://sqlerrors.com/&lt;/a&gt;.&lt;br&gt;
I have an SQL table called "posts" that looks like this:&lt;/p&gt;

&lt;h2&gt;
  
  
  id | category
&lt;/h2&gt;

&lt;p&gt;1  | 3&lt;br&gt;
2  | 1&lt;br&gt;
3  | 4&lt;br&gt;
4  | 2&lt;br&gt;
5  | 1&lt;br&gt;
6  | 1&lt;br&gt;
7  | 2&lt;br&gt;
Each category number corresponds to a category. How would I go about counting the number of times each category appears on a post all in one SQL query?&lt;/p&gt;

&lt;p&gt;As an example, such a query might return a symbolic array such as this: (1:3, 2:2, 3:1, 4:1)&lt;/p&gt;

&lt;p&gt;My current method is to use queries for each possible category, such as: SELECT COUNT(*) AS num FROM posts WHERE category=#, and then combine the return values into a final array. However, I'm looking for a solution that uses only one query.&lt;/p&gt;

&lt;p&gt;Solution 1:&lt;/p&gt;

&lt;p&gt;SELECT&lt;br&gt;
  category,&lt;br&gt;
  COUNT(*) AS &lt;code&gt;num&lt;/code&gt;&lt;br&gt;
FROM&lt;br&gt;
  posts&lt;br&gt;
GROUP BY&lt;br&gt;
  category&lt;br&gt;
mysql - How to count the number of instances of each foreign-key ID in a table?&lt;br&gt;
Question:&lt;br&gt;
Here's my simple SQL question...&lt;/p&gt;

&lt;p&gt;I have two tables:&lt;/p&gt;

&lt;p&gt;Books&lt;/p&gt;




&lt;h2&gt;
  
  
  | book_id | author | genre | price | publication_date |
&lt;/h2&gt;

&lt;p&gt;Orders&lt;/p&gt;




&lt;h2&gt;
  
  
  | order_id | customer_id | book_id |
&lt;/h2&gt;

&lt;p&gt;I'd like to create a query that returns:&lt;/p&gt;




&lt;h2&gt;
  
  
  | book_id | author | genre | price | publication_date | number_of_orders |
&lt;/h2&gt;

&lt;p&gt;In other words, return every column for ALL rows in the Books table, along with a calculated column named 'number_of_orders' that counts the number of times each book appears in the Orders table. (If a book does not occur in the orders table, the book should be listed in the result set, but "number_of_orders" should be zero.&lt;/p&gt;

&lt;p&gt;So far, I've come up with this:&lt;/p&gt;

&lt;p&gt;SELECT&lt;br&gt;
    books.book_id,&lt;br&gt;
    books.author,&lt;br&gt;
    books.genre,&lt;br&gt;
    books.price,&lt;br&gt;
    books.publication_date,&lt;br&gt;
    count(*) as number_of_orders&lt;br&gt;
from books&lt;br&gt;
left join orders&lt;br&gt;
on (books.book_id = orders.book_id)&lt;br&gt;
group by&lt;br&gt;
    books.book_id,&lt;br&gt;
    books.author,&lt;br&gt;
    books.genre,&lt;br&gt;
    books.price,&lt;br&gt;
    books.publication_date&lt;br&gt;
That's almost right, but not quite, because "number_of_orders" will be 1 even if a book is never listed in the Orders table. Moreover, given my lack of knowledge of SQL, I'm sure this query is very inefficient.&lt;/p&gt;

&lt;p&gt;What's the right way to write this query? (For what it's worth, this needs to work on MySQL, so I can't use any other vendor-specific features).&lt;/p&gt;

&lt;p&gt;Thanks in advance!&lt;/p&gt;

&lt;p&gt;Solution 1:&lt;/p&gt;

&lt;p&gt;Your query is almost right and it's the right way to do that (and the most efficient)&lt;/p&gt;

&lt;p&gt;SELECT books.&lt;em&gt;, count(orders.book_id) as number_of_orders&lt;br&gt;&lt;br&gt;
from books&lt;br&gt;
left join orders&lt;br&gt;
on (books.book_id = orders.book_id)&lt;br&gt;
group by&lt;br&gt;
    books.book_id&lt;br&gt;
COUNT(&lt;/em&gt;) could include NULL values in the count because it counts all the rows, while COUNT(orders.book_id) does not because it ignores NULL values in the given field.&lt;/p&gt;

&lt;p&gt;Solution 2:&lt;/p&gt;

&lt;p&gt;SELECT b.book_id,&lt;br&gt;
    b.author,&lt;br&gt;
    b.genre,&lt;br&gt;
    b.price,&lt;br&gt;
    b.publication_date,&lt;br&gt;
    coalesce(oc.Count, 0) as number_of_orders&lt;br&gt;
from books b&lt;br&gt;
left join (&lt;br&gt;
    select book_id, count(*) as Count &lt;br&gt;
    from Order &lt;br&gt;
    group by book_id&lt;br&gt;
) oc on (b.book_id = oc.book_id)&lt;br&gt;
Solution 3:&lt;/p&gt;

&lt;p&gt;Change count(*) to count(orders.book_id)&lt;/p&gt;

</description>
      <category>mysql</category>
    </item>
    <item>
      <title>python - How to display a message box on PyQT4?</title>
      <dc:creator>ZtoloGame</dc:creator>
      <pubDate>Wed, 17 Aug 2022 09:41:09 +0000</pubDate>
      <link>https://dev.to/ztologame/python-how-to-display-a-message-box-on-pyqt4-4gcl</link>
      <guid>https://dev.to/ztologame/python-how-to-display-a-message-box-on-pyqt4-4gcl</guid>
      <description>&lt;p&gt;Question:&lt;br&gt;
I'd like a MessageBox to display when I click a button on my simple PyQT application. How can I declare two textboxes and have a MessageBox display with the text from both textboxes?&lt;/p&gt;

&lt;p&gt;Here's my code far:&lt;/p&gt;

&lt;p&gt;import sys&lt;br&gt;
from PyQt4 import QtGui, QtCore&lt;/p&gt;

&lt;p&gt;class myWindow(QtGui.QWidget):&lt;br&gt;
    def &lt;strong&gt;init&lt;/strong&gt;(self, parent=None):&lt;br&gt;
        QtGui.QWidget.&lt;strong&gt;init&lt;/strong&gt;(self, parent)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    #The setGeometry method is used to position the control.
    #Order: X, Y position - Width, Height of control.
    self.setGeometry(300, 300, 500, 350)
    self.setWindowTitle("Sergio's QT Application.")
    self.setWindowIcon(QtGui.QIcon('menuScreenFolderShadow.png'))

    self.setToolTip('&amp;lt;i&amp;gt;Welcome&amp;lt;/i&amp;gt; to the &amp;lt;b&amp;gt;first&amp;lt;/b&amp;gt; app ever!')
    QtGui.QToolTip.setFont(QtGui.QFont('Helvetica', 12))

    txtFirstName = QtGui.?
    txtLastName = QtGui.?

    btnQuit = QtGui.QPushButton('Exit Application', self)
    btnQuit.setGeometry(340, 300, 150, 35)

    self.connect(btnQuit, QtCore.SIGNAL('clicked()'),
                QtGui.qApp, QtCore.SLOT('quit()'))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;app = QtGui.QApplication(sys.argv)&lt;br&gt;
mainForm = myWindow()&lt;br&gt;
mainForm.show()&lt;br&gt;
sys.exit(app.exec_())&lt;/p&gt;

&lt;p&gt;Solution 1:&lt;/p&gt;

&lt;p&gt;Since such simple code is a common request, I decided to hack something basic together, here you go:&lt;/p&gt;

&lt;p&gt;from PyQt4.QtCore import *&lt;br&gt;
from PyQt4.QtGui import *&lt;/p&gt;

&lt;p&gt;class AppForm(QMainWindow):&lt;br&gt;
    def &lt;strong&gt;init&lt;/strong&gt;(self, parent=None):&lt;br&gt;
        QMainWindow.&lt;strong&gt;init&lt;/strong&gt;(self, parent)&lt;br&gt;
        self.create_main_frame()       &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def create_main_frame(self):        
    page = QWidget()        

    self.button = QPushButton('joy', page)
    self.edit1 = QLineEdit()
    self.edit2 = QLineEdit()

    vbox1 = QVBoxLayout()
    vbox1.addWidget(self.edit1)
    vbox1.addWidget(self.edit2)
    vbox1.addWidget(self.button)
    page.setLayout(vbox1)
    self.setCentralWidget(page)

    self.connect(self.button, SIGNAL("clicked()"), self.clicked)

def clicked(self):
    QMessageBox.about(self, "My message box", "Text1 = %s, Text2 = %s" % (
        self.edit1.text(), self.edit2.text()))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;if &lt;strong&gt;name&lt;/strong&gt; == "&lt;strong&gt;main&lt;/strong&gt;":&lt;br&gt;
    import sys&lt;br&gt;
    app = QApplication(sys.argv)&lt;br&gt;
    form = AppForm()&lt;br&gt;
    form.show()&lt;br&gt;
    app.exec_()&lt;br&gt;
Write something into the line edits (text boxes), click the button. Profit! :-)&lt;/p&gt;

&lt;p&gt;Note: it can be done with less code, but this is a good PyQt coding practice - create a widget to serve as a central widget of a window, populate it with a layout, etc.&lt;/p&gt;

&lt;p&gt;Solution 2:&lt;/p&gt;

&lt;p&gt;PyQt comes with examples when you install it. These examples contain a lot of very useful code and you can learn from them, as well as take whole code chunks and use them.&lt;/p&gt;

&lt;p&gt;Check out, for example, the "Address book" example which pops message boxes along other things (search its sources for "messagebox").&lt;/p&gt;

&lt;p&gt;Solution 3:&lt;/p&gt;

&lt;p&gt;enter image description here&lt;/p&gt;

&lt;p&gt;from PyQt4 import QtGui, QtCore&lt;/p&gt;

&lt;p&gt;class Window( QtGui.QWidget ):&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def __init__( self ):
    QtGui.QWidget.__init__( self )

    msgBox = QtGui.QMessageBox( self )
    msgBox.setIcon( QtGui.QMessageBox.Information )
    msgBox.setText( "Do not stare into laser with remaining eye" )

    msgBox.setInformativeText( "Do you really want to disable safety enforcement?" )
    msgBox.addButton( QtGui.QMessageBox.Yes )
    msgBox.addButton( QtGui.QMessageBox.No )

    msgBox.setDefaultButton( QtGui.QMessageBox.No ) 
    ret = msgBox.exec_()

    if ret == QtGui.QMessageBox.Yes:
        print( "Yes" )
        return
    else:
        print( "No" )
        return
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;if &lt;strong&gt;name&lt;/strong&gt; == '&lt;strong&gt;main&lt;/strong&gt;':&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import sys
app = QtGui.QApplication( sys.argv )
window = Window()
# window.show()
sys.exit( app.exec_() )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Source:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pythonhowto.org/howto/22358/python---How-to-display-a-message-box-on-PyQT4?"&gt;https://pythonhowto.org/howto/22358/python---How-to-display-a-message-box-on-PyQT4?&lt;/a&gt;&lt;br&gt;
&lt;a href="https://pythonhowto.org/"&gt;pythonhowto&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>pyqt4</category>
    </item>
    <item>
      <title>sql — Add primary key to PostgreSQL table only if it does not exist</title>
      <dc:creator>ZtoloGame</dc:creator>
      <pubDate>Mon, 15 Aug 2022 10:49:38 +0000</pubDate>
      <link>https://dev.to/ztologame/sql-add-primary-key-to-postgresql-table-only-if-it-does-not-exist-2ikh</link>
      <guid>https://dev.to/ztologame/sql-add-primary-key-to-postgresql-table-only-if-it-does-not-exist-2ikh</guid>
      <description>&lt;p&gt;Question:&lt;/p&gt;

&lt;p&gt;I have simple table creating script in Postgres 9.1. I need it to create the table with 2-attributes PK only if it does not exist.&lt;/p&gt;

&lt;p&gt;CREATE TABLE IF NOT EXISTS "mail_app_recipients"&lt;br&gt;
(&lt;br&gt;
    "id_draft" Integer NOT NULL,&lt;br&gt;
    "id_person" Integer NOT NULL&lt;br&gt;
) WITH (OIDS=FALSE); -- this is OK&lt;br&gt;
ALTER TABLE "mail_app_recipients" ADD PRIMARY KEY IF NOT EXISTS ("id_draft","id_person");&lt;br&gt;
-- this is problem since "IF NOT EXISTS" is not allowed.&lt;br&gt;
Any solution how to solve this problem? Thanks in advance.&lt;/p&gt;

&lt;p&gt;Solution 1:&lt;/p&gt;

&lt;p&gt;You could do something like the following, however it is better to include it in the create table as a_horse_with_no_name suggests.&lt;/p&gt;

&lt;p&gt;if NOT exists (select constraint_name from information_schema.table_constraints where table_name = 'table_name' and constraint_type = 'PRIMARY KEY') then&lt;br&gt;
ALTER TABLE table_name&lt;br&gt;
  ADD PRIMARY KEY (id);&lt;br&gt;
end if;&lt;br&gt;
Solution 2:&lt;/p&gt;

&lt;p&gt;Why not include the PK definition inside the CREATE TABLE:&lt;/p&gt;

&lt;p&gt;CREATE TABLE IF NOT EXISTS mail_app_recipients&lt;br&gt;
(&lt;br&gt;
    id_draft Integer NOT NULL,&lt;br&gt;
    id_person Integer NOT NULL,&lt;br&gt;
    constraint pk_mail_app_recipients primary key (id_draft, id_person)&lt;br&gt;
)&lt;br&gt;
Solution 3:&lt;/p&gt;

&lt;p&gt;You can try to DROP it before creating it (DROP has the IF EXISTS clause):&lt;/p&gt;

&lt;p&gt;ALTER TABLE mail_app_recipients DROP CONSTRAINT IF EXISTS mail_app_recipients_pkey;&lt;br&gt;
ALTER TABLE mail_app_recipients ADD CONSTRAINT mail_app_recipients_pkey PRIMARY KEY ("id_draft","id_person");&lt;br&gt;
Note that this require that you give a name to the primary key constraint — in this example mail_app_recipients_pkey.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://sqlerrors.com/"&gt;sql &lt;/a&gt;— Postgresql: how to create table only if it does not already exist?&lt;br&gt;
Question:&lt;/p&gt;

&lt;p&gt;In Postgresql, how can I do a condition to create a table only if it does not already exist?&lt;/p&gt;

&lt;p&gt;Code example appreciated.&lt;/p&gt;

&lt;p&gt;Solution 1:&lt;/p&gt;

&lt;p&gt;I’m not sure when it was added, but for the sake of completeness I’d like to point out that in version 9.1 (maybe before) IF NOT EXISTS can be used. IF NOT EXISTS will only create the table if it doesn't exist already.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;CREATE TABLE IF NOT EXISTS users.vip&lt;br&gt;
(&lt;br&gt;
  id integer&lt;br&gt;
)&lt;br&gt;
This will create a table named vip in the schema users if the table doesn't exist.&lt;/p&gt;

&lt;p&gt;Source&lt;/p&gt;

&lt;p&gt;Solution 2:&lt;/p&gt;

&lt;p&gt;create or replace function update_the_db() returns void as&lt;br&gt;
$$&lt;br&gt;
begin&lt;br&gt;
    if not exists(select * from information_schema.tables &lt;br&gt;
        where &lt;br&gt;
            table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA&lt;br&gt;
            and table_name = 'your_table_name_here') then&lt;br&gt;
        create table your_table_name_here&lt;br&gt;
        (&lt;br&gt;
            the_id int not null,&lt;br&gt;
            name text&lt;br&gt;
        );&lt;br&gt;
    end if;&lt;br&gt;
end;&lt;br&gt;
$$&lt;br&gt;
language 'plpgsql';&lt;br&gt;
select update_the_db();&lt;br&gt;
drop function update_the_db();&lt;br&gt;
Solution 3:&lt;/p&gt;

&lt;p&gt;Just create the table and don’t worry about whether it exists. If it doesn’t exist it will be created; if it does exist the table won’t be modified. You can always check the return value of your SQL query to see whether the table existed or not when you executed the create statement.&lt;/p&gt;

&lt;p&gt;sql — Postgresql tables exists, but getting “relation does not exist” when querying&lt;br&gt;
Question:&lt;/p&gt;

&lt;p&gt;I have a postgresql db with a number of tables. If I query:&lt;/p&gt;

&lt;p&gt;SELECT column_name&lt;br&gt;
FROM information_schema.columns&lt;br&gt;
WHERE table_name="my_table";&lt;br&gt;
I will get a list of the columns returned properly.&lt;/p&gt;

&lt;p&gt;However, when I query:&lt;/p&gt;

&lt;p&gt;SELECT *&lt;br&gt;
FROM "my_table";&lt;br&gt;
I get the error:&lt;/p&gt;

&lt;p&gt;(ProgrammingError) relation "my_table" does not exist&lt;br&gt;
'SELECT *\n    FROM "my_table"\n' {}&lt;br&gt;
Any thoughts on why I can get the columns, but can’t query the table? Goal is to be able to query the table.&lt;/p&gt;

&lt;p&gt;Solution 1:&lt;/p&gt;

&lt;p&gt;You have to include the schema if isnt a public one&lt;/p&gt;

&lt;p&gt;SELECT *&lt;br&gt;
FROM ."my_table"&lt;br&gt;
Or you can change your default schema&lt;/p&gt;

&lt;p&gt;SHOW search_path;&lt;br&gt;
SET search_path TO my_schema;&lt;br&gt;
Check your table schema here&lt;/p&gt;

&lt;p&gt;SELECT *&lt;br&gt;
FROM information_schema.columns&lt;/p&gt;

&lt;p&gt;For example if a table is on the default schema public both this will works ok&lt;/p&gt;

&lt;p&gt;SELECT * FROM parroquias_region&lt;br&gt;
SELECT * FROM public.parroquias_region&lt;br&gt;
But sectors need specify the schema&lt;/p&gt;

&lt;p&gt;SELECT * FROM map_update.sectores_point&lt;br&gt;
Solution 2:&lt;/p&gt;

&lt;p&gt;create or replace function update_the_db() returns void as&lt;br&gt;
$$&lt;br&gt;
begin&lt;br&gt;
    if not exists(select * from information_schema.tables &lt;br&gt;
        where &lt;br&gt;
            table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA&lt;br&gt;
            and table_name = 'your_table_name_here') then&lt;br&gt;
        create table your_table_name_here&lt;br&gt;
        (&lt;br&gt;
            the_id int not null,&lt;br&gt;
            name text&lt;br&gt;
        );&lt;br&gt;
    end if;&lt;br&gt;
end;&lt;br&gt;
$$&lt;br&gt;
language 'plpgsql';&lt;br&gt;
select update_the_db();&lt;br&gt;
drop function update_the_db();&lt;br&gt;
Solution 3:&lt;/p&gt;

&lt;p&gt;I had to include double quotes with the table name.&lt;/p&gt;

&lt;p&gt;db=&amp;gt; \d&lt;br&gt;
                           List of relations&lt;br&gt;
 Schema |                     Name                      | Type  | Owner &lt;br&gt;
--------+-----------------------------------------------+-------+-------&lt;br&gt;
 public | COMMONDATA_NWCG_AGENCIES                      | table | dan&lt;br&gt;
 ...&lt;br&gt;
db=&amp;gt; \d COMMONDATA_NWCG_AGENCIES&lt;br&gt;
Did not find any relation named "COMMONDATA_NWCG_AGENCIES".&lt;br&gt;
???&lt;/p&gt;

&lt;p&gt;Double quotes:&lt;/p&gt;

&lt;p&gt;db=&amp;gt; \d "COMMONDATA_NWCG_AGENCIES"&lt;br&gt;
                         Table "public.COMMONDATA_NWCG_AGENCIES"&lt;br&gt;
          Column          |            Type             | Collation | Nullable | Default &lt;br&gt;
--------------------------+-----------------------------+-----------+----------+---------&lt;br&gt;
 ID                       | integer                     |           | not null | &lt;br&gt;
 ...&lt;br&gt;
Lots and lots of double quotes:&lt;/p&gt;

&lt;p&gt;db=&amp;gt; select ID from COMMONDATA_NWCG_AGENCIES limit 1;&lt;br&gt;
ERROR:  relation "commondata_nwcg_agencies" does not exist&lt;br&gt;
LINE 1: select ID from COMMONDATA_NWCG_AGENCIES limit 1;&lt;br&gt;
                       ^&lt;br&gt;
db=&amp;gt; select ID from "COMMONDATA_NWCG_AGENCIES" limit 1;&lt;br&gt;
ERROR:  column "id" does not exist&lt;br&gt;
LINE 1: select ID from "COMMONDATA_NWCG_AGENCIES" limit 1;&lt;br&gt;
               ^&lt;br&gt;
db=&amp;gt; select "ID" from "COMMONDATA_NWCG_AGENCIES" limit 1;&lt;/p&gt;

&lt;h2&gt;
  
  
   ID 
&lt;/h2&gt;

&lt;p&gt;1&lt;br&gt;
(1 row)&lt;br&gt;
This is postgres 11. The CREATE TABLE statements from this dump had double quotes as well:&lt;/p&gt;

&lt;p&gt;DROP TABLE IF EXISTS "COMMONDATA_NWCG_AGENCIES";&lt;br&gt;
CREATE TABLE "COMMONDATA_NWCG_AGENCIES" (&lt;br&gt;
...&lt;br&gt;
sql — drop primary key constraint in postgresql by knowing schema and table name only&lt;br&gt;
Question:&lt;/p&gt;

&lt;p&gt;As far I know the only way of dropping primary key in postgresql is:&lt;/p&gt;

&lt;p&gt;ALTER TABLE schema.tableName DROP CONSTRAINT constraint_name;&lt;br&gt;
the constraint name by default is tableName_pkey. However sometimes if table is already renamed I can’t get the original table name to construct right constraint name.&lt;/p&gt;

&lt;p&gt;For example, for a table created as A then renamed to B the constraint remains A_pkey but I only have the table name B.&lt;/p&gt;

&lt;p&gt;Do you know right way to drop the pkey constraint by knowing only the schema name and table name ?&lt;/p&gt;

&lt;p&gt;I am writing program for doing this so I need to use only SQL queries. Solutions like “open pgAdmin and see the constraint name” will not work.&lt;/p&gt;

&lt;p&gt;Solution 1:&lt;/p&gt;

&lt;p&gt;You can use information from the catalog tables like so:&lt;/p&gt;

&lt;p&gt;Create a table with id as the primary key&lt;/p&gt;

&lt;p&gt;create table test1 (id int primary key, name text);&lt;br&gt;
Create the SQL to drop the key&lt;/p&gt;

&lt;p&gt;select concat('alter table public.test1 drop constraint ', constraint_name) as my_query&lt;br&gt;
from information_schema.table_constraints&lt;br&gt;
where table_schema = 'public'&lt;br&gt;
      and table_name = 'test1'&lt;br&gt;
      and constraint_type = 'PRIMARY KEY';&lt;br&gt;
The result will be:&lt;/p&gt;

&lt;p&gt;alter table public.test1 drop constraint test1_pkey&lt;br&gt;
You can create a stored function to extract this query and then execute it.&lt;/p&gt;

&lt;p&gt;Solution 2:&lt;/p&gt;

&lt;p&gt;login to the database using psql, the command line tool.&lt;/p&gt;

&lt;p&gt;Then type:&lt;/p&gt;

&lt;p&gt;\d &lt;br&gt;
for example:&lt;/p&gt;

&lt;p&gt;\d claim&lt;br&gt;
                                                  Table "public.claim"&lt;br&gt;
             Column             |            Type             | Collation | Nullable |              Default&lt;br&gt;&lt;br&gt;
--------------------------------+-----------------------------+-----------+----------+-----------------------------------&lt;br&gt;
 id                             | integer                     |           | not null | nextval('claim_id_seq'::regclass)&lt;br&gt;
 policy_id                      | integer                     |           |          | &lt;br&gt;
 person_id                      | integer                     |           |          | &lt;br&gt;
 incident_id                    | integer                     |           |          | &lt;br&gt;
 first_notification_of_loss     | timestamp without time zone |           |          | &lt;br&gt;
 police_reference               | character varying(40)       |           |          | &lt;br&gt;
 photos_to_follow               | boolean                     |           |          | &lt;br&gt;
 sketch_to_follow               | boolean                     |           |          | &lt;br&gt;
 description_of_weather         | character varying(2000)     |           |          | &lt;br&gt;
 description_of_property_damage | character varying(2000)     |           |          | &lt;br&gt;
 created_at                     | timestamp without time zone |           | not null | now()&lt;br&gt;
 updated_at                     | timestamp without time zone |           | not null | &lt;br&gt;
Indexes:&lt;br&gt;
    "primary_key_claim" PRIMARY KEY, btree (id)&lt;br&gt;
Foreign-key constraints:&lt;br&gt;
    "foreign_key_claim_incident" FOREIGN KEY (incident_id) REFERENCES incident(id)&lt;br&gt;
    "foreign_key_claim_person" FOREIGN KEY (person_id) REFERENCES person(id)&lt;br&gt;
    "foreign_key_claim_policy" FOREIGN KEY (policy_id) REFERENCES policy(id)&lt;br&gt;
Referenced by:&lt;br&gt;
    TABLE "claimant" CONSTRAINT "foreign_key_claimant_claim" FOREIGN KEY (claim_id) REFERENCES claim(id)&lt;br&gt;
    TABLE "damage" CONSTRAINT "foreign_key_damage_claim" FOREIGN KEY (claim_id) REFERENCES claim(id)&lt;br&gt;
    TABLE "witness" CONSTRAINT "foreign_key_witness_claim" FOREIGN KEY (claim_id) REFERENCES claim(id)&lt;br&gt;
This shows you the primary key name (as well as other stuff).&lt;/p&gt;

&lt;p&gt;If you want to do this programmatically and you are using Java or another language that uses the JDBC interface, you can use the class DatabaseMetaData, method getPrimaryKeys.&lt;/p&gt;

&lt;p&gt;Otherwise, the other answer, selecting from the system catalogs, is the way to go.&lt;/p&gt;

&lt;p&gt;sql — Adding column with primary key in existing table&lt;br&gt;
Question:&lt;/p&gt;

&lt;p&gt;I am trying to add primary key to newly added column in existing table name Product_Details.&lt;/p&gt;

&lt;p&gt;New Column added: Product_Detail_ID (int and not null)&lt;/p&gt;

&lt;p&gt;I am trying add primary key to Product_Detail_ID (please note: there are no other primary or foreign key assigned to this table)&lt;/p&gt;

&lt;p&gt;I am trying with this query but getting error.&lt;/p&gt;

&lt;p&gt;ALTER TABLE Product_Details&lt;br&gt;
ADD CONSTRAINT pk_Product_Detils_Product_Detail_ID PRIMARY KEY(Product_Detail_ID)&lt;br&gt;
GO&lt;br&gt;
Error:&lt;/p&gt;

&lt;p&gt;The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.Product_Details' and the index name 'pk_Product_Detils'. The duplicate key value is (0).&lt;/p&gt;

&lt;p&gt;Am I missing something here? I am using SQL Server 2008 R2. I would appreciate any help.&lt;/p&gt;

&lt;p&gt;Solution 1:&lt;/p&gt;

&lt;p&gt;If you want SQL Server to automatically provide values for the new column, make it an identity.&lt;/p&gt;

&lt;p&gt;ALTER TABLE Product_Details DROP COLUMN Product_Detail_ID&lt;br&gt;
GO&lt;br&gt;
ALTER TABLE Product_Details ADD Product_Detail_ID int identity(1,1) not null&lt;br&gt;
GO&lt;br&gt;
ALTER TABLE Product_Details&lt;br&gt;
add CONSTRAINT pk_Product_Detils_Product_Detail_ID primary key(Product_Detail_ID)&lt;br&gt;
GO&lt;br&gt;
Solution 2:&lt;/p&gt;

&lt;p&gt;In mysql, I was able to achieve with following query&lt;/p&gt;

&lt;p&gt;ALTER TABLE table_name ADD new_column int NOT NULL AUTO_INCREMENT primary key&lt;/p&gt;

&lt;p&gt;Solution 3:&lt;/p&gt;

&lt;p&gt;You are getting the error because you have existing data that does not fullfill the constraint.&lt;/p&gt;

&lt;p&gt;There are 2 ways to fix it:&lt;/p&gt;

&lt;p&gt;clean up the existing data before adding the constraint&lt;br&gt;
add the constraint with the “WITH NOCHECK” option, this will stop sql server checking existing data, only new data will be checked&lt;/p&gt;

</description>
      <category>sql</category>
    </item>
    <item>
      <title>python — How do I remove a trailing newline?</title>
      <dc:creator>ZtoloGame</dc:creator>
      <pubDate>Mon, 15 Aug 2022 10:37:18 +0000</pubDate>
      <link>https://dev.to/ztologame/python-how-do-i-remove-a-trailing-newline-28o9</link>
      <guid>https://dev.to/ztologame/python-how-do-i-remove-a-trailing-newline-28o9</guid>
      <description>&lt;p&gt;Question:&lt;/p&gt;

&lt;p&gt;How do I remove the last character of a string if it is a newline?&lt;/p&gt;

&lt;p&gt;"abc\n"  --&amp;gt;  "abc"&lt;br&gt;
Solution 1:&lt;/p&gt;

&lt;p&gt;Try the method rstrip() (see doc Python 2 and Python 3)&lt;/p&gt;

&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;'test string\n'.rstrip()&lt;br&gt;
'test string'&lt;br&gt;
Python’s rstrip() method strips all kinds of trailing whitespace by default, not just one newline as Perl does with chomp.&lt;/p&gt;

&lt;p&gt;'test string \n \r\n\n\r \n\n'.rstrip()&lt;br&gt;
'test string'&lt;br&gt;
To strip only newlines:&lt;/p&gt;

&lt;p&gt;'test string \n \r\n\n\r \n\n'.rstrip('\n')&lt;br&gt;
'test string \n \r\n\n\r '&lt;br&gt;
In addition to rstrip(), there are also the methods strip() and lstrip(). Here is an example with the three of them:&lt;/p&gt;

&lt;p&gt;s = "   \n\r\n  \n  abc   def \n\r\n  \n  "&lt;br&gt;
s.strip()&lt;br&gt;
'abc   def'&lt;br&gt;
s.lstrip()&lt;br&gt;
'abc   def \n\r\n  \n  '&lt;br&gt;
s.rstrip()&lt;br&gt;
'   \n\r\n  \n  abc   def'&lt;br&gt;
Solution 2:&lt;/p&gt;
&lt;/blockquote&gt;


&lt;/blockquote&gt;
&lt;br&gt;
&lt;/blockquote&gt;

&lt;p&gt;And I would say the “pythonic” way to get lines without trailing newline characters is splitlines().&lt;/p&gt;

&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;text = "line 1\nline 2\r\nline 3\nline 4"&lt;br&gt;
text.splitlines()&lt;br&gt;
['line 1', 'line 2', 'line 3', 'line 4']&lt;br&gt;
Solution 3:&lt;/p&gt;
&lt;/blockquote&gt;


&lt;/blockquote&gt;
&lt;br&gt;
&lt;/blockquote&gt;

&lt;p&gt;The canonical way to strip end-of-line (EOL) characters is to use the string rstrip() method removing any trailing \r or \n. Here are examples for Mac, Windows, and Unix EOL characters.&lt;/p&gt;

&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;'Mac EOL\r'.rstrip('\r\n')&lt;br&gt;
'Mac EOL'&lt;br&gt;
'Windows EOL\r\n'.rstrip('\r\n')&lt;br&gt;
'Windows EOL'&lt;br&gt;
'Unix EOL\n'.rstrip('\r\n')&lt;br&gt;
'Unix EOL'&lt;br&gt;
Using ‘\r\n’ as the parameter to rstrip means that it will strip out any trailing combination of ‘\r’ or ‘\n’. That’s why it works in all three cases above.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;/blockquote&gt;
&lt;br&gt;
&lt;/blockquote&gt;

&lt;p&gt;This nuance matters in rare cases. For example, I once had to process a text file which contained an HL7 message. The HL7 standard requires a trailing ‘\r’ as its EOL character. The Windows machine on which I was using this message had appended its own ‘\r\n’ EOL character. Therefore, the end of each line looked like ‘\r\r\n’. Using rstrip(‘\r\n’) would have taken off the entire ‘\r\r\n’ which is not what I wanted. In that case, I simply sliced off the last two characters instead.&lt;/p&gt;

&lt;p&gt;Note that unlike Perl’s chomp function, this will strip all specified characters at the end of the string, not just one:&lt;/p&gt;

&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;"Hello\n\n\n".rstrip("\n")&lt;br&gt;
"Hello"&lt;br&gt;
thanks to : &lt;a href="https://pythonhowto.org/"&gt;pythonhowto&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;


&lt;/blockquote&gt;
&lt;br&gt;
&lt;/blockquote&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>How can I disable landscape mode in Android?</title>
      <dc:creator>ZtoloGame</dc:creator>
      <pubDate>Fri, 12 Aug 2022 10:31:38 +0000</pubDate>
      <link>https://dev.to/ztologame/how-can-i-disable-landscape-mode-in-android-3f67</link>
      <guid>https://dev.to/ztologame/how-can-i-disable-landscape-mode-in-android-3f67</guid>
      <description>&lt;p&gt;Question - &lt;a href="https://androidandrey.com/"&gt;androidandrey&lt;/a&gt;:&lt;br&gt;
How can I disable landscape mode for some of the views in my Android app?&lt;/p&gt;

&lt;p&gt;Solution 1:&lt;/p&gt;

&lt;p&gt;Add android:screenOrientation="portrait" to the activity in the AndroidManifest.xml. For example:&lt;/p&gt;

&lt;p&gt;
          android:label="@string/app_name"&lt;br&gt;
          android:screenOrientation="portrait" /&amp;gt;&lt;br&gt;
Since this has become a super-popular answer, I feel very guilty as forcing portrait is rarely the right solution to the problems it's frequently applied to.&lt;br&gt;
The major caveats with forced portrait:&lt;/p&gt;

&lt;p&gt;This does not absolve you of having to think about activity lifecycle events or properly saving/restoring state. There are plenty of things besides app rotation that can trigger an activity destruction/recreation, including unavoidable things like multitasking. There are no shortcuts; learn to use bundles and retainInstance fragments.&lt;br&gt;
Keep in mind that unlike the fairly uniform iPhone experience, there are some devices where portrait is not the clearly popular orientation. When users are on devices with hardware keyboards or game pads a la the Nvidia Shield, on Chromebooks, on foldables, or on Samsung DeX, forcing portrait can make your app experience either limiting or a giant usability hassle. If your app doesn't have a strong UX argument that would lead to a negative experience for supporting other orientations, you should probably not force landscape. I'm talking about things like "this is a cash register app for one specific model of tablet always used in a fixed hardware dock."&lt;br&gt;
So most apps should just let the phone sensors, software, and physical configuration make their own decision about how the user wants to interact with your app. A few cases you may still want to think about, though, if you're not happy with the default behavior of sensor orientation in your use case:&lt;/p&gt;

&lt;p&gt;If your main concern is accidental orientation changes mid-activity that you think the device's sensors and software won't cope with well (for example, in a tilt-based game) consider supporting landscape and portrait, but using nosensor for the orientation. This forces landscape on most tablets and portrait on most phones, but I still wouldn't recommend this for most "normal" apps (some users just like to type in the landscape softkeyboard on their phones, and many tablet users read in portrait - and you should let them).&lt;br&gt;
If you still need to force portrait for some reason, sensorPortrait may be better than portrait for Android 2.3 (Gingerbread) and later; this allows for upside-down portrait, which is quite common in tablet usage.&lt;br&gt;
Solution 2:&lt;/p&gt;

&lt;p&gt;I was not aware of the AndroidManifest.xml file switch until reading this post, so in my apps I have used this instead:&lt;/p&gt;

&lt;p&gt;setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // Fixed portrait orientation&lt;br&gt;
Solution 3:&lt;/p&gt;

&lt;p&gt;Add android:screenOrientation="portrait" in your manifest file where you declare your activity. Like this:&lt;/p&gt;

&lt;p&gt;
    android:name=".yourActivity"&lt;br&gt;
    ....&lt;br&gt;
    android:screenOrientation="portrait" /&amp;gt;&lt;br&gt;
If you want to do it using Java code, try:&lt;/p&gt;

&lt;p&gt;setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);&lt;br&gt;
before you call setContentView method for your activity in onCreate().&lt;/p&gt;

</description>
      <category>android</category>
    </item>
    <item>
      <title>How to display Oracle schema size with SQL query?</title>
      <dc:creator>ZtoloGame</dc:creator>
      <pubDate>Thu, 11 Aug 2022 12:06:04 +0000</pubDate>
      <link>https://dev.to/ztologame/how-to-display-oracle-schema-size-with-sql-query-194f</link>
      <guid>https://dev.to/ztologame/how-to-display-oracle-schema-size-with-sql-query-194f</guid>
      <description>&lt;p&gt;Question:&lt;/p&gt;

&lt;p&gt;I have a Oracle schema with 70+ tables. I want to create simple page which can display the HDD space occupied by the tables. How I can get this value with SQL query?&lt;/p&gt;

&lt;p&gt;P.S And how I can get the Oracle architecture version?&lt;/p&gt;

&lt;p&gt;Solution 1:&lt;/p&gt;

&lt;p&gt;You probably want&lt;/p&gt;

&lt;p&gt;SELECT sum(bytes)&lt;br&gt;
  FROM dba_segments&lt;br&gt;
 WHERE owner = &amp;lt;&amp;gt;&lt;br&gt;
If you are logged in as the schema owner, you can also&lt;/p&gt;

&lt;p&gt;SELECT SUM(bytes)&lt;br&gt;
  FROM user_segments&lt;br&gt;
That will give you the space allocated to the objects owned by the user in whatever tablespaces they are in. There may be empty space allocated to the tables that is counted as allocated by these queries.&lt;/p&gt;

&lt;p&gt;Solution 2:&lt;/p&gt;

&lt;p&gt;If you just want to calculate the schema size without tablespace free space and indexes :&lt;/p&gt;

&lt;p&gt;select&lt;br&gt;
   sum(bytes)/1024/1024 as size_in_mega,&lt;br&gt;
   segment_type&lt;br&gt;
from&lt;br&gt;
   dba_segments&lt;br&gt;
where&lt;br&gt;
   owner=''&lt;br&gt;
group by&lt;br&gt;
   segment_type;&lt;br&gt;
For all schemas&lt;/p&gt;

&lt;p&gt;select&lt;br&gt;
   sum(bytes)/1024/1024 as size_in_mega, owner&lt;br&gt;
from&lt;br&gt;
   dba_segments&lt;br&gt;
group by&lt;br&gt;
  owner;&lt;br&gt;
Solution 3:&lt;/p&gt;

&lt;p&gt;select T.TABLE_NAME, T.TABLESPACE_NAME, t.avg_row_len*t.num_rows from dba_tables t&lt;br&gt;
order by T.TABLE_NAME asc&lt;br&gt;
See e.g. &lt;a href="https://sqlerrors.com/"&gt;sqlerrors&lt;/a&gt; for more options&lt;/p&gt;

</description>
      <category>sql</category>
      <category>oracle</category>
    </item>
    <item>
      <title>Disable Rails SQL logging in console</title>
      <dc:creator>ZtoloGame</dc:creator>
      <pubDate>Sun, 07 Aug 2022 17:36:38 +0000</pubDate>
      <link>https://dev.to/ztologame/disable-rails-sql-logging-in-console-29mg</link>
      <guid>https://dev.to/ztologame/disable-rails-sql-logging-in-console-29mg</guid>
      <description>&lt;p&gt;To turn it off:&lt;/p&gt;

&lt;p&gt;old_logger = ActiveRecord::Base.logger&lt;br&gt;
ActiveRecord::Base.logger = nil&lt;br&gt;
To turn it back on:&lt;/p&gt;

&lt;p&gt;ActiveRecord::Base.logger = old_logger&lt;br&gt;
&lt;a href="https://sqlerrors.com/"&gt;Solution 2&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;Here's a variation I consider somewhat cleaner, that still allows potential other logging from AR. In config/environments/development.rb :&lt;/p&gt;

&lt;p&gt;config.after_initialize do&lt;br&gt;
  ActiveRecord::Base.logger = Rails.logger.clone&lt;br&gt;
  ActiveRecord::Base.logger.level = Logger::INFO&lt;br&gt;
end&lt;br&gt;
Solution 3:&lt;/p&gt;

&lt;p&gt;This might not be a suitable solution for the console, but Rails has a method for this problem: Logger#silence&lt;/p&gt;

&lt;p&gt;ActiveRecord::Base.logger.silence do&lt;br&gt;
  # the stuff you want to be silenced&lt;br&gt;
end&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
