analitics

Pages

Saturday, August 1, 2009

Sqlite and Python

These script explore the Python Sqlite modules available for database administration.
Sometime you need to use extraction, processing, storage and presentation of your data.
This is a short example :
#!/usr/bin/python

import sqlite3

def select_db():
print "Select database"

co=sqlite3.connect("cata2.db")
cursor=co.execute("CREATE TABLE report (nr INT,user VARCHAR(20),descriere VARCHAR(100));")
cursor=co.execute("INSERT INTO report (nr,user,descriere) VALUES (1,'root','root administration');")
cursor=co.execute("SELECT * FROM report;")

print cursor.fetchall()

This python script will be create cata.db file on your folder.
The table of database is 'report' and add next values "1,'root','root administration'".
If you want create new database , use :
co=sqlite3.connect("new_database.db")
cursor=co.execute("CREATE TABLE tabela (some_value_integer INT,some_value_chars VARCHAR(20));")

If you want show it , use this :
cursor=co.execute("SELECT * FROM new_database;")

Thank you !

Saturday, July 4, 2009

resize images to create thumbnails

First you have many images.
If you want to create thumbnail for each one this is python script:

import glob 
import PIL
from PIL import Image
for infile in glob.glob("*.jpg"):
 img = Image.open(infile)
 dim_percent=(100/float(img.size[0]))
 dim_size=int((float(img.size[1])*float(dim_percent)))
 img = img.resize((100,dim_size),PIL.Image.ANTIALIAS)
 if infile[0:2] != "trumb_":
  img.save("trumb_" + infile, "JPEG")