#1 2012-11-05 19:14:43

richardmaley
Member
Registered: 2012-11-05
Posts: 4

How to create an empty Sqlite3 DB

I am trying to create an empty Sqlite3 database by using TSQLDBSQLite3ConnectionProperties.  Not errors are thrown and no database is created.

How should I do this?

procedure TForm3.FormCreate(Sender: TObject);
Var
  aServerName: RawUTF8;
  aDatabaseName: RawUTF8;
  aUserID: RawUTF8;
  aPassWord: RawUTF8;
begin
  aServerName:=DirDB+'Sqlite3201211051120.db3';
  aDatabaseName:='';
  aUserID:='';
  aPassWord:='';
  DB:=TSQLDBSQLite3ConnectionProperties.Create(aServerName,aDatabaseName,aUserID,aPassWord);
end;

Thank you.

Richard Maley

Offline

#2 2012-11-05 19:45:13

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: How to create an empty Sqlite3 DB

There is no need to create the file explicitely.

The SQLite3 engine will create the file if not existing.
But you have to run at least one SQL statement on it.
This is the default behavior of SQLite3.

Offline

#3 2012-11-05 19:59:57

richardmaley
Member
Registered: 2012-11-05
Posts: 4

Re: How to create an empty Sqlite3 DB

Modifying my original code as follows does create the empty table.

procedure TForm3.FormCreate(Sender: TObject);
Var
  aServerName: RawUTF8;
  aDatabaseName: RawUTF8;
  aUserID: RawUTF8;
  aPassWord: RawUTF8;
  Conn: TSQLDBSQLite3Connection;
begin
  aServerName:=DirDB+'Sqlite3201211051120.db3';
  ShowMessage(aServerName);
  aDatabaseName:='';
  aUserID:='';
  aPassWord:='';
  DB:=TSQLDBSQLite3ConnectionProperties.Create(aServerName,aDatabaseName,aUserID,aPassWord);
  DB.UseMormotCollations := False;
  Conn := DB.MainConnection as TSQLDBSQLite3Connection;
  Conn.Connect;
end;

Thank you for your reply.

Richard Maley

Offline

#4 2012-11-05 21:26:29

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: How to create an empty Sqlite3 DB

You are right: Connect will create the DB if not existing.

Offline

Board footer

Powered by FluxBB