"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects." (Robert A. Heinlein)
Showing posts with label MySQL. Show all posts
Showing posts with label MySQL. Show all posts

Thursday, 25 June 2015

More work on the Raspberry PI


I've been working a little more on my Raspberry PI based server. After installing Minidlna server my Raspberry PI server needed some easy way to transfer media files into the USB disk. Sharing the disk with Samba has been, to me, the obvious solution. Also I installed the basis of a LAMP server (Apache2, MySQL, PHP5) for future installation of server applications.

Sharing a disk with Samba

The first step has been so installing samba from command line apt-get


sudo apt-get update
sudo apt-get install samba samba-common-bin
then I edited samba configuration file
sudo vim.tiny /etc/samba/smb.conf
where I added the definition instructions for sharing the USB disk
[usbdisk]
comment = Raspberry PI USB disk
path = /media/usbdisk
browsable = yes
guest ok = yes
read only = no
create mask = 0777
directory mask=0777
public= yes
only guest = no
force user = pi
force group = users
To keep things on the easy-to-use side I set the share for a public “guest” use without asking for passwords. Since my Raspberry server is still in a experimental status I don't have, not yet at least, many security worries to think about.
I tested the configuration with the testparm command
testparm /etc/samba/smb.conf
and eventually restarted the samba service.
sudo service samba restart

Friday, 30 January 2009

Names generators with MySQL

There are on the net several names generator. theese programs staring from an input name generate some kind of funny phrase.
Here is an example of a "nickname generator":
The algorithm behind these programs is quite simple: it's just matter of using your name as a key to select phrase components from one or more tables. Here is a simple way to do this using MySQL:
First let's create a couple of tables:
CREATE TABLE ADJECTIVES(
              TERM VARCHAR(30));

CREATE TABLE ANIMALS(
              NAME VARCHAR(30));
Then fill the tables with some value
INSERT INTO ADJECTIVES VALUES('Lazy');
INSERT INTO ADJECTIVES VALUES('Fat');
INSERT INTO ADJECTIVES VALUES('Crazy');
INSERT INTO ADJECTIVES VALUES('Ugly');

INSERT INTO ANIMALS VALUES('Bull');
INSERT INTO ANIMALS VALUES('Cow');
INSERT INTO ANIMALS VALUES('Monkey');
INSERT INTO ANIMALS VALUES('Chicken');
To implement the selection we can use MySQL RAND()  function. Rand function can generate a pseudo-random number sequence based on a seed integer passed as parameter. The same seed number ever generates the same sequence.
Another function HEX() let us easily convert our input string to a number we can use as seed.
The number generated from RAND() function can eventually be used to select a row in the table.
Let's put all together:
SELECT
(SELECT TERM FROM ADJECTIVES ORDER BY RAND(HEX('musante.i.ph')) LIMIT 1) AS Adjective,
(SELECT NAME FROM ANIMALS ORDER BY RAND(HEX(REVERSE('musante.i.ph'))) LIMIT 1) AS Animal;
(Update) since RAND() ever generates the same number sequence from the same seed number we must use a different seed for every word we need to extract. To do this we neeed to change the source string in order to obtain a different seed number. In my example I used the REVERSE() function to implement it (not the best choice indeed) but even simpler solutions, like adding a different constant to each, should work also well.
Here is the result:

Adjective Animal 
Crazy     Chicken

Does it fit with my blog? Probably no more than "Ultimate Monk"!
What makes the difference, in this kind of programs, is the database of names used. A wider database generates, of course , a less repetitive set of phrases but what really matters is a good choice of the words database.

Friday, 31 October 2008

Populating a table with random people


It happened I had  to  fill a table with realistic but not real people data.Here how did I managed it (ported to MySQL sintax).
First lets create a people table like this :
CREATE TABLE PEOPLE(NAME VARCHAR(20),
                                     SURNAME VARCHAR(20),
                                     SEX VARCHAR(1),
                                     DOB DATE);
Then create tables with sample names and surnames:
CREATE TABLE NAMES(NAME VARCHAR(20), SEX VARCHAR(1));
CREATE TABLE SURNAMES(SURNAME VARCHAR(20));
And fill them with some values:
INSERT INTO NAMES(NAME,SEX) VALUES('Jhon','M');
INSERT INTO NAMES(NAME,SEX) VALUES('Jack','M');
INSERT INTO NAMES(NAME,SEX) VALUES('Jeff','M');
INSERT INTO NAMES(NAME,SEX) VALUES('Judy','F');
INSERT INTO NAMES(NAME,SEX) VALUES('Jade','F');
INSERT INTO NAMES(NAME,SEX) VALUES('Jane','F');
INSERT INTO SURNAMES(SURNAME) VALUES('Red');
INSERT INTO SURNAMES(SURNAME) VALUES('White');
INSERT INTO SURNAMES(SURNAME) VALUES('Green');
Now we'll be able to get a single random person with a query like this:
SELECT NAME, SEX,
           CURDATE() - INTERVAL RAND()*365.25*100 DAY AS DOB,
                 (SELECT SURNAME FROM SURNAMES ORDER BY RAND() LIMIT 1) AS SURNAME
FROM NAMES ORDER BY RAND() LIMIT 1;
We can write a simple procedure to populate the table with the desired number of rows:
CREATE PROCEDURE FILL(IN NUM INT)
   BEGIN
       WHILE NUM > 0 DO
          INSERT INTO PEOPLE(NAME,SEX,DOB,SURNAME)
          SELECT NAME, SEX,
                            CURDATE() - INTERVAL RAND()*365.25*100 DAY AS DOB,
                         (SELECT SURNAME FROM SURNAMES ORDER BY RAND() LIMIT 1) AS SURNAME
         FROM NAMES ORDER BY RAND() LIMIT 1;
         SET NUM = NUM-1;
      END WHILE;
END;
We can call this procedure like this:
mysql> call FILL(10000);
Query OK, 1 row affected (40.73 sec)
A ten thousand people population in forty seconds (on PIII at 550Mz) is not bad.
Of course many improvements can be done like adding a random place of birth (based on a table of places) or making date of birth distribution more realistic.