Posts Tagged Programming

Repairing tables on MySQL

 

MySQL fix

If for any reason you come across next message when querying a table:

mysql> select * from data_values_queued;
ERROR 1194 (HY000): Table 'data_values_queued' is marked as crashed and should be repaired

You can fix this problem as follow.

CHECK TABLE

First, proceed to check the table to determine the problem:

mysql> check table data_values_queued;
+----------------------------+-------+----------+----------------------------------------------------------+
| Table                      | Op    | Msg_type | Msg_text                                                 |
+----------------------------+-------+----------+----------------------------------------------------------+
| datbas1.data_values_queued | check | warning  | 4 clients are using or haven't closed the table properly |
| datbas1.data_values_queued | check | error    | Size of datafile is: 4200         Should be: 4220        |
| datbas1.data_values_queued | check | error    | Corrupt                                                  |
+----------------------------+-------+----------+----------------------------------------------------------+
3 rows in set (0.05 sec)

Here you can see the table wasn’t closed properly, for instance due to a abrupt shutdown. In my case a discard the first message because I’m using InnoDB as engine, which allows concurrency on tables.

REPAIR TABLE

Now for repairing the table you can use the following sentence:

mysql> repair table data_values_queued ;
+----------------------------+--------+----------+----------------------------------------+
| Table                      | Op     | Msg_type | Msg_text                               |
+----------------------------+--------+----------+----------------------------------------+
| datbas1.data_values_queued | repair | warning  | Number of rows changed from 144 to 143 |
| datbas1.data_values_queued | repair | status   | OK                                     |
+----------------------------+--------+----------+----------------------------------------+
2 rows in set (0.00 sec)

 

With this you will have repaired the table . Let’s check it:

mysql> select * from data_values_queued;
+---------+--------+----------+---------------------+
| id      | iodbId | value    | timestamp           |
+---------+--------+----------+---------------------+
| 1476194 |    170 | 297      | 2012-01-11 08:40:45 |
| 1476193 |    170 | 296      | 2012-01-11 08:40:17 |
| 1476176 |     71 | 11692    | 2012-01-11 05:06:46 |
| 1476177 |     72 | 12061    | 2012-01-11 05:06:46 |
| 1476178 |     73 | 11820    | 2012-01-11 05:06:46 |
| 1476179 |    107 | 11703    | 2012-01-11 05:06:46 |
...
| 1476258 |    170 | 282      | 2012-01-11 11:07:43 |
+---------+--------+----------+---------------------+
143 rows in set (0.00 sec)

References

,

2 Comments

MySQL Prepared statements for C (complete example)

MySql logo

At work I had to make a interface to MySQL from pure C, and searching on the net I came across the book MySQL Developer’s Library by Paul DuBois which has really good example in chapter 7 on how to do SQL prepared statements using the C connector for MySQL. Although the book explains how to execute a SQL sentence with prepared parameters (prepared statements) and the explains how to data from a query, at the end  you can’t see the whole thing together so I decided to post a complete example:

void iodb_getNumAddresses(int id, int *numAddresses){

 MYSQL *conn;
 MYSQL_STMT *stmt;
 char *sql;

 // Bind variables
 MYSQL_BIND param[1], result[1];

 int myId, myNumAddresses;
 my_bool is_null[1];

 sql = "select count(*) from addresses where id = ?";

 // Open Database
 openDB(&conn);

 // Allocate statement handler
 stmt = mysql_stmt_init(conn);

 if (stmt == NULL) {
  print_error(conn, "Could not initialize statement handler");
  return;
 }

 // Prepare the statement
 if (mysql_stmt_prepare(stmt, sql, strlen(sql)) != 0) {
  print_stmt_error(stmt, "Could not prepare statement");
  return;
 }

 // Initialize the result column structures
 memset (param, 0, sizeof (param)); /* zero the structures */
 memset (result, 0, sizeof (result)); /* zero the structures */

 // Init param structure
 // Select
 param[0].buffer_type     = MYSQL_TYPE_LONG;
 param[0].buffer         = (void *) &myId;
 param[0].is_unsigned    = 0;
 param[0].is_null         = 0;
 param[0].length         = 0;

 // Result
 result[0].buffer_type     = MYSQL_TYPE_LONG;
 result[0].buffer         = (void *) &myNumAddresses;
 result[0].is_unsigned    = 0;
 result[0].is_null         = &is_null[0];
 result[0].length         = 0;

 // Bind param structure to statement
 if (mysql_stmt_bind_param(stmt, param) != 0) {
  print_stmt_error(stmt, "Could not bind parameters");
  return;
 }

 // Bind result
 if (mysql_stmt_bind_result(stmt, result) != 0) {
  print_stmt_error(stmt, "Could not bind results");
  return;
 }

 // Set bind parameters
 myId            = id;

 // Execute!!
 if (mysql_stmt_execute(stmt) != 0) {
  print_stmt_error(stmt, "Could not execute statement");
  return;
 }

 if (mysql_stmt_store_result(stmt) != 0) {
  print_stmt_error(stmt, "Could not buffer result set");
  return;
 }

 // Init data
 (*numAddresses) = 0;

 // Fetch
 if(mysql_stmt_fetch (stmt) == 0){
  (*numAddresses) = myNumAddresses;
 }

 // Deallocate result set
 mysql_stmt_free_result(stmt); /* deallocate result set */

 // Close the statement
 mysql_stmt_close(stmt);

 // Close Database
 closeDB(conn);

}

