Page 1 of 1
creating a SQLite database with UniDAC
Posted: Tue 06 Dec 2016 04:54
by ewong
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
Re: creating a SQLite database with UniDAC
Posted: Tue 06 Dec 2016 20:55
by ertank
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.
Re: creating a SQLite database with UniDAC
Posted: Wed 07 Dec 2016 14:13
by MaximG
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