#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Posts
    52
    Rep Power
    14

    PHP and Python DB


    I am designing a website for a hobby of mine and part of it is in XML dynamically generated by using PHP to extract the element's appropriate data from a database and then parsing it with an XSLT stylesheet (again w/ PHP). This can be done fairly easily but part of the purpose of the site is to practice web programming. I wanted to learn Python so I am using that to develop a communications script for a different section. While studying the language, I had an idea and wanted to know how viable it is. What if instead of using MySQL for the database, I used a Python dictionary? It may sound silly, but would it be slow, perhaps just plain stupid? I really don't need any of the special functions of a relational database...but should I just stick to MySQL? Thanks.

    Ryan Kaskel - MIA
    Last edited by ryankask; June 7th, 2004 at 10:50 PM. Reason: grammar
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    It would work, all you'd have to do is show PHP how to parse the dictionary from a file and insert it into a hash. Though I dont see the need for it if your just learning it should be fun . Yes, it might be slower than MySQL; especially with large data sets. Give it a go .

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    If you can hold all the data in memory all the time, then using a dictionary will be orders of magnitude faster than using a DB. If you have to save and load it from disk each time then I think it will be faster for small data sets, but the DB will overtake it at some point. You may also have the issue of multiple users trying to access the file at the same time, with the potential for data loss.

    There is an alternative. Look at the dbm family of modules in the standard library. These look like dictionaries but are simple databases (not relational, so do not have all the overheads associated with that). I recommend bsddb if your OS supports it, since it can support transactions and multiple simultaneous users.

    Dave - The Developer's Coach
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Posts
    52
    Rep Power
    14
    So the dbm module with a standard Python distribution? I assume my web host (which supports Python) will have this module preinstalled? Thank you, I will look into it.
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    There are actually a number of dbm modules included with Python. Which ones you have will depend on your playform etc. But there is always at least one available. And you can also use the Pickle module for this kind of thing . Check out the Python docs for more info...

    Have fun,

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Originally Posted by ryankask
    So the dbm module with a standard Python distribution? I assume my web host (which supports Python) will have this module preinstalled? Thank you, I will look into it.
    It is standard but optional, depending on the OS. Most UNIX systems have dbm and/or gdbm. Windows and some UNIX installations have bsddb.

    All installations have dumbdbm available, which is a simple implementation in Python, and is meant as a last resort.

    If you use anydbm then it will transparently use the best implementation available on that system.

    I think only bsddb supports multiple users, so if you are using it in a web app then check with your host to see if it is available (or write a short CGI test script).

    Dave - The Developers' Coach

IMN logo majestic logo threadwatch logo seochat tools logo