creating a SQLite database with UniDAC

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
ewong
Posts: 21
Joined: Thu 20 Jan 2011 01:22

creating a SQLite database with UniDAC

Post by ewong » Tue 06 Dec 2016 04:54

Hi,

How would I create a SQLite database with UniDAC programmatically?

I searched for some answers and tried the following:
(this is in a form_oncreate event handler)

sql_cmd := 'create database "j:\test.db"';
uniscript1.sql.add(sql_cmd);
uniscript1.noPreconnect := True;
uniscript1.execute;

But when I run it, I get "unable to open database file." when it hits
the last line.

So obviously that doesn't work.

Any help appreciated.

Edmund

ertank
Posts: 172
Joined: Wed 13 Jan 2016 16:00

Re: creating a SQLite database with UniDAC

Post by ertank » Tue 06 Dec 2016 20:55

Hello,

Below is my own solution. You may have an idea and adapt to your needs.

Code: Select all

uses
  Uni,
  NetEncoding;

procedure SetDatabaseParams(const DB: TUniConnection; const DBName, DllName: string);
begin
  DB.Close();
  DB.ProviderName := 'SQLite';
  DB.Database := DBName;
  // If you prefer to use a SQLite.DLL you need below code.
  // Can be safely assigned EmptyStr to the parameter
  DB.SpecificOptions.Values['ClientLibrary']       := DllName;
  DB.SpecificOptions.Values['ForceCreateDatabase'] := 'True';
  DB.SpecificOptions.Values['Direct'] := 'True';
  // Create Encrypted Database
  DB.SpecificOptions.Values['EncryptionAlgorithm'] := 'leAES256';
  DB.SpecificOptions.Values['EncryptionKey'] := TNetEncoding.Base64.EncodeBytesToString(TBytes(KeyDB));
end;
KeyDB is something of my own encryption string for database.

Usage is something like:

Code: Select all

  SetDatabaseParams(uncURUN,    AppPath + URUNDBNAME,    EmptyStr);
  uncURUN.Open();
Above code will create a file on the disc if it is not exists. If it is exists it will open it.

MaximG
Devart Team
Posts: 1822
Joined: Mon 06 Jul 2015 11:34

Re: creating a SQLite database with UniDAC

Post by MaximG » Wed 07 Dec 2016 14:13

SQLite does not support the CREATE DATABASE command: http://www.sqlite.org/lang.html . To execute queries to SQLite using the TUniScript component at first you should connect to an existing database or create a new one using the TUniConnection component. UniDAC when working with SQLite will create a DB file automatically when setting the 'ForceCreateDatabase' option of the TUniConnection component to TRUE :
...
UniConnection.Database := < your database file >;
UniConnection.SpecificOptions.Values['ForceCreateDatabase'] := 'True';
UniConnection.Connect;
...

This will create a database file, the name and full path to which is specified in the UniConnection.Database property.

Detailed information about using SQLite with UniDAC components you can find via the link : https://www.devart.com/unidac/docs/?sql ... rticle.htm

Post Reply