Halaman

Jumat, 07 Mei 2010

SQLite: ROWID and VACUUM

A. ROWID
Every row of every SQLite table has a 64-bit signed integer key that is unique within the same table. This integer is called "rowid". The rowid is the actual key used in the B-Tree that implements an SQLite table. Because rows are stored in rowid order, a rowid is a candidate for a primary key, as it can uniquely identify a record within a table...

Below we see how it works:

1. WITHOUT Primary Key Keyword
sqlite>CREATE TABLE t1 (name TEXT);
sqlite>insert into t1 values ('a');
sqlite>insert into t1 values ('b');
sqlite>insert into t1 values ('c');
sqlite>insert into t1 values ('d');
sqlite>select rowid, name from t1;
rowid name
1 a
2 b
3 c
4 d

sqlite>delete from t1 where name='a';
sqlite>delete from t1 where rowid=3;
sqlite>select rowid, name from t1;
rowid name
2 b
4 d
sqlite>vacuum;
sqlte>select _rowid_, name from t1;

rowid name
1 b
2 d

2. WITH Primary Key Keyword
CREATE TABLE t2 (pk INTEGER PRIMARY KEY, name TEXT);

insert into t2(name) values ('a');
insert into t2(name) values ('b');
insert into t2(name) values ('c');
insert into t2(name) values ('d');

select rowid, pk, name from t2;
pk pk_1 name
1 1 a
2 2 b
3 3 c
4 4 d

delete from t2 where name='a';
delete from t2 where rowid=3;
select oid, pk, name from t2;
pk pk_1 name
2 2 b
4 4 d

sqllite>vacuum;
select oid, pk, name from t2;
pk pk_1 name
2 2 b
4 4 d

B. OBSERVED
- As we saw, the ROWID in the second sample will be retain static!
- So, don't forget to include INTEGER PRIMARY KEY in your Create Table statement.
- Rowid is integer key with amount of 9223372036854775807. Is it enough for you?.
- The rowid value can be accessed using one of the special names "ROWID", "OID", or "_ROWID_".
- If a column is declared to be an INTEGER PRIMARY KEY, then that column is not a "real" database column but instead becomes an alias for the rowid. Unlike normal SQLite columns, the rowid must be a non-NULL integer value.
- The VACUUM command may change the ROWIDs of entries in tables that do not have an explicit INTEGER PRIMARY KEY.

C. VACUUM
When an object (table, record, index, trigger, or view) is dropped from the database, it leaves behind empty space. This empty space will be reused the next time new information is added to the database. But in the meantime, the database file might be larger than strictly necessary. Also, frequent inserts, updates, and deletes can cause the information in the database to become fragmented - scrattered out all across the database file rather than clustered together in one place.

The VACUUM command cleans the main database by copying its contents to a temporary database file and reloading the original database file from the copy. This eliminates free pages, aligns table data to be contiguous, and otherwise cleans up the database file structure.

Sabtu, 01 Mei 2010

Unlock DataGridView properties in Inherited Form

Form inheritance improve our coding time. But I'm facing a problem while inherit a form that contain DataGridView control. I could not modify the DataGridView properties. Eventhough change the modifier property to protected or public. The DataGridView control still locked in designer.

Here a brief step to work with:
1. I add a new class file for example FerDataGrdView.cs in my library objects (mylib.dll).
2. Type this code:
using System.Windows.Forms;
using System.ComponentModel;
namespace Ferryptk
{
[Designer(typeof( System.Windows.Forms.Design.ControlDesigner))]
public class FerDataGrdView : DataGridView { }
}

3. Build library (mylib.dll).
4. Add reference to mylib.dll
5. Drag the control (FerDataGrdView) in ancestor form.
6. Do form inheritance.
7. Now, I could modify the properties of DataGridView control.

O.. happy day..

Selasa, 27 April 2010

SQLite is so simple

SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. It means a simplicity. Simplicity in a database engine can be either a strength or a weakness, depending on what you are trying to do.

A. Appropriate Uses
The primary design goal of SQLite is to be simple:
- Simple to administer
- Simple to operate
- Simple to embed in a larger program
- Simple to maintain and customize

