rulururu

post Store Images in MySQL

May 29th, 2008

Filed under: Database, PHP — Kai @ 1:00 pm

Sometimes, it’s more convenient to save images in a database than as files. MySQL and PHP make it very easy to do this. I will describe how to save images in a MySQL database.

  • Setting up the database
  • The difference between any regular text or integer fields and a field that needs to save an image is the amount of data that is needed to be held in the field. MySQL uses special fields to hold large amounts of data. These fields are known as blobs (Blob).

    Here is the BLOB definition from the MySQL site:

    A BLOB is a binary large object that can hold a variable amount of data. The four BLOB types TINYBLOB, BLOB, MEDIUMBLOB and LONGBLOB differ only in the maximum length of the values they can hold.

    For more information about MySQL Blobs check out: Reference: Blob

    Use the next syntax to create a basic table that will hold the images:

    CREATE TABLE Images (
    PicNum int NOT NULL AUTO_INCREMENT PRIMARY KEY,
    Image BLOB
    );
  • Setting the upload script
  • What we need now is the PHP script that will get the file and insert it into the database. The next script does just that. In the script, I’m assuming that the name of the file field is “Picture”.

    <?
    If($Picture != "none") {
      $PSize = filesize($Picture);
      $mysqlPicture = addslashes(fread(fopen($Picture, "r"), $PSize));
     
      mysql_connect($host,$username,$password) 
                           or die("Unable to connect to server");
      @mysql_select_db($db) 
                               or die("An error occured when selecting database");
      mysql_query("INSERT INTO Images (Image) VALUES ('$mysqlPicture')") 
                       or die("Query failed");
    }
    else {
      echo "Failure during upload.";
    }
    ?>

    This is all that is needed to enter the image into the database. Note that in some cases you might get an error when you try to insert the image into the database. In such a case you should check the maximum packet size allowed by your MySQL version. It might be too small and you will see an error about this in the MySQL error log.

    What we did in the above file is:

    1. Check if a file is chosen.
    2. addslashes() to the picture stream to avoide errors in MySQL.
    3. Connect to MySQL, choose the database and insert the image.

  • Size does matter
  • The theoretical limit in MySQL 4.0 is 2G, however each blob requires generally to have 3 copies of it in the memory (stored in various buffers) so you need a lot of memory, if you have large BLOBs stored in MySQL. This is the reason, why the theoretical limit can be reached only on 64bit systems. The Practical limits are around some hundreds of megs per BLOB.

    While logged into MySQL, we should modify the max_allowed_packet system variable. This variable determines how large of a packet (i.e. a single row) can be sent to the MySQL server. By default, the server will only accept a maximum size of 1 meg from our client application. If you do not intend to exceed 1 meg, this should be fine. If you do intend to exceed 1 meg in your file transfers, this number has to be increased.

  • Conclusion
  • It’s often suggested to save path to a file in database and not the file itself. Rethink everything before storing large data in your database - it can save lots of memory.

    On the other hand this is extremely useful for downloading files of different types from a database, or even displaying images stored in Blob fields.

post How Python can be fast

May 22nd, 2008

Filed under: Python — Kai @ 5:00 pm

I’ve been working with python for a sometime, I’m starting to understand why people like it so much. A bit part of it is that its just so damn simple. Its so easy to write code that works very quickly. The syntax is very elegant and easy to read. I’ve come to actually prefer the indent-based code grouping over most other language’s braces.

But one thing that has been plaguing me is how a dynamiclanguage could actually be faster than a static language like C++ or Java. I have faith in the theory, but its just that: faith. Until today, I didn’t have any real world examples. I needed something that you simply can’t do in a static language without bending over backwards. Conversely, I needed something that required the dynamicism of a language.

Always good examples for problems like that are searching or sorting issues.

What I particularly like is the “key” function that you can now pass to the sort method in Python 2.4. You can simply pass a method that retrieves the “key” to a sort. Then when the sort is performed, the keys are more or less cached, and a fast internal comparison can be used.

For example:

MyList = [(2,3,4), (5,3,4), (1,2,3)]
def get_key(v):
  return v[2]
 
MyList.sort(key=get_key)

Now compare this to a traditional language, which might allow you to pass in a sort callback:

int* MyList[] = [ [2,3,4], [5,3,4], [1,2,3] ]
 
