Delphi (RAD Studio) 中的 Android 服务写入 SQLite 数据库  
官方Delphi 学习QQ群: 682628230(三千人)
频道

Delphi (RAD Studio) 中的 Android 服务写入 SQLite 数据库


解决方案:


UniDAC 组件


TUniConnection 和 TUniQuery 与 Android 服务中的 SQLite 配合良好。


添加到宿主应用程序的 Project->Deployment 我的 SQLite DB 文件,远程路径设置为“.\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;



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.046588897705078 seconds