So use SQLite in situations where simplicity of administration, implementation, and maintenance are more important than the countless complex features that enterprise database engines provide. SQLite is small, fast and reliable. Reliability is a consequence of simplicity. With less complication, there is less to go wrong.

B. Situations Where SQLite Works Well
1. Application File Format
SQLite has been used with great success as the on-disk file format for desktop applications such as financial analysis tools, CAD packages, record keeping programs, and so forth. The traditional File/Open operation does an sqlite3_open() and executes a BEGIN TRANSACTION to get exclusive access to the content. The use of transactions guarantees that updates to the application file are atomic, consistent, isolated, and durable (ACID).

2. Embedded devices and applications
SQLite is a good choice for devices or services that must work unattended and without human support. SQLite is a good fit for use in cellphones, PDAs, set-top boxes, and/or appliances. It also works well as an embedded database in downloadable consumer applications.

3. Websites
SQLite usually will work great as the database engine for low to medium traffic websites (which is to say, 99.9% of all websites). The amount of web traffic that SQLite can handle depends, of course, on how heavily the website uses its database. Generally speaking, any site that gets fewer than 100K hits/day should work fine with SQLite.

4. Replacement for ad hoc disk files
Many programs use fopen(), fread(), and fwrite() to create and manage files of data in home-grown formats. SQLite works particularly well as a replacement for these ad hoc data files.

5. Internal or temporary databases
For programs that have a lot of data that must be sifted and sorted in diverse ways, it is often easier and quicker to load the data into an in-memory SQLite database and use queries with joins and ORDER BY clauses to extract the data in the form and order needed rather than to try to code the same operations manually. Using an SQL database internally in this way also gives the program greater flexibility since new columns and indices can be added without having to recode every query.

6. Command-line dataset analysis tool
Experienced SQL users can employ the command-line sqlite program to analyze miscellaneous datasets. Raw data can be imported from CSV files, then that data can be sliced and diced to generate a myriad of summary reports. Possible uses include website log analysis, sports statistics analysis, compilation of programming metrics, and analysis of experimental results.

You can also do the same thing with an enterprise client/server database, of course. The advantages to using SQLite in this situation are that SQLite is much easier to set up and the resulting database is a single file that you can store on a floppy disk or flash-memory stick or email to a colleague.

7. Stand-in for an enterprise database during demos or testing
If you are writing a client application for an enterprise database engine, it makes sense to use a generic database backend that allows you to connect to many different kinds of SQL database engines. It makes even better sense to go ahead and include SQLite in the mix of supported databases and to statically link the SQLite engine in with the client. That way the client program can be used standalone with an SQLite data file for testing or for demonstrations.

8. Database Pedagogy
Because it is simple to setup and use (installation is trivial: just copy the sqlite or sqlite.exe executable to the target machine and run it) SQLite makes a good database engine for use in teaching SQL. Students can easily create as many databases as they like and can email databases to the instructor for comments or grading.

11. Experimental SQL language extensions
The simple, modular design of SQLite makes it a good platform for prototyping new, experimental database language features or ideas.

C. Situations Where Another RDBMS May Work Better
1. Client/Server Applications

If you have many client programs accessing a common database over a network, you should consider using a client/server database engine instead of SQLite. A good rule of thumb is that you should avoid using SQLite in situations where the same database will be accessed simultaneously from many computers over a network filesystem.

2. High-volume Websites
SQLite will normally work fine as the database backend to a website. But if you website is so busy that you are thinking of splitting the database component off onto a separate machine, then you should definitely consider using an enterprise-class client/server database engine instead of SQLite.

3. Very large datasets
With the default page size of 1024 bytes, an SQLite database is limited in size to 2 tebibytes (241 bytes). And even if it could handle larger databases, SQLite stores the entire database in a single disk file and many filesystems limit the maximum size of files to something less than this. So if you are contemplating databases of this magnitude, you would do well to consider using a client/server database engine that spreads its content across multiple disk files, and perhaps across multiple volumes.

4. High Concurrency
SQLite uses reader/writer locks on the entire database file. That means if any process is reading from any part of the database, all other processes are prevented from writing any other part of the database. Similarly, if any one process is writing to the database, all other processes are prevented from reading any other part of the database. For many situations, this is not a problem. Each application does its database work quickly and moves on, and no lock lasts for more than a few dozen milliseconds. But there are some applications that require more concurrency, and those applications may need to seek a different solution.

