Jun 17

I ran into a problem where i had to update two values on a table. One was fixed, but the other depended on a value stored on a second table. This is how its done.


TableA

id_cli   test_cut

1          15

2          56

TableB

id    num_of        job    test_cut    id_client

1     20090464    34      0               2

2     20081012    200    0               1

I needed to update the value of the fields “job” and “test_cut” on Table B, but the value i wanted to put on “test_cut” needs to come from Table A, linked by field “id_cli”. Confusing?

update TableB Tb,TableA Ta set Tb.job_meio=’550′,Tb.test_cut=Ta.test_cut where Tb.num_of=’20090494′ and Ta.id_cli=Tb.id_cli

This update joins the two tables by the field id_cli, fetches the test_cut on TableA and updates fields test_cut and job_meio on TableB

This will turn TableB into:

id    num_of        job      test_cut    id_client

1     20090464    550 56 2

2     20081012    200     0               1


May 8

Didn’t quite know what to use as a title, because the problem sounds a bit too generic. Anyway, on to the problem/solution.

The problem was that i was trying to convert an already built/in progress Flex project to Air and it was giving me some grief, since it simply didnt run. No error, nothing. The only message i got was if i tried to debug it, where i would get:

Process terminated without establishing connection to debugger.

Command:

C:\win2kapp\FlexBuilder3\sdks\3.3.0\bin\adl.exe D:\AppServ\www\workspace\tg_preimpressao_air_2009.05.08\bin-debug\main_preimpressao-app.xml D:\AppServ\www\workspace\tg_preimpressao_air_2009.05.08\bin-debug

Output from command:

invocation forwarded to primary instance

From some googling around, i found the error to be because the Air version on my Flex project was wrong to the one of the new Air project.

I had to go to the <project name>-app.mxml file in the src dir and change the second line from:

<application xmlns=”http://ns.adobe.com/air/application/1.0“>

to

<application xmlns=”http://ns.adobe.com/air/application/1.5“>

Notice the version diference.

Stir. Compile. Run. Smile.

NOTE: Alternatively, you can create a new Air project and open its <project name>-app.mxml to see the version number of your Air.

This solution was found somewhere on the net, cant really recall where, and was documented on a bug track in adobe.

EDIT: This problem happened again today and this time it was nothing of the above. It seems it can happen when, by some misfortune (i dont know why), adl.exe is still running when we try to run our Air project. I solved it by going to task manager and terminating the process. Everything compiled and ran normally from then on.


Mar 30

Found this somewhere on google.

If we need to compare two tables, this query will list the missing records on one of them (on table2):

SELECT t1.id
FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.id
WHERE t2.id IS NULL

Then you do it the other way around to check the mising records on the other table (table1).

To check for different values in identical fields:

SELECT t1.id
FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.id
WHERE t1.field1 <> t2.field1
OR t1.field2 <> t2.field2

etc…


Mar 30

This is a simple reminder of the mysql command line tool.

In windows, open a command line, navigate to mysql binary folder (if you have it on your PATH, you can skip the navigation part), and use:

To login:

mysql -u #username# -p#password#

