DEV Community

Zeki Ahmet Bayar for Açıklab

Posted on • Edited on

2

Windows Server Üzerinde SQLite Kullanımı

Kurulum adımlarını tamamladıktan sonra SQLite'a tam bir giriş yapmak için SQL üzerindeki temel işlemleri beraber inceleyelim.

Veri Tabanı ve Örnek Tablo Oluşturulması

Function createDataBase([string]$db) {
    Try {
        If (!(Test-Path $db)) {

            $CONN = New-Object -TypeName System.Data.SQLite.SQLiteConnection

            $CONN.ConnectionString = "Data Source=$db"
            $CONN.Open()

            $createTableQuery = "CREATE TABLE test(
            ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
            name              TEXT    NULL,
            surname           TEXT    NULL,
            number            TEXT    NULL,
            address           TEXT    NULL
            );"

            $CMD = $CONN.CreateCommand()
            $CMD.CommandText = $createTableQuery
            $CMD.ExecuteNonQuery()
            $CMD.CommandText = $createUniqueIndex
            $CMD.ExecuteNonQuery()

            $CMD.Dispose()
            $CONN.Close()
            Write-Host "Veri tabanı başarıyla oluşturuldu."

        } Else {
            Write-Host "Veri tabanı zaten var."
        }

    } Catch {
        Write-Host "Veri tabanı oluşturulamadı !"
    }
}
Add-Type -Path "C:\Windows\sqlite\System.Data.SQLite.dll"
createDataBase "C:\Windows\db\testVT.sqlite"
Enter fullscreen mode Exit fullscreen mode

Yukarıdaki Powershell betiğinde kısaca test adında bir veri tabanı oluşturuyor, bu veri tabanının içine de id, ad, soyad, numara ve adres kolonlarını içeren bir tablo ekliyoruz.

Tabloya Veri Ekleme İşlemi

Function insertDatabase([string]$db, [System.Collections.ArrayList]$rows) {

    Try {
        If (Test-Path $db) {

            $CONN = New-Object -TypeName System.Data.SQLite.SQLiteConnection

            $CONN.ConnectionString = "Data Source=$db"
            $CONN.Open()

            $CMD = $CONN.CreateCommand()
            ForEach($row in $rows) {
                $sql = "INSERT OR REPLACE INTO test (ID,name,surname,number,address)"
                $sql += " VALUES ((select ID from test where number = @number),@name,@surname,@number,@address);"

                $CMD.Parameters.AddWithValue("@ID", $NULL)
                $CMD.Parameters.AddWithValue("@name", $row.name)
                $CMD.Parameters.AddWithValue("@surname", $row.surname)
                $CMD.Parameters.AddWithValue("@number", $row.number)
                $CMD.Parameters.AddWithValue("@address", $row.address)

                $CMD.CommandText = $sql
                $CMD.ExecuteNonQuery()
            }

            $CMD.Dispose()
            $CONN.Close()

            Write-Host "Kayıtlar başarıyla eklendi."

        } Else {
            Write-Host "Veri tabanına ulaşılamıyor."
        }

    } Catch {
        Write-Host "Kayıtlar veri tabanına eklenemedi."
    }
}

Add-Type -Path "C:\Windows\sqlite\System.Data.SQLite.dll"
$Rows = New-Object System.Collections.ArrayList
$Rows.Add(@{'name'='Zeki Ahmet'; 'surname'= 'Bayar'; 'number'='5986'; 'address'='127.0.0.1'})
insertDatabase "C:\Windows\db\testVT.sqlite" $Rows
Enter fullscreen mode Exit fullscreen mode

Eklenmek istenen satırlar, main aşamasında tanımlanan Rows listesine add methodu yardımı ile eklenip, insertDatabase fonksiyonuna gönderilmelidir. insertDatabase fonksiyonu eğer verilen numara tabloda başka bir numara ile aynı ise onu güncelleyecek, daha önceden böyle bir satır eklenmediyse yenisini ekleyecektir.

Tablonun Okunması

Function queryDatabase([string]$db, [string]$sql) {

    Try {
        If (Test-Path $db) {

            $CONN = New-Object -TypeName System.Data.SQLite.SQLiteConnection
            $CONN.ConnectionString = "Data Source=$db"
            $CONN.Open()

            $CMD = $CONN.CreateCommand()
            $CMD.CommandText = $sql

            $ADAPTER = New-Object  -TypeName System.Data.SQLite.SQLiteDataAdapter $CMD
            $DATA = New-Object System.Data.DataSet

            $ADAPTER.Fill($DATA)

            $TABLE = $DATA.Tables

            ForEach ($t in $TABLE){
                Write-Output $t
            }

            $CMD.Dispose()
            $CONN.Close()

        } Else {
            Write-Host "Veritabanı bulunamadı."
        }

    } Catch {
        Write-Host "Sorgu gönderilemiyor."
    }
}
Add-Type -Path "C:\Windows\sqlite\System.Data.SQLite.dll"
$Query = "Select * From test"
queryDatabase "C:\Windows\db\testVT.sqlite" $Query 
Enter fullscreen mode Exit fullscreen mode

Bu kısımda da oluşturduğumuz veri tabanına eklediğimiz satırları kontrol etmek için basit bir select ifadesini veri tabanına nasıl gönderebileceğimizi gördük. Query değişkenine başka sql sorguları yazarak onların da çıktılarını bu fonksiyon yardımı ile görebilirsiniz.

Bu dokümanda kullanılan fonksiyonlar buradaki bağlantıdan uyarlanmıştır.

Image of AssemblyAI tool

Challenge Submission: SpeechCraft - AI-Powered Speech Analysis for Better Communication

SpeechCraft is an advanced real-time speech analytics platform that transforms spoken words into actionable insights. Using cutting-edge AI technology from AssemblyAI, it provides instant transcription while analyzing multiple dimensions of speech performance.

Read full post

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay