Free Pascal이나 Lazarus에 기본으로 있는 sqlite 관련 컴포넌트를 리눅스 환경에서 사용할 경우 Close나 Free를 호출할 경우 알 수 없는 에러가 발생하더군요.
그래서 sqlite 라이브러리를 직접 호출하는 방법을 사용했습니다.
아래는 소스 코드입니다.
uses sqlite3;procedure TAlertD.InsertMessageToDB(email, contents, ip: string);
var
rc: longint;
db: Psqlite3;
query: string;
stmt: Psqlite3_stmt;
idx0, idx1, idx2, idx3: Integer;
begin
rc := sqlite3_open(PChar(ExpandFileName(ChangeFileExt(ParamStr(0), ‘.db3’))), @db);
if (rc <> SQLITE_OK) then
begin
WriteLn(‘Can”t open database’);
sqlite3_close(db);
Exit;
end;rc := sqlite3_exec(db, ‘BEGIN’, nil, nil, nil);
if rc <> SQLITE_OK then
begin
WriteLn(‘Can”t begin Transaction: ‘, sqlite3_errmsg(db));
sqlite3_close(db);
Exit;
end;query := ‘ INSERT INTO message(email, contents, sender_ip, reg_date) VALUES(:email, :contents, :ip, :reg_date) ‘;
rc := sqlite3_prepare(db, PChar(query), Length(query), @stmt, nil);
if rc = SQLITE_OK then
begin
idx0 := sqlite3_bind_parameter_index(stmt, ‘:email’);
idx1 := sqlite3_bind_parameter_index(stmt, ‘:contents’);
idx2 := sqlite3_bind_parameter_index(stmt, ‘:ip’);
idx3 := sqlite3_bind_parameter_index(stmt, ‘:reg_date’);sqlite3_bind_text(stmt, idx0, PChar(email), Length(email), SQLITE_STATIC);
sqlite3_bind_text(stmt, idx1, PChar(contents), Length(contents), SQLITE_STATIC);
sqlite3_bind_text(stmt, idx2, PChar(ip), Length(ip), SQLITE_STATIC);
sqlite3_bind_text(stmt, idx3, PChar(FormatDateTime(‘yyyy-mm-dd hh:nn:ss’, now)), Length(FormatDateTime(‘yyyy-mm-dd hh:nn:ss’, now)), SQLITE_STATIC);rc := sqlite3_step(stmt);
if rc <> SQLITE_DONE then
begin
WriteLn(‘sqlite3_step error: ‘, sqlite3_errmsg(db));
sqlite3_close(db);
Exit;
end;rc := sqlite3_exec(db, ‘COMMIT’, nil, nil, nil);
if rc <> SQLITE_OK then
begin
WriteLn(‘Commit error: ‘, sqlite3_errmsg(db));
sqlite3_close(db);
Exit;
end;
end
else
begin
WriteLn(‘SQL error: ‘, sqlite3_errmsg(db));
end;sqlite3_finalize(stmt);
sqlite3_close(db);
end;