What is important here is the fact that there are two special structures from the MySQL API: param which set the parameters fro the query,  and result which will be used to store the data retrieved from the data base. In this example, after quering the database numAddresses will have the value queried

References

2 Comments

Installing Subclipse on Debian (and spinoffs)


Link to the video

If you are interested in installing Subclipse as a SVN client for Eclipse, you can use the following steps, which are OS independent, so they are valid for Windows, Mac and Linux, except the procedure for installing and setting JavaHL library on Debian (and spin-off like Ubuntu).

Installing Subclipse:

In general terms, these are the steps for installing Subclipse in Eclipse:

  1. Open Eclipse, got to  Help > Install New Software.
  2. Add the Subclipse’s URL  to the specific Eclipse version you have. For instance, in Galileo you must add this one http://subclipse.tigris.org/update_1.6.x
  3. Choose all shown items: Core SVNkit Library, Optional JNA Library (recommended) and Subclipse.
  4. Accept the user agreement to star the installation process. At the end a message will pop up to warn you about the fact you are installing third-party software. Just say yes.
  5. When finished you will be asked to restart Eclipse.

JavaHL library on Debian

If you don’t want to use  SVNKit library but JavaHL, then you must install this package:

 aptitude install libsvn-java

You have two alternatives to notify Eclipse about the existence of that library:

  1. The right thing is to make Eclipse read the configuration file with the path to the JavaHL library. To be honest I couldn’t find the user’s eclipse.ini file, just the general /etc/eclipse.ini. There you can add the lines shown in bold:
  2. -startup
    plugins/org.eclipse.equinox.launcher_1.0.201.R35x_v20090715.jar
    --launcher.library
    plugins/org.eclipse.equinox.launcher.gtk.linux.x86_1.0.200.v20090520
    -showsplash
    org.eclipse.platform
    --launcher.XXMaxPermSize
    256m
    -vmargs
    -Djava.library.path=/usr/lib/jni
    -Xms128m
    -Xmx512m
  3. The other solution is to make a symbolic link to the library, somethink like:
  4. ln -s /usr/lib/jni/libsvnjavahl-1.so /usr/lib/
  5. Reestar Eclipse and check changes by going to Preferences > Team > SVN, where you will be able to see the library version.

SVNKit library

With the installation process explained at the beginning you have downloaded SVNKit, which be used as a substitution of JavaHL, without needing to configure any file at all and with the advantage of be an multi-platform solutions, I mean, it will work on Windows and Mac as well. To do so just go to Window > Preferences > Team > SVN and choose  at the SVN section the SVNKit option:

SVNKit

With all these now you will be able to use this SVN client in Eclipse!!

, ,

6 Comments

Sociable plugin hack for bit.ly

Sociable WordPress Plugin

I was testing some plugins for allowing my visitor to share my posts in social networks like Twitter ot Facebook and the one I liked it the most was Sociable. ButI realized it used awe.sw for shorten URLs with Twitter and Identi.ca, but awe.sw service is colapsed and they’re not longer inviting new people. Thus, I started to research some ways to make it work with Bit.ly and I found this post that explains exactly what I wanted to, however they use the file_get_contents function, which throws this error in my server:

Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /home/g/gallardo/web/wp-content/plugins/sociable/sociable.php on line 813