(if you dont add the #password#.. it will ask you for one. Also, note that the password doesnt have a space between the switch)

To show all the databases:

mysql> show databases;

To switch a database:

mysql> use dbname;

To list the tables in the selected db:

mysql> show tables;

To see database’s field formats:

mysql> describe table_name;

To show columns and column information of table_name:

mysql> show columns from table_name;

All mysql commands apply:

mysql> select * from table_name where field_name="blahblah";

NOTE - Always end the command with ;

MYSQL Dump/Restore

On the binary folder, theres another command - mysqldump - that makes, well, dumps.

To make a dump of all the databases on the server:

mysqldump -u #username# -p#password# -A > dumpfile.sql

Dump only certain databases:

mysqldump -u #username# -p#password# db_1 db_2 db_3 > dumpfile.sql

Restore a database dump:

mysql --verbose -user=#username# --password=#password# db_1 < dumpfile.sql

NOTE - note the difference in the mysqdump and mysql when adding the switch for username and password, you can use either “-u #username#” or “–username=#username#”.

Command line help:

General help:

mysql> help

See the help contents:

mysql> help contents

To see help on a specific command/category type:

mysql> help _command_or_category_

Some examples:

mysql> help select
mysql> help administration
mysql> help me because i dont know anything about mysql and want to learn everything in 3 days (ok, doesnt work, but you can still try it for the error message)

Links to resources:

MYSQL commands

mysqldump


Mar 13

Like other people i’ve seen complaining about in the net, i keep forgetting how to do this.

There are several ways of doing this, but here’s a simple one:

$haystack = "Once upon a time, on a far away kingdom...";
$needle = "kingdom";
if(strstr($haystack,$needle)){
    echo "Word found.";
}else{
    echo "Couldn't find the word.";
}

The function strstr($haystack,$needle) is case sensitive in its search.

If it doesnt find the $needle, it will return false, otherwise returns the string from the first occurence of the needle to the end of the haystack text.

There’s also the function stristr($haystack,$needle) does case insensitive search.

Here are links to the manual:

strstr()
stristr()

Other alternatives (regEx, etc):

strrchr() - Find the last occurrence of a character in a string
substr() - Return part of a string
preg_match() - Perform a regular expression match


Feb 26

If we want to make an update to a table, but have to condition that update with values from a second one, we do something like this:

TABLE_1
id_t1 name fee
1 john 100
2 rita 100
3 peter 100

TABLE_2
id_t2 id_t1 type
1 1 single
2 2 single
3 3 married

If we want to update the field “fee” from TABLE_1, for example, to make single people pay more than married people (i know, stupid example, but bear with me), we can do this:

update TABLE_1 t1,TABLE_2 t2 set t1.fee=200 where t1.id_t1=t2.id_t1 and t2.type='single'

this is going to update all the rows in TABLE_1 that belong to “single” people, resulting in:

TABLE_1
id_t1 name fee
1 john 200
2 rita 200
3 peter 100

The update is an inner join with the comma operator.

Heres a link to the MySQL manual page on the update command as a reference. Has some nice examples:

http://dev.mysql.com/doc/refman/5.0/en/update.html


Feb 23

My problem was to convert some calculations from decimal to hexadecimal to be used in a canvas background color. Specifically, convert from CMYK, from a pantone list, to RGB and apply the result to a canvas.

In a different post i will describe the convertion, might be useful for someone.

Flex has a way of converting to several numeral systems. For example, from the manual:

var myuint:uint = 9;
trace(myuint.toString(2)); // output: 1001 - binary system
trace(myuint.toString(8)); // output: 11 - octal system

And to create hexadecimal values, to be used in uint format:

var r:uint = 250;
var g:uint = 128;
var b:uint = 114;
var rgb:String = "0x" + r.toString(16) + g.toString(16) + b.toString(16);
trace(rgb); // 0xfa8072

All seems well, except when the result in hexadecimal is just one caracter long, so be sure to check and pad it with a zero.
A simple example:

var r:uint = 8
var rs:String = (r.toString(16).length == 1) ? "0"+r.toString(16) : r.toString(16);

Breaking it down:

r.toString(16) –> converts the var r to hexadecimal, which equals “8″.

I then check the length of the result and, in case it equals 1, add a “0″ to the beginning of the string to pad it.


Feb 18

On my current project, i have a debug TextArea that continuously lists some var values and traces. This way i can keep track of whats going on without stopping execution to debug in Flex.

As the list fills, the Text starts going off the screen, downwards. I needed to keep the scroll on the TextArea always on the last piece of text. I googled and i found a solution by Joan Lafferty, from the blog “flex butterflies and bugs”, so all credit goes to where its due. I leave a link to the original post at the end of this one.

Anyway, the solution is as follows:

<mx:TextArea
valueCommit=”txtl.verticalScrollPosition=txtl.maxVerticalScrollPosition”
id=”txtl”
x=”202″
y=”10″
height=”206″
width=”122″
text=”{log}”
editable=”false”
enabled=”true”
wordWrap=”true”
textAlign=”right”/>

Save for the usual dimension/position properties, what you should look at is the valueCommit method, which will simply keep the scroll at the last bit of content in the TextArea.

Joan’s post is far better explained than this, and has an example. I am just posting this here as a reminder to myself and to other forgetful geeks out there.

Heres the link to Joan’s blog post about this subject:

http://butterfliesandbugs.wordpress.com/2007/08/20/scrolling-to-the-bottom-of-a-container-or-textarea-automatically/


Dec 4

Its simple but i thought of leaving it here if i forget (which is usually the case) how to do it again.

If you want to insert the records from tableA into tableB, you do something like this:

INSERT INTO tableA SELECT * FROM tableB

You have to make sure all fields are the same on both tables or you will get an error.

You can list the fields so you wont go wrong:

INSERT INTO tableA(name,address) SELECT name,address FROM tableB

And you can even prevent the key field from being the same:

INSERT INTO tableA SELECT * FROM tableB ON DUPLICATE KEY UPDATE tableB.state =”changed”

Like i mentioned, simple.


Oct 6

Before i came up with this solution, i had some trouble formatting and padding decimal numbers, specifically when i want to do it before and after the decimal mark. To top it all, i wanted the result signed (with a plus sign(+) or minus sign(-) depending on the value of the variable)

Example:

I want this “-4.3″ into this “-04.3″

Or this “6″ into this “+06.0″

PHP has a lot of number formatting options, from number_format, to sprontf and printf. Well, just search the php docs and you will see what i mean.

The thing is that, as always, i wanted to do it in as less code as possible so, to do what i mentioned above, and after a bit of testing, i came up with this:

$value = -14.6;
printf('%1$s%2$02d.%3$01s', ($value <0 ) ? '-' : '+',floor(abs($value)),(abs($value)-floor(abs($value)))*10);

Although pretty simple, i think its still a bit contrived and dirty. But it works.

A bit of background:

the first part of the printf means:

%1 will contain the first parameter (the value after the first comma), with $s as the format (in sprintf, ’s’ means string, so it will just output the result of the first parameter, no formatting)

%2 has $02d as format so it will show a pad of two digits of an integer treated number showed as decimal

%3 has $01s as format - like i mentioned for %1, shows a string with a pad of one digit

Since i want to keep these posts simple, to serve as reference and jump to other reference sites, i will just leave some links:

PHP docs for sprintf (it has all the formatting…formats):

W3C for PHP math:

Just ask if you need any more feedback or help.