Monday, January 19, 2015

SQL table - basic

Pada kesempatan kali ini akan dipelajari tentang pembuatan table pada sqlite menggunakan sqlite shell. Tutorial ini cuma menunjukkan teknik-teknik dasar saja tidak ada yang begitu rumit. Ok, pertama-tama download dulu sqlite shellnya dari situs resmi sqlite disini. Karena saya menggunakan windows, jadi yang didonlot adalah shell untuk windows, jika menggunakan linux, windows phone, atau mac, silahkan pilih sesuai dengan OS yang digunakan.
Setelah didownload, buat saja folder dan extract file zipnya didalam folder tersebut. Shell sqlite ini cuma terdiri dari satu file exe seperti terlihat di bawah :

Sekarang mari kita buka sqlite3.exe tersebut menggunakan command prompt. Caranya tekan shift kemudian klik kanan folder "sqlite", terus pilih "Open command window here" seperti terlihat :

Kemudian pada command prompt masukkan perintah "sqlite3 test1.db" terus tekan enter. Hasilnya :


Perintah sqlite3 maksudnya adalah kita akan membuka sqlite3.exe tersebut dengan nama database test1.db. Jika database tersebut belum ada maka akan dibuat yang baru, namun jika sudah ada maka database tersebut akan dibuka saja. Jadi sampai disini kita telah berhasil membuat database baru dengan nama test1.db. Ok selanjutnya mari buat table dalam database tersebut dengan memasukkan perintah :

create table test1table(name text,umur integer);

Karena SQlite itu incase sensitive, jadi perintah di atas dapat pula ditulis seperti ini :

CREATE TABLE TEST1TABLE(NAME TEXT, UMUR INTEGER);

Jadi terserah saja mau pake huruf kapital atau huruf kecil atau campuran dua-duanya tidak masalah.

Setelah menekan enter, dan tidak ada yang error, maka kita telah berhasil membuat suatu table dengan nama test1table pada database test1.db dengan parameter table tersebut adalah nama sebagai text dan umur sebagai integer. Terus didalam folder sqlite kita terdapat file baru dengan nama test1.db seperti terlihat dibawah :

Kemudian masukkan data baru kedalam table tersebut dengan menggunakan perintah :

insert into test1table(name,umur) values('Hisoka',123);


Nah untuk melihat data yang baru saja kita masukkan kedalam table, masukkan perintah :

select * from test1table;


Terlihat data yang baru saja dimasukkan yaitu Hisoka dengan umur 123. Biar penampilan isi table lebih bagus, Eksekusi perintah berikut :

.mode column; tekan enter kemudian masukkan lagi :

.header on;


Kemudian eksekusi lagi perintah :
select * from test1table;


Nah terlihat bentuknya lebih terlihat manusiawi dari yang sebelumnya :D

Ok berikutnya kita akan melakukan perintah delete, namun sebelum itu ada baiknya kita masukkan 1 element lagi ke dalam table dengan nama: Poipo dan umur: 1234

Nah... element yang Poipo itu akan didelete dengan perintah :

delete from test1table where name='Poipo';


Nah terlihat element pada table tersisa Hisoka saja karena yang Poiponya sudah didelete.. :-)

Terus command yang terakhir adalah update. Misalhkan kita ingin mengganti umur Hisoka menjadi 37, perintahnya :

update test1table set umur=379 where name="Hisoka";

Terlihat sekarang umurnya sudah menjadi 379.
Ok... cukup sekian basic untuk table di sqlite..Sebagai catatan disini name adalah text sehingga membutuhkan tanda ' ', sedangkan integer langsung ditulis saja angkanya.

Semoga bermanfaat... :-)

Friday, January 16, 2015

Get ATR using pcsc-sharp

This time I'm gonna show you how to get an ATR from a smartcard using pcsc-sharp wrapper step by step. First you have to download the pcsc-shrap project, you can download that from this site. After downloading that project, extract it into your pc hardisk. Then let's start by making a new project in visual studio 2010 :

Then add one button, textbloct and viewbox from toolbox menu to mainwindow.xml then arrange those items according to picture below :


If you are confuse with that, you can directly edit the xml file like this :

Well, you can see that the main window is so simple, so you can make your own window as you like. Next let's add the pcsc-sharp project into this project. Just right click on the solution project, choose "add", then "Existing Project..."  :
After clicking "Existing Project.." then browse the "pcsc-sharp.csproj" :


If everything is right, then you should see a new project in your visual studio.
In order to use all of the available function in pcsc-sharp, we need to reference to it first. So right click in getATRv0 project, choose "add reference.." :

After clicking "Add Reference...", choose project tab, then choose pcsc-sharp, last hit "ok" button :

Until this state, now we can use the available method, class, or namespace from pcsc-sharp project. To prove that, let's go back to our mainwindow.xaml.cs and put these line codes :

As you can see at line 14, we are using PCSC namespace that we reference from pcsc-sharp project and at line 23 to 26, we are using the classes from PCSC namespace, so this is a proof that now we can use the pcsc-sharp project in our current visual studio project. Well, umm... I think it's good if we run our getATR project, just to make sure that everything is ok hehhee.... :D

Ok,.Let's add another variables into our project :

the szReaders will gonna be saving the reader name for our smartcard and atr will be being used to save the byte of smartcard ATR. Ok let's add some methods, the first is :
getAvailableReader will be used to make a context from our reader that already connected to our PC. That method is also used to list all of the available reader then put the readers name to szReader variabel.

The next method is :
As you can see from the method's name, connecReader will be used to connect our application to smartcard reader.

The last method is getATR() as we can see in below :

getATR is the main method that will accomplish our goal in this posting. This method will be used to get the ATR bytes. As a note, this method has to be preceded by getAvailableReader and connectReader methos. Btw, if getATR method failed to be executed or an error occured during getATR method execution, then atrbuff will be filled up with 0x6E and 0x00.

Next, let's add a handle code to our button. Just double click the button in your xml file, then add these line codes into it :
As we can see, when the button is pressed by user, the getAvailableReader's method is called first then connectReader. After that we call our getAtr which put the ATR bytes into atr variable, then we display those atr bytes into our textbox with helped by BitConverter that change atr bytes into a hex string.

The result from this project is shown below :

Thanks,.. I hope this posting is usefull to someone who has been facing the same problem with me when trying to find a way to get the ATR from smartcard using pcsc-sharp :-)