D. Want to explore?
1. Installation
Download Precompiled Windows Binaries:
1. Download http://www.sqlite.org/sqlite-3_6_23_1.zip (contains: sqllite3.exe)
2. and this http://www.sqlite.org/sqlitedll-3_6_23_1.zip (This is a DLL of the SQLite library)
3. Extract all the file and put in one directory (ex: C:/sqlite3)

C:\sqlite3>dir
Volume in drive C has no label.
Volume Serial Number is 9863-4221
Directory of C:\sqlite3
2010-04-27 00:13 .
2010-04-27 00:13 ..
2010-03-29 16:02 3.809 sqlite3.def
2010-03-29 16:02 520.234 sqlite3.dll
2010-03-29 16:02 528.522 sqlite3.exe
3 File(s) 1.052.565 bytes
2 Dir(s) 84.336.992.256 bytes free

The only external dependency is MSVCRT.DLL.

2. Create Database
A standalone program called sqlite3 is provided which can be used to create a database, define tables within it, insert and change rows, run queries and manage an SQLite database file. This program is a single executable file on the host machine. It also serves as an example for writing applications that use the SQLite library.


C:\sqlite3>sqlite3 pontianak.db3
SQLite version 3.6.23.1
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .exit

SQLite will create a new one, if the database does not exist.

3. Create Table
If you are not exit in the previous step, just do