This is because the allow_url_fopen variable is not enabled in PHP. After asking my hosting provider, they recommended me to use cURL instead of file_get_contents,so I had to make some modifications to the sociable.php file, and I had to add a new one called sociable-bitly.php where all URLs are shorten with cURL.

I put both files into the sociable.3.5.2-hacked.zip zip, just in case somebody needs it. But be aware, it’s not the whole plugin, I just put these file to be copied into the /sociable folder in the official plugin.  Another thing to keep in mind is that you have to edit the sociable-bitly.php with your Bit.ly login and API Key information in the following lines:

$bitlylogin = 'yourbitlylogin';
$bitlyapikey= 'yourbitlyapikey';

Other things can be done with this plugin, for instance all awe.se code can be removed, the Bit.ly input files can be added to the setting panel, or it can be integrated with tinyurl. I hope next official version comes with some of these features.

References:

,

2 Comments

Packing Python code for Symbian

PyS60

Let’s say you want to pack an application in Python for Symbian phones in order to deliver you software in an installation .sis file. How can you do it? Easy, with ensymble

Installing ensymble

In Debian you can install it form repositories by typing:

aptitude install  ensymble

Packing an application

If you have a file called application.py and want to make an installation file, just type:

ensymble py2sis application.py

Packing directories

Sometimes you need to pack a directory to deliver libraries. In this case you have to copy the directory structure you want to have on your phone and copy your files there. For instance, let’s say you want to make a .sis file for a Python library you created, called mylib where files settings.py and networing.py will be store:

mkdir MyLib/pyhon/lib/mylib
cp settings.py MyLib/pyhon/lib/mylib
cp networking.py MyLib/pyhon/lib/mylib

ensymble simplesis --caption="MyLib" --version=1.0.2 MyLib

With the last command you will end up with a file called MyLib_v1_0_2.sis. This file will install a directory called E:\\Python\Lib\mylib (or C:\\Python\Lib\mylib depending on where you install it) with your two Python files.

,

2 Comments

Octave: An alternative to MATLAB

sombrero

I’ve always wondered if there was any Software Libre alternative to MATLAB for doing some lab’s experiments  at collage, like those you might find on subjects such as signal processing or communication. Browsing on Internet I came across Octave, so I decided to test it and give it a try. Let’s see how to install it, how it looks like and my comments about it…

Installing Octave

On Debian just install the following package:

aptitude install qtoctave

It installs Octave’s command interpreter, some of its components and a graphic interface based on Qt to be able to use Octave as  you would use MATLAB. If other components are required, they can be installed later according to everybody’s needs. In my case I had to install the communication component:

aptitude install octave-communications

QtOctave interface

The graphic interface let you, among other things, work with Octave’s interpreter, see the variable list, edit a .m file, and see the command list as you would do in MATLAB.

QtOctave

Editing a .m file

In the above picture you can see a script for one of the experiments on the “Comunication II” subject (Universidad Simón Bolívar, Caracas – Venezuela), where students have to generate some line codes to see how they look like in time, their power spectral densities, how inmune they are to noise and their eye patterns; using functions provided by the teacher, which were made in MATLAB. Let’s see some picture I could generate:

Tiempo Frecuencia

Subplot Patron del Ojo

Compatibility with MATLAB

Although many of the MATLAB commands and functions are recognized by Octave, not everything is implemented the same way or in fact it might not be coded in Octave. For instance, I had to edit the scope.m file to comment the cla command (clear current axes) and some global variables didn’t work  in some of the Toolbox’s scripts provided for this experiment. If you have any problem with a MATLAB’s function or command you can check out the Octave <-> Matlab Compatibility Database.

Final comments

I think Octave is a real  alternative to MATLAB. People can say it’s not as complete as MATLAB is…and it’s true, but who use MATLAB at 100%? On the other hand, I think it would be an option to eradicate other operating systems and some bad habits like buying licenses, because students end it up buying a pirated license to finish their assignments at home, even though university’s labs have 100% legal licenses.

Can those experiments be adapated to Octave? I do think so. Furthermore, post-grade students could write code (like fft6) to contribute to the Octave project by growing its function library up…For now I’m going to share my findings with my teacher to see what she thinks!

,

6 Comments

PHP’s Classes and Objects

