Menambahkan Data Biner ke Tabel Menggunakan SQL

Data biner tidak dapat disisipkan ke dalam tabel secara langsung menggunakan kueri INSERT INTO atau UPDATE SQL. Untuk menambahkan data biner ke tabel, Anda harus terlebih dahulu menggunakan penanda parameter (?) dalam kueri sebagai tempat penampung untuk nilai biner. Eksekusi kueri harus menyertakan rekaman yang berisi data biner di salah satu bidangnya.

Penanda adalah referensi parameter ke nilai yang disediakan oleh rekaman yang dikirimkan dengan kueri. Ini diwakili dalam pernyataan SQL dengan tanda tanya (?).

Kode sampel berikut menambahkan data biner ke tabel.

#include <windows.h>
#include <Msiquery.h>
#include <tchar.h>
#pragma comment(lib, "msi.lib")


int main()  
{ 

PMSIHANDLE hDatabase = 0;
PMSIHANDLE hView = 0;
PMSIHANDLE hRec = 0;

if (ERROR_SUCCESS == MsiOpenDatabase(_T("c:\\temp\\testdb.msi"), MSIDBOPEN_TRANSACT, &hDatabase))
{
    //
    // Open view on Binary table so that we can add a new row, must use '?' to represent Binary data
    //

    if (ERROR_SUCCESS == MsiDatabaseOpenView(hDatabase, _T("INSERT INTO `Binary` (`Name`, `Data`) VALUES ('NewBlob', ?)"), &hView))
    {

        //
        // Create record with binary data in 1st field (must match up with '?' in query)
        //

        hRec = MsiCreateRecord(1);
        if (ERROR_SUCCESS == MsiRecordSetStream(hRec, 1, _T("c:\\temp\\data.bin")))
        {

            //
            // Execute view with record containing binary data value; commit database to save changes
            //

            if (ERROR_SUCCESS == MsiViewExecute(hView, hRec)
              && ERROR_SUCCESS == MsiViewClose(hView)
              && ERROR_SUCCESS == MsiDatabaseCommit(hDatabase))
            {
                //
                // New binary data successfully committed to the database
                //
            }
        }
    }
}

return 0;  
}

Contoh skrip berikut menambahkan data biner ke tabel.

Dim Installer
Dim Database
Dim View
Dim Record

Set Installer = CreateObject("WindowsInstaller.Installer")

Set Record = Installer.CreateRecord(1)
Record.SetStream 1, "c:\temp\data.bin"

Set Database = Installer.OpenDatabase("c:\temp\testdb.msi", msiOpenDatabaseModeTransact)
Set View = Database.OpenView("INSERT INTO `Binary` (`Name`, `Data`) VALUES ('NewBlob', ?)")
View.Execute Record
Database.Commit ' save changes

Bekerja dengan Kueri

Sintaks SQL

Contoh Kueri Database Menggunakan SQL dan Skrip