sqlite> create table create table mytab (pk integer primary key, nametext not null);
sqlite> .table (this a sqllite command line to display tables, for other commands just type .help
mytab
sqlite>

or if you have exitted, type this:

C:\sqlite3>sqlite3 pontianak.db3 "create table mytab (pk integer primary key, nametext not null);


4. Insert Data
C:\sqlite3>sqlite3 pontianak.db3 "insert into mytab values (100, 'My Name');"
C:\sqlite3>sqlite3 pontianak.db3 "insert into mytab values (200, 'Your Name');"
or

sqlite>insert into mytab values (300, 'Tova Naruto');


5. Display Data
C:\sqlite3>sqlite3 pontianak.db3 "select * from mytab ;"
or

sqlite>select * from mytab;
100My Name
200Your Name
300Tova Naruto


E. GUI for Data Administration
Yes, you are absolutely right...for productivity reason we use GUI for data administration. You can find some in Management Tools , and I use SQLite Administration.

As stated above, if we want to move our database into another folder, computer, other device or mail it, just copy and paste pontianak.db3 database into destination.

At all...it's so simple and now it's time for me to do my job.
Happy coding and for detail see this.

Jumat, 23 April 2010

Benchmark Best Practice

Ilustrasi: Berdasarkan hasil Survey Pelanggan terdapat petunjuk bahwa Pelanggan menghabiskan waktu yang lama saat reservasi hotel. Pihak hotel berpikir untuk menemukan best practice-nya dan memutuskan melakukan benchmark ke Unit Emergency/Instalasi Gawat Darurat Rumah Sakit. Hasil benchmark disesuaikan dengan bisnis hotel, dan diterapkan: Layanan booking engine, Saluran reservasi secara elektronik dan travel agents, Sistim memberships atau corporate clients terutama terhadap tamu frequent guest/flyer sehingga mengurangi waktu check-in.

"best practice" merupakan suatu mekanisme (teknik, metode, proses, aktivitas, insentif atau penghargaan) yang diyakini lebih efektif dibanding mekanisme lain untuk diterapkan pada kondisi dan keadaan tertentu.

Teringat pada saat sekolah -atau secara umum dalam dunia pendidikan- kita pasti familiar dengan study tour. Misalnya study tour ke pabrik Mobil. A study tour is a travel experience with specific learning goals. Dengan studi tour, kita mengetahui bagaimana bentuk nyata (real world) atas praktek iptek di dunia industri. Sekarang saat mengabdi maupun berkarya, kita terus melakukan proses pembelajaran, yang salah satu caranya melalui benchmark.

Benchmarking is the process of comparing one's business processes and performance metrics to industry bests and/or best practices from other industries.

Benchmarking merupakan proses pengukuran produk, servis, dan proses terhadap organisasi yang diketahui sebagai pemimpin dalam satu atau beberapa aspek operasinya. Benchmarking memberi gambaran tentang organisasi kita jika dibandingkan dengan organisasi lain, bahkan meskipun organisasi itu berada pada bisnis yang tidak serupa atau melayani kelompok pelanggan yang berbeda.

Benchmarking is a matter of identifying who's best at something (in your company, in your industry, in the world), not by guesswork or reputation but by the numbers. http://money.cnn.com/magazines/fortune/fortune_archive/1996/10/28/203926/index.htm

4 Jenis Benchmark
Empat jenis Benchmarking yang sering kita jumpai:
1. Internal benchmarking
Benchmark yang dilakukan dalam organisasi misalnya antar kantor cabang atau unit bisnisnya sendiri. Benchmark ini tentu bisa cepat dilaksanakan, biaya rendah, mudah mentransfer hal2 yang dipelajari, serta memberikan pemahaman terhadap proses sendiri dengan lebih dalam.

2. Competitive benchmarking
Benchmark ini dilakukan terhadap organisasi pesaing. Targetnya tentu apa yang menjadi kekuatan pesaing, misalnya: Desain produk, Supply Chain, Kapasitas Pabrik, Paten atau R & D.

3. Functional benchmarking
Benchmark yang dilakukan terhadap proses serupa untuk satu fungsi misalnya HRM, IT, atau Pengolahan saja.

4. Generic benchmarking
Benchmark ini dilakukan pada industri yang berbeda. Contohnya perusahaan Telekomunikasi yang melakukan benchmark ke Restaurant. Kelebihannya, kita dapat memperoleh banyak informasi karena organisasi berbeda bisnis, peluang untuk melakukan proses breakthrough.

Tahapan Benchmark
1. Nyatakan tujuan, subyek atau problem
2. Definisikan prosesnya
3. Cari partner (tempat benchmark)
4. Identifikasi dan Kumpulkan sumber data awal
5. Tentukan dan Kunjungi partner yang dipilih
6. Temukan gap-nya
7. Buat Target atau Upaya pencapaian
8. Komunikasi dan Sesuaikan Target
9. Proses perbaikan/implementasi
10. Review/kalibrasi ulang.

Sumber Benchmark
Benchmark dapat dilakukan dengan berbagai cara dan tidak terbatas pada:
- Produk
- Kunjungan langsung
- Publikasi, Pameran
- Jurnal, Industry Directories
- Customer, Employee, Supplier
- Internet, Bertanya melalui telepon, e-mail, group diskusi (mailing lists)
- Perpustakaan /Library
- Experts/ Konsultan
- Pemenang Baldrige Award

Hasil Benchmark
Dengan benchmark, kita memperoleh manfaat:
- Mengetahui posisi perusahaan dalam industrinya
- Mengetahui bagaimana organisasi beroperasi dengan ekselen
- Memberikan atmosfir untuk improvement
- Memicu pembaruan atau mempercepat improvement
- Memberi semangat baru untuk melakoni best practice,
- Menegaskan keyakinan bahwa ada kebutuhan untuk berubah
- Membantu mengidentifikasi kelemahan dan room for improvement
- dll

Benchmark dapat dilihat sebagai bentuk pemikiran dari Kaizen dan Competitive Advantage dari Porter. Praktek benchmark telah membantu organisasi dalam upayanya untuk melakukan perbaikan produk, proses atau sistem. Improvement itu dapat dilakukan secara "inkremental" dari waktu ke waktu maupun "breakthrough" improvement yang dilakukan sekaligus di suatu waktu.

Benchmark bukanlah buku resep (cook book), benchmarking adalah proses kompleks dan seringkali memerlukan effort yang besar (Process Re-Engineering atau inisiatif Quality Improvent) dan karenanya diperlukan komitmen bagi keberhasilannya. Hasil benchmarking banyak yang dapat diterapkan oleh perusahaan namun tidak sedikit yang berakhir pada sindrome 'they are different from us'.

Rabu, 21 April 2010

FPDF Multicell: Printing automatic word wrap and border

Purpose: To print columnar style report with text descriptions lie in top area of a report.
Software: PHP + FPDF
Output:

In FPDF we use Cell method/function to print columnar data, while MultiCell is used for variable-length data to fit in a specified area.

MultiCell allows printing text with line breaks. Word or text wrapping can be automatic (as soon as the text reaches the right border of the cell) or explicit (via the \n character). As many cells as necessary are output, one below the other. Text can be aligned, centered or justified. The cell block can be framed and the background painted.

The example above shows a header - as what this article want to address - as a part of a Purchase Requisition form, while the same also apply for Billing or Application form, etc.

Here is a part of my class:


class PDF extends FPDF {
//my var declaration
//my public declaration

function BuatJudulObyekKerja($nomor) {
$nrowHeight=4;
$npageWidth=179;
$nwidthCol1=40;
$nwidthCol2=30;
$nwidthCol3=25;
$nColonWidth=2;

$this->SetFont('Helvetica','B',10);
$this->Cell(0, 0,'Nomor: '.$nomor,'',1,'C',0);
$this->Ln(3);
$this->SetFont('Helvetica','',8.5);
$this->Cell($npageWidth, $nrowHeight, '', 'LTR',0,'R',0);
$this->Cell($npageWidth, 2, '', '',1,'R',0);
$this->Cell($nwidthCol1, 1,'Description', 'L' ,0,'L');
$this->Cell(nColonWidth, 1,':', '' ,0,'L');

$this->SetXY(58,33);

$y=$this->GetY();
$this->MultiCell(137,$nrowHeight,$this->pan_namaobyek,'R','J');
$y1=$this->GetY();

$nrowPrinted=($y1-$y) /$nrowHeight;
$this->SetY($y);
for ($i=0; $i < $nrowPrinted; $i++) {

$this->Cell($nwidthCol1+11, $nrowHeight,'', 'L' ,1,'L');
}


$this->Cell($nwidthCol1, $nrowHeight,'Form','LT' ,0,'L');
$this->Cell($nColonWidth, $nrowHeight,':','T' ,0,'L');
$this->Cell($nwidthCol1,$nrowHeight, $this->kategori , 'T' ,0,'L');
$this->Cell($nwidthCol2, $nrowHeight,'','T',0,'L');
$this->Cell($nwidthCol3, $nrowHeight,'Doc.ID#', 'LT' ,0,'L');
$this->Cell($nColonWidth, $nrowHeight,':','T' ,0,'L');
$this->SetFont('Helvetica','B',8.5);
$this->Cell($nwidthCol1, $nrowHeight, $this->pan_pk , 'TR',1,'L');
$this->SetFont('Helvetica','',8.5);

$this->Cell($nwidthCol1, $nrowHeight,'Raised by','LT' ,0,'L');
$this->Cell($nColonWidth, $nrowHeight,':','T' ,0,'L');
$this->Cell($nwidthCol1,$nrowHeight, $this->usaha , 'T' ,0,'L');
$this->Cell($nwidthCol2, $nrowHeight,'','T',0,'L');
$this->Cell($nwidthCol3, $nrowHeight,'Reff.No', 'LT' ,0,'L');

$this->Cell($nColonWidth, $nrowHeight,':','T' ,0,'L');
$this->Cell($nwidthCol1, $nrowHeight, $this->pan_nosurat , 'TR',1,'L');
....
}

Sabtu, 17 April 2010

Delphi 7: frame.color property frame does not exist

I just open a Quick Report form of Delphi 7 but face error message that says "...frame.color: property frame does not exist...". I checked the installed package but there is no QR component there, except Rave report.

Delphi 7 does not automatically install Quick Report component, so I did this:
1. Choose Install Packages in Component menu
2. Click Add then select dclqrt70.bpl file from Delphi7\Bin directory
3. Click Open then click OK.

The problem solved.

Delphi 7 on Windows Vista

Borland Delphi 7 was released in 2002 and perhaps you may still use this software. If we run it on Windows Vista, an error raised with the following message "Unable to rename C:\Program Files\Borland\Delphi7\Delphi32.$$$ to Delphi32.dro".

I thought that was about security access/permission. With Administrator privilege, I did these:
1. Right click on C:\Program Files\Borland\Delphi7 directory, then click Properties.
2. On General tab, clear the Read-Only attribute then press Apply (all files including subdirectory).
3. On Security tab, Check or give Full Access to Users.
4. Click Apply.

That's all.