int MyCompare(void* a, void* b) {
  key1 = ((int*)a)[2]
  key2 = ((int*)b)[2]
  return key1 - key2
}
 
qsort(MyList, MyCompare)

Type definitions aside, aside what we see here is that MyCompare is going to be called much more than get_key. In this simple case, obviously it doesn’t matter much, but if you’re sorting something big and retrieving/calculating the key is slow, you’re going to retrieve and calculate the key way more often in C than in Python.

The reason this works only in a dynamic language is that a dynamic comparison function (MyCompare) in C++ is still not as dynamic as the key-retrieval function. The key retrieval function is a function who takes an arbitrary object as a parameter and returns an arbitrary object. Then the sort routine needs to arbitrarily compare two objects. You just can’t do that in C without a whole lot of work.

post Question of Recursion

May 22nd, 2008

Filed under: .NET, C++, General Programming — Kai @ 2:53 pm

Algorithms can often be implemented recursively or nonrecursively; the decision rests with the programmer, who might shy away from a recursive solution because the algorithm might not terminate or that performance might be poor. In reality, recursion can allow for very elegant code as well as facilitating an interesting and economical type of code reuse.

Iterative algorithms, especially those with a loop, can usually be easily rewritten to use recursive function calls instead of loops. In some cases it’s a bad idea because the iterative version is usually simpler, faster, and uses less memory.

The classical scenario for recursion was search algorithms, factorial calculations, and so on. A well-know algorithmn that uses recursion is Binary Search.

This recursive version checks to see if we’re at the key (in which case it can return), otherwise it calls itself so solve a smaller problem, ie, either the upper or lower half of the array.

int BinarySearch(int iSortedArray[], int iFirst, int iLast, int iKey) 
{
   if (iFirst <= iLast) {
       int iMid = (iFirst + iLast) / 2;  
       if (iKey == iSortedArray[iMid]) 
           return iMid;  
       else if (iKey < iSortedArray[iMid]) 
           return BinarySearch(iSortedArray, iFirst, iMid-1, iKey);
       else
           return BinarySearch(iSortedArray, iMid+1, iLast, iKey);
   }
   return -(iFirst+ 1);    // failed to find key
}

Besides elegant code design recursion can be the reason for some problems.
Recursion isn’t safe in C/C++ because there is no reasonable way to deal with running out of call stack space. Even if you dynamically allocate stack frames from the heap, you still can run out of heap and how do you handle the error then?

You might convert the failed call stack allocation into an exception that unwinds the stack until an out-of-stack exception handler is found, but the problem is that any function in any library called from a recursive routine might be the call that blows the stack.

Fortunately any algorithm that can be written recursively can be rewritten iteratively by keeping a heap-based stack object (and if it can be written completely tail-recursively, you don’t even need a stack). The code might be uglier when written iteratively, but it’s always possible.

You’d defenetly use recusion when you can guarantee two things:

  • Each recursive step breaks down the problem into a smaller problem of the same type.
  • Each recursive step reduces the problem significantly.

The first is a general rule of recursion. If each step doesn’t break the problem down into a smaller problem of the same type, it’s harder to write a recursive function and guarantee that it will terminate. The second is kinda personal guideline. I generally won’t write a recursive function unless it divides the problem in half with each step. This way I can verify with relative ease that the algorithm will be efficient.

Originally I planted to write this post about anonymous recursion with lambda in c#, but then I decited to describe recursion basics a bit more in detail.

A simple recursive function in c# would be for example:

private static double pow(double var, double n)
{
    if(n == 0)
        return 1;
    else
        return var * pow(var, n - 1);
}

Anonymous recursion is a recursion technique that uses anonymous functions.
I got the idea to define a special delegate type for self-applicable functions:

delegate T Pow<T>(Pow<T> self);

The delegate above can be defined like this:

Pow<double> myPow = f => f(f);
 
myPow(myPow);

When myPow is being applied to itselfwill apply myPow to itself, which will … infinite recursion. While not particularly useful in this setting, the example demonstrates that you can in fact have recursive application in a lambda expression. Now all we have to do is to make it do something useful as it goes, such as call functions recursively.

Recursion is beautiful and lambdas are the ultimate abstraction. But that’s too much for now…I’ll write about that sometime.

post PowerCommands for Visual Studio

May 19th, 2008

Filed under: Visual Studio — Kai @ 10:58 am

