Thursday, February 6, 2014

GROUP BY Date Time, Day, Month, Year, calculate SUM function in SQLite for app developers


SELECT strftime('%m', trxDateTime) as valMonth, 
SUM(trxAmount) as valTotalMonth 
FROM trx_log 
WHERE strftime('%Y', trxDateTime)='2014' GROUP BY valMonth

Given above is the query, which will be explained in the blog below; continue reading for further details.

SQLite GROUP BY Day, Month, Year and sum query example

I have a database table that contains a few fields like transaction date, which is a string representing date time, transaction amount, and transaction status in an SQLite database present on an Android device. The SQL Lite database contains a table named "trx_log" The table has fields _id int, trxDateTime text, trxTargetNumber text, trxAmount number, and trxStatus text. A graphical representation of the table structure is given below:

sqlite-android-search-by-year-group-by-month

SQLite Table used in the example of GROUP BY date, time, month, year, and SUM function.

SQLite Query Sum and Group By Year

How to calculate the sum of all transactions that took place in 2014? What is the SQLite query for the group by results by year and calculating a sum? We will combine SQLite aggregate function SUM with GROUP BY Clause.

SELECT strftime('%Y', trxDateTime) as valYear, SUM(trxAmount) 
FROM trx_log WHERE valYear = '2014' GROUP BY valYear

Wednesday, September 4, 2013

Android MediaRecorder.setMaxFileSize Exception due to file size

Android Media Recorder will stop functioning and show a Java.lang.RuntimeException if we provide a very small file size, for example if you will put 1024 as the MediaRecorder.setMaxFileSize parameter, the recording won't even start in the first place. Remember, the number provided as input to this method represents maximum number of bytes which would be stored in a file by the MediaRecorder.
Android MediaRecorder

The reason is that it takes almost 10 KB to store a audio recording of 1 second or so. When you will put 1024 bytes as a parameter, the MediaRecord will simple run out of space when it will try to stop the voice recording upon reaching the tiny number 1024 bytes. The error code returned by such a situation is -22, and the logging line will look something like the one given below:

Wednesday, March 20, 2013

Determining percentage of colors in a bitmap using C# without getPixel

To keep things simple, let's suppose we have a black and white bitmap image.
I will not use getPixel method here, Bitmap.getPixel() is a very expensive method, and it alone can turn your program into a lazy snail.

Please note that I am using a Format24bpprgb image format here, 24BPPRGB can be defined as:
Each pixel in the image is represented by 24 bits. First 8 bits represent red color, next 8 bits represent green color, and last 8 bits represent blue color in a bitmap.

Another popular format is 32BPPARGB, in this format the first 8 bits represent "Alpha" or the opacity of pixel, next 3 bytes or 24 bits describe the RGB components same like 24BPPRGB.

Given below is the C# language source code for checking pixel colors without Bitmap.getPixel() method.

Friday, November 11, 2011

What makes Android Programming Simpler and iOS difficult?

ios-5-android-ice-cream-sandwich-apple-google

I cannot start writing iPhone and iPad software because?

I don't have an Apple... No! don't bring me a KG of fresh red apples please, I am referring to the Apple which is a computer. It's impossible to code stuff up without an Apple machine, even for sake of learning. Apple MUST create a tool chain for people running Windows as we know it's the most widely used operating system, just like the iPhone which is a crazily used "Somewhat Smart" Phone. They could create a development environment, and a few emulators to get new going with iOS development. I believe it would cost less than $100k to develop such a solution, maintenance cost won't go beyond this as well.

Friday, October 7, 2011

C# string utility functions containsAtLeastOneChar-containsOnlySpecifiedSpecChars-excludesSimilar

c-sharp-green

A utility class to perform simple operations on strings using C#. The logic is simple, one can transform the code given below to C++, PHP, or JavaScript within 2 minutes. I know each one of the programming languages I mentioned here, and all other first class modern programming languages have got sophisticated regular expression packages. But, the use of regex is not an option for everyone. Some got the skill to learn, but just don't have the time needed to read docs. Some don't want to import in a full blown namespace like System.Text.RegularExpressions
And some other are plain lazy.
Here goes the code, the names are descriptive enough, first one containsAtLeastOneChar  checks whether a string contains at least one character of a certain character set. For example, we want to check whether a password generated by a automatic password generator contains at least one capital letter, one number, and one special character.
Second one, containsOnlySpecifiedSpecChars returns true when the supplied string conains only specified characters.
Third function excludesSimilar will return true when a string does not contain repeating characters.
The class Commons contains static member variables, you could try passing these values to the static member functions of this class and experiment  around to check the results. It will also save you some typing :)
You could just copy paste the code given below as it is, it's supposed to work.
Happy coding!
public class Commons
{
public static bool containsAtLeastOneChar(string strWord, string strCompairTo)
{
 int nIdx = -1;
 foreach (char c in strWord)
 {
  nIdx = strCompairTo.IndexOf(c);
   if (nIdx >= 0) return true;
 }
 return false;
}
public static bool containsOnlySpecifiedSpecChars(string strWord, string strSpecialChars)
{
foreach (char c in strWord)
 {
  if ((strCaps.IndexOf(c) < 0) & (strSmalls.IndexOf(c) < 0) & (strNums.IndexOf(c) < 0))
 {
  if (strSpecialChars.IndexOf(c) < 0)
   return false;
  }
}
return true;
}
public static bool excludesSimilar(string strWord)
{
foreach (char c in strWord)
{
 if (strWord.IndexOf(c) != strWord.LastIndexOf(c))
  return false;
 }
 return true;
}

public static string strCaps = "ABCDEFGHIKLMNOPQRSTUVWXYZ";
public static string strSmalls = "abcdefghijklmnopqrstuvwxyz";
public static string strNums = "0123456789";
}//class