-
Notifications
You must be signed in to change notification settings - Fork 5
TSQLite3Insert
Ivan Semenkov edited this page Feb 4, 2021
·
2 revisions
TSQLite3Insert class encapsulates the SQLite3 database insert query.
uses
sqlite3.insert;
type
TSQLite3Insert = class
A new insert query can be created by call its constructor.
constructor Create (AErrorsStack : PSQL3LiteErrorsStack; ADBHandle :
ppsqlite3; ATableName : String);
uses
libpassqlite, sqlite3.errors_stack, sqlite3.connection, sqlite3.insert;
var
errors : TSQLite3ErrorsStack;
handle : psqlite3;
connection : TSQLite3DatabaseConnection;
insert : TSQLite3Insert
begin
errors := TSQLite3ErrorsStack.Create;
connection := TSQLite3DatabaseConnection.Create(@errors, @handle, 'database',
[SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
insert := SQLite3Insert.Create(@errors, @handle, 'table_name');
FreeAndNil(insert);
FreeAndNil(connection);
FreeAndNil(errors);
end;
Or use TSQLite3Builder object.
function Insert : TSQLite3Insert;
uses
sqlite3.builder, sqlite3.table, sqlite3.insert;
var
builder : TSQLite3Builder;
insert : TSQLite3Insert;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
insert := builder.Table('table_name').Insert;
FreeAndNil(builder);
end;
There are two ways to insert values into table.
Set the value of the insertion row column.
function Value (AColumnName : String; AValue : Integer) : TSQLite3Insert;
function Value (AColumnName : String; AValue : Double) : TSQLite3Insert;
function Value (AColumnName : String; AValue : String) : TSQLite3Insert;
function Value (AColumnName : String; AValue : TStream) : TSQLite3Insert;
function Value (AColumnName : String; AValue : TMemoryBuffer) :
TSQLite3Insert;
uses
sqlite3.builder, sqlite3.table, sqlite3.insert;
var
builder : TSQLite3Builder;
insert : TSQLite3Insert;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
insert := builder.Table('table_name').Insert
.Value('value', 23);
FreeAndNil(builder);
end;
Set the null value of the insertion row column.
function ValueNull (AColumnName : String) : TSQLite3Insert;
uses
sqlite3.builder, sqlite3.table, sqlite3.insert;
var
builder : TSQLite3Builder;
insert : TSQLite3Insert;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
insert := builder.Table('table_name').Insert
.ValueNull('value');
FreeAndNil(builder);
end;
It is very important set a correct value types. It is raising TException('Mistmatch column type.') if column type is not equal value type.
Set multiple insert column name.
function Column (AColumnName : String; AColumnType : TDataType) :
TSQLite3Insert;
AColumnType
is one of fundamental database column data types.
TDataType = (
SQLITE_INTEGER,
SQLITE_FLOAT,
SQLITE_BLOB,
SQLITE_TEXT
);
uses
sqlite3.builder, sqlite3.table, sqlite3.insert;
var
builder : TSQLite3Builder;
insert : TSQLite3Insert;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
insert := builder.Table('table_name').Insert
.Column('val_1', SQLITE_INTEGER)
.Column('val_2', SQLITE_TEXT);
FreeAndNil(builder);
end;
Start new insert row.
function Row : TSQLite3Insert;
uses
sqlite3.builder, sqlite3.table, sqlite3.insert;
var
builder : TSQLite3Builder;
insert : TSQLite3Insert;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
insert := builder.Table('table_name').Insert
.Column('val_1', SQLITE_INTEGER)
.Column('val_2', SQLITE_TEXT)
.Row;
FreeAndNil(builder);
end;
Add values to insert row.
function Value (AValue : Integer) : TSQLite3Insert;
function Value (AValue : Double) : TSQLite3Insert;
function Value (AValue : String) : TSQLite3Insert;
function Value (AValue : TStream) : TSQLite3Insert;
function Value (AValue : TMemoryBuffer) : TSQLite3Insert;
uses
sqlite3.builder, sqlite3.table, sqlite3.insert;
var
builder : TSQLite3Builder;
insert : TSQLite3Insert;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
insert := builder.Table('table_name').Insert
.Column('val_1', SQLITE_INTEGER)
.Column('val_2', SQLITE_TEXT)
.Row
.Value(12)
.Value('some text')
.Row
.Value(-3)
.Value('another text');
FreeAndNil(builder);
end;
Add null value to insert row.
function ValueNull : TSQLite3Insert;
uses
sqlite3.builder, sqlite3.table, sqlite3.insert;
var
builder : TSQLite3Builder;
insert : TSQLite3Insert;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
insert := builder.Table('table_name').Insert
.Column('val_1', SQLITE_INTEGER)
.Column('val_2', SQLITE_TEXT)
.Row
.Value(12)
.Value('some text')
.Row
.Value(-3)
.ValueNull;
FreeAndNil(builder);
end;
Run query and return number of inserted columns.
function Get : Integer;
uses
sqlite3.builder, sqlite3.table, sqlite3.insert;
var
builder : TSQLite3Builder;
begin
builder := TSQLite3Builder.Create('database', [SQLITE_OPEN_CREATE, SQLITE_OPEN_READWRITE]);
builder.Table('table_name').Insert
.Column('val_1', SQLITE_INTEGER)
.Column('val_2', SQLITE_TEXT)
.Row
.Value(12)
.Value('some text')
.Row
.Value(-3)
.ValueNull
.Get;
FreeAndNil(builder);
end;