PowerCommands 1.1 is a set of useful extensions for the Visual Studio 2008 adding additional functionality to various areas of the IDE. The source code is included and requires the VS SDK for VS 2008 to allow modification of functionality or as a reference to create additional custom PowerCommand extensions.

VS

Useful gimmicks are for instance opening of a Visual Studio Command Prompt, which has the right path to the current project on start. I really do like the “Undo Close” function, which helps you if you have clicked accidently a window into nirvana.

Below is a list of the included in PowerCommands:

  • Collapse Projects
  • Copy Class
  • Paste Class
  • Copy References
  • Paste References
  • Copy As Project Reference
  • Edit Project File
  • Open Containing Folder
  • Open Command Prompt
  • Unload Projects
  • Reload Projects
  • Remove and Sort Usings
  • Extract Constant
  • Clear Recent File List
  • Clear Recent Project List
  • Transform Templates
  • Close All

More about extending Visual Studio can be found Visual Studio Extensibility Development Center.

post SP 1 (Beta) for Visual Studio 2008 and .NET Framework 3.5

May 14th, 2008

Filed under: .NET, Visual Studio — Kai @ 1:33 pm

As I recently have experienced a few days ago the first beta version of the service pack 1 for Visual Studio 2008 and .NET Framework 3.5 have been released.

The release of the final version is announced for summer.

This servicing updates provide a roll-up of bug fixes and performance improvements.
Furthermore, additions and enhancements that make building .NET applications have become better.

The parts that provide development with C++/MFC won’t have many new features but C# development is said to be improved.

In any case you’d be aware of:

  • Users that have Windows Vista should have SP1 for Vista installed before upgrading the .NET 3.5 Framework with SP1.
  • Any users who still work with beta of Visual Studio 2008 (Codename Orcas I think) will get in trouble when trying to install the service pack. A detailed step-guide can be found here.
  • There is a change in behavior in the .NET 3.5 SP1 beta that causes a problem with the shipping versions of Expression Blend (WYSIWYG-Editor für XAML). The recommented way to work around this issue is to download the recently updated version of Blend 2.5.

At least bear in mind “be careful - it’s still beta form”. The usage on critical system is not recommented yet.

Scott Guthrie (Corporate Vice President der Microsoft Developer Division) has done a great blog post that shows the improvments in detail.
It can be found here.

The Visual Studio SP1 Beta is quite extensive and includes support for SQL Server 2008, new ADO.NET features, improvements to the WPF designers, WCF templates for Silverlight projects, debugger support for the .NET Framework public symbols and source release, control improvements and Visual C++ improvements such as the Office 2007 Ribbon bar.

post Keep Your Pocket Code

May 13th, 2008

Filed under: General Programming, Internet — Kai @ 9:10 am

Every developer, including me, has some amount of code that they feel is reusable to them, but doesn’t clear that bar to be reusable for everyone.

This is what I call Pocket Code - reusable code that does not belong in reusable libraries that is shared amongst projects and team members, but code that you keep handy somewhere to be cut-and-pasted into applicable projects.

For the reason not to keep just around with you on a memory stick here are some ideas besides keeping it more clever:

Development Websites

There are tons of code snippet sites and directories in the web. Some are generic, while others are very specific to language or need. Here are the few that I use frequently:

Please feel free to add a comment below to your favorite snippet website.

Repository

The first thing I do when starting a new project is to create the SVN repository. The second thing I do is immediately create a a toolbox of repository for that project.

Anything code that does not contribute to the product but does indirectly support it (quick and dirty data migration apps, record matching apps, or that crummy State enum) gets saved for prosperity in this repository.

I have seen people attempt to organically grow “reusable” libraries from these snippets in the forms of assemblies and jars. I highly frown upon this practice - you wouldn’t keep your yeast and flour in the same jar, why are you keeping junk and clean code together?

Blogging

The majority of developers who blog create posts only around code - making their code available for the entire world.

Here are a couple of thoughts on this:

  • I love bloggers who post and explain code because it adds to community learning
  • I loathe bloggers who post uncommented code with no explanation further than - “thought this might be useful to someone else”
  • If you are blogging about something - normally you are passionate about the topic, so I will take that piece of code a little more seriously.
ruldrurd
Next Page »
Powered by WordPress, Content and Design by Kai Bellmann
Entries (RSS) and Comments (RSS)