从 Delphi 中的 Android 服务写入 SQLite 数据库 (RAD Studio)
Write into the SQLite database from an Android service in Delphi (RAD Studio)
我在同一进程中有一个 Android 应用程序和服务。在RAD Studio Delphi 10.1.
下写代码
我需要在 Android 服务中获取地理坐标(我做得很好)并将它们写入 SQLite 数据库。
并且应用程序可以不时地(当用户需要时)处理用户界面中的坐标。
当我将 TConnection(任何 - ADO、FireDAC、UniDAC)放入 DataModule 时甚至没有建立活动连接,服务停止工作,甚至没有 运行 OnStartCommand 事件。
Monitor.bat 没有明显错误。
请告诉我如何在 Android 服务及其 Android 应用程序中同时使用 SQLite 数据库。
我找到了解决方案:
我将柏林的 UniDAC 组件更新到最新版本 (6.3.12)。
TUniConnection 和 TUniQuery 在 Android 服务中与 SQLite 配合得很好。
添加到项目->部署主机应用程序我的 SQLite 数据库文件,远程路径设置为“.\assets\internal\”。
希望这段代码对你有用。
procedure TDM.conSQLiteBeforeConnect(Sender: TObject);
begin
{$IF DEFINED(iOS) or DEFINED(ANDROID)}
conSQLite.Database := TPath.Combine(TPath.GetDocumentsPath, 'mybase.sqlite');
{$ENDIF}
end;
procedure TDM.conSQLiteError(Sender: TObject; E: EDAError; var Fail: Boolean);
begin
Log('--- DB error: %s:', [E.Message]);
Fail := False;
end;
function TDM.AndroidServiceStartCommand(const Sender: TObject; const Intent: JIntent; Flags, StartId: Integer): Integer;
begin
Log('+ START with Intent: ' + JStringToString(Intent.getAction.toString), []);
if Intent.getAction.equalsIgnoreCase(StringToJString('StopIntent')) then
begin
try
conSQLite.Disconnect;
Log('- DB disconnected', []);
except
on E: Exception do
Log('- can not to disconnect DB', [E.Message]);
end;
Log('... service to be stoped', []);
JavaService.stopSelf;
Result := TJService.JavaClass.START_NOT_STICKY; // don't reload service
end
else
begin
Log('... service started', []);
try
conSQLite.Connect;
Log('+ DB connected', []);
UniQuery.SQL.Text := 'select count(*) as ALLREC from orders';
UniQuery.Open;
if UniQuery.RecordCount > 0 then
begin
UniQuery.First;
Log('... record count: %s', [UniQuery.FieldByName('ALLREC').AsString]);
end;
UniQuery.Close;
except
on E: Exception do
Log('- can not to connect DB: %s', [E.Message]);
end;
Result := TJService.JavaClass.START_STICKY; // rerun service if it stops
end;
end;
我遇到了同样的问题,但我开始将 TSQLConnection (dbexpress) 组件与 TSQLQuery (dbexpress) 组件一起使用,这些组件在 Android 服务中运行良好。
我在同一进程中有一个 Android 应用程序和服务。在RAD Studio Delphi 10.1.
下写代码我需要在 Android 服务中获取地理坐标(我做得很好)并将它们写入 SQLite 数据库。 并且应用程序可以不时地(当用户需要时)处理用户界面中的坐标。
当我将 TConnection(任何 - ADO、FireDAC、UniDAC)放入 DataModule 时甚至没有建立活动连接,服务停止工作,甚至没有 运行 OnStartCommand 事件。
Monitor.bat 没有明显错误。
请告诉我如何在 Android 服务及其 Android 应用程序中同时使用 SQLite 数据库。
我找到了解决方案:
我将柏林的 UniDAC 组件更新到最新版本 (6.3.12)。
TUniConnection 和 TUniQuery 在 Android 服务中与 SQLite 配合得很好。
添加到项目->部署主机应用程序我的 SQLite 数据库文件,远程路径设置为“.\assets\internal\”。
希望这段代码对你有用。
procedure TDM.conSQLiteBeforeConnect(Sender: TObject);
begin
{$IF DEFINED(iOS) or DEFINED(ANDROID)}
conSQLite.Database := TPath.Combine(TPath.GetDocumentsPath, 'mybase.sqlite');
{$ENDIF}
end;
procedure TDM.conSQLiteError(Sender: TObject; E: EDAError; var Fail: Boolean);
begin
Log('--- DB error: %s:', [E.Message]);
Fail := False;
end;
function TDM.AndroidServiceStartCommand(const Sender: TObject; const Intent: JIntent; Flags, StartId: Integer): Integer;
begin
Log('+ START with Intent: ' + JStringToString(Intent.getAction.toString), []);
if Intent.getAction.equalsIgnoreCase(StringToJString('StopIntent')) then
begin
try
conSQLite.Disconnect;
Log('- DB disconnected', []);
except
on E: Exception do
Log('- can not to disconnect DB', [E.Message]);
end;
Log('... service to be stoped', []);
JavaService.stopSelf;
Result := TJService.JavaClass.START_NOT_STICKY; // don't reload service
end
else
begin
Log('... service started', []);
try
conSQLite.Connect;
Log('+ DB connected', []);
UniQuery.SQL.Text := 'select count(*) as ALLREC from orders';
UniQuery.Open;
if UniQuery.RecordCount > 0 then
begin
UniQuery.First;
Log('... record count: %s', [UniQuery.FieldByName('ALLREC').AsString]);
end;
UniQuery.Close;
except
on E: Exception do
Log('- can not to connect DB: %s', [E.Message]);
end;
Result := TJService.JavaClass.START_STICKY; // rerun service if it stops
end;
end;
我遇到了同样的问题,但我开始将 TSQLConnection (dbexpress) 组件与 TSQLQuery (dbexpress) 组件一起使用,这些组件在 Android 服务中运行良好。