blocksBefore start working with classes and objects, we must first know the difference between traditional programming and object oriented programing (OOP).  In traditional programming problems are solved performing one action after the other, in other words, you write a program where some actions are executed one by one to solve a problem.

For Example,  if you want to add two numbers in PHP, it’s enough the following code:

<?
// Define the numbers
$a = 2;
$b = 3;
// Add both numbers
$c = $a + $b;
echo "a+b=".$c;
?>

Read the rest of this entry »

,

4 Comments

Automatic full backup on cPanel

logo_cpanelYesterday I had some issues with the WordPress 2.8 update and, supposedly,  qtranslate (a plugin to support several languages). The thing is I could restore WordPress 2.7.1 from an old backup, but I realized it dated from a month ago…bad, bad. I decided to do a research on automatic full-backup with cpanel and I came across the Automatic cPanel backup (domain & MySQL) with cron & PHP post by Justin Cook .There’s a php script to perform the cPanel‘s full backup request with ftp redirection to a remote host. Due to I don’t have a ftp server, I changed some lines to make backup locally, so I could get those backups from cPanel full-backup tool. Here’s what I got:

PHP Script

<?php
// Info required for cPanel access
$cpuser = "username";
$cppass = "password";
$domain = "example.com";
$skin = "x3";

// Notification information
$notifyemail = "[email protected]";

// Secure or non-secure mode
$secure = 1;

// Set to 1 to have web page result appear in your cron log
$debug = 0;

// ** NO EDIT BELOW THIS LINE ***

if ($secure) {
   $url = "ssl://".$domain;
   $port = 2083;
} else {
   $url = $domain;
   $port = 2082;
}

$socket = fsockopen($url,$port);

if (!$socket) {
   echo "Failed to open socket connection… Bailing out!\n"; exit;
}

// Encode authentication string
$authstr = $cpuser.":".$cppass;
$pass = base64_encode($authstr);

$params = \
"dest=homedir&email=$notifyemail&server=&user=&pass=&port=&rdir=";

// Make POST to cPanel
fputs($socket,\
"POST /frontend/".$skin."/backup/dofullbackup.html?".$params." HTTP/1.0\r\n");
fputs($socket,"Host: $domain\r\n");
fputs($socket,"Authorization: Basic $pass\r\n");
fputs($socket,"Connection: Close\r\n");
fputs($socket,"\r\n");

// Grab response even if we don't do anything with it.
while (!feof($socket)) {

$response = fgets($socket,4096);

if ($debug) echo $response;
}
fclose($socket);
?>

You can download the example here

Cron jobs

In order to have the backup made every 15 days just save the above script as fullbackup.php and set cron in cPanel‘s tool as shown next:

cron-cpanel-web

References

,

12 Comments

HP48 programming

Hp-48g_Program

Some time ago I uploaded a programming course for the HP-48g / HP-48gx calculators. The thing is that I lost my password on that server (angelfire) I couldn’t update any more. So, it’ s time get back what’ s mine: I’m going to put the entire course here with more examples and better explanation if possible.

Let’s start over…

What is a program?

All between << >> character is taken as a program. For example, you can write this on the calculator:

<< 1 2 + >>

This program put on your HP’s stack 1 and 2 to add them. In order to edit any program in USER-RPL mode just put it on the stack and push “EDIT”. To save this program put it on the stack, then put a name and push “STO” button (you can also type it). Here’s the example:

3: << 1 2 + >>
2: ‘Nombre’
1: STO

It’s all for now. In the next post I will cover variable definition…

,

7 Comments

Adding subtitles to avi files

SubtítulosWe saw on previous posts how to convert video files to avi on the command line, later we focused on how to make it work on Nautilus, the Gnome’s file manager. Now we’ll learn how to add subtitles to avi files from .srt text files. We will use the mencoder command with the -sub option, as shown in the following example:

mencoder file.avi \
-ovc lavc -oac mp3lame \
-sub file.srt \
-font “/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf” -subfont-autoscale 2 \
-o file-sub.avi

In this example mencoder takes as input “file.avi”, uses lavc (mpeg4) video codec and mp3lame (mp3) audio codec, adds “file.srt ” subtitles to the picture, uses DejaVuSans fonts  with autoscaling according to the video’s width, and finally saves it on “file-sub.avi”.

Read the rest of this entry »

,

4 Comments