Showing posts with label database. Show all posts
Showing posts with label database. Show all posts

Wednesday, March 25, 2009

4 database abstraction layers for Python

Database abstraction layers for Python includes:

  1. DB-API
    A standard python api and each db creates their own module that uses it.


  2. CouchDB Python
    Python library for working with CouchDB


  3. SQLObject
    Object Relational Manager for providing an object interface to your database, with tables as classes, rows as instances, and columns as attributes.


  4. SQLAlchemy
    Enables classes to be mapped against the database in more than one way.


Friday, April 25, 2008

How to backup WordPress data

Backup is a process of duplicating data, so that it could be used to restore the original after a data loss event. This tutorial will show how to backup your WordPress posts, pages, comments, custom fields, categories, and tags easily, using WordPress eXtended RSS (WXR).

To backup:

Step 1: Login to your WordPress Control Panel.
Step 2: Click on Manage.
Step 3: Click on Export
Step 4: Click on "Download Export File" button.



You will download the WXR file, for example "wordpress.2008-04-24.xml". 2008-04-24 is the date of the WXR file downloading.

To restore:


Step 1: Login to your WordPress Control Panel.
Step 2: Click on Manage.
Step 3: Click on import.
Step 4: Scroll down to the end of the Import list.
Step 5: Click on Wordpress.
Step 6: Click on the Browse button, and choose the WXR file.
Step 7: Specify the author. Click on "Download and import file attachments", if necessary.
Step 8: Click on the Submit button.

note: The maximum WXR size that can be imported, may vary, depends on the PHP settings.

Wednesday, April 16, 2008

Python: Parse Apache log to sqlite database

This Python script was written for a friend in Australia, as part of his Ph.D project. The script will parse the Apache server log into sqlite3 database.

Apache server log like this:
23.13.171.152 - - [26/Sep/2007:21:20:36 +0800]
“GET /forum/Themes/BlueStory/images/bbc/ftp://ftp.gif HTTP/1.1? 200 191
“http://www.malaysiaindah.com/index.php?action=post;topic=587.0;
num_replies=3?
“Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7)
Gecko/20070914 Firefox/2.0.0.7?
could be parse to an sqlite database, with the class below.
class Parser:
    def __init__(self, serverLog, db):
        if db.strip() == "":
        db = "log.db"
        try:
            conn = sqlite3.connect(db)
            cursor = conn.cursor()
            cursor.execute("create table if not exists log
            (ip, date, time, gmt, request,
            errorcode, bytes, referel, osa)" )
        except sqlite3.Error, error:
            wx.MessageBox(str(error), "Info")
            exit()

        numLog = len(open(serverLog).readlines())
        for line in open(serverLog, "r"):
            data = []
            a = line.split(‘"‘)
            line = line.split()
            data.append(line[0])

        #ip
        data.append(line[3][1:line[3].index(":")])

        #date
        data.append(line[3][line[3].index(":") + 1:])

        #time
        data.append(line[4][:-1])

        #gmt
        data.append(line[5][1:] + " " + line[6])

        #urlreq
        data.append(line[8])

        #statuscode
        data.append(line[9]) #bytestr data.append(line[10][1:-1])

        #referel
        data.append(a[-2])

        #osagent
        try:
            cursor.execute( "insert into log values(
             ?, ?, ?, ?, ?, ?, ?, ?, ?)",
            (data[0], data[1], data[2], data[3],
            data[4], data[5], data[6],
            data[7], data[8]) )
        except sqlite3.Error, error:
            wx.MessageBox(str(error), 'Info')
            exit()
        
        conn.commit()
        cursor.close()
        conn.close()