Tuesday, October 4, 2011

Super simple SQLite application for iOS FireMonkey

The following unit creates a table, inserts two records, and queries them back.

Next I'll try to tackle DataSnap... ;)

Enjoy!

unit Unit1;

{$IFDEF FPC}
{$mode objfpc}{$H+}
{$modeswitch objectivec1}
{$ENDIF}

interface

uses
SysUtils, Types, UITypes, Classes, Variants, FMX_Types, FMX_Controls, FMX_Forms,
FMX_Dialogs, FMX_Layouts, FMX_Memo
{$IFDEF FPC}
, iPhoneAll, SQLite3db, SQLite
{$ENDIF}
;

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.lfm}

procedure Log(Msg : String);
begin
{$IFDEF FPC}
NSLog(NSSTR(PChar(Msg)));
{$ENDIF}
end;

{$IFDEF FPC}
function MyDirectory : NSString;
var
paths : NSArray;
fileName : NSString;
begin
paths := NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, True);
fileName := paths.objectAtIndex(0);
Result := fileName;
end;
{$ENDIF}

procedure TForm1.Button1Click(Sender: TObject);
{$IFDEF FPC}
var
FileName, SQL : String;
DB : TSQLite;
SL : Classes.TStringList;
{$ENDIF}
begin
{$IFDEF FPC}
FileName := String(MyDirectory.UTF8String)+'/MyDB.sqlite';
Log(FileName);

DB := TSQLite.Create(FileName);

(*
// Drop the table and start from scratch
SQL := 'drop table Customers';
DB.Query(SQL,nil);
*)

// Create the table
SQL := 'create table Customers (Id integer not null, LastName char(25), FirstName char(25))';
DB.Query(SQL,nil);

// Insert a couple of records
SQL := 'insert into Customers values(1, "Ohlsson", "Anders")';
DB.Query(SQL,nil);
SQL := 'insert into Customers values(2, "Intersimone", "David")';
DB.Query(SQL,nil);

// Get the records back and list them in the memo
SL := Classes.TStringList.Create;
SQL := 'select * from Customers';
if DB.Query(SQL,SL) then
Memo1.Text := SL.Text;
SL.Free;

DB.Free;
{$ENDIF}
end;

end.

33 comments:

  1. Be aware that the units you are using (SQLite or SQLite3DB) are part of the standard FPC library, and will certainly not be available in the next version of Delphi, when the FPC compiler will be replaced by a pure Embarcadero's product.

    ReplyDelete
  2. I'd recommend using {$mode delphi} rather than {$mode objfpc}. ObjFPC syntax is different from Delphi syntax in a couple of situations (primarily related to procedure variables, but there are more).

    ReplyDelete
  3. @A. Bouchez: While that may be so, people need examples today. And I'm happy to tinker around and provide examples. In fact, all my latest examples will probably have to change sooner or later. They're not set in stone.

    @Jonas: I think I had to go ObjC mode for some reason, but I'll check to see if I can remove it. I may have removed code that needed it.

    ReplyDelete
  4. FPC has no problem combining Objective-Pascal with Delphi-style Pascal. {$mode objfpc} and {$mode delphi} set the basic syntax mode. {$modeswitch objectivec1} is a modeswitch rather than a full syntax mode, and as such simply adds functionality to the current syntax mode. It can even be combined with {$mode tp} if for some reason you'd like to combine Turbo Pascal-style code with Objective-Pascal :)

    So you can use:

    {$ifdef fpc}
    {$mode delphi}
    {$modeswitch objectivec1}
    {$endif}

    ({$h+} is the default in Delphi mode, just like it is in Delphi itself)

    ReplyDelete
  5. Also, you may want to use {$modeswitch objectivec2} instead of {$modeswitch objectivec1} in case you are targeting iOS and/or Mac OS X 10.5 or later (FPC still supports 10.4 as well). That allows you to use the for..in statement with Objective-C classes that support fast enumeration (http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/Chapters/ocFastEnumeration.html )

    ReplyDelete
  6. Thanks! To be honest, I just copied and pasted from an example I was given by R&D. Sometimes when I use a construct and forget about the modeswitch, Xcode will yell quite clearly and say I have to add them. I haven't double checked this example per se, but eventually I'll clean up and distribute a whole bunch of example.

    ReplyDelete
  7. Hello! I'm from Brazil and I have a doubt, the possibility of something I can connect to a mysql based online and in firemonkey ios.
    However I would like to make that connection without DataSnap.
    Please try to help me!

    thank you

    shaka41@gmail.com

    ReplyDelete
  8. Can someone tell me how to acquire the (SQLite or SQLite3DB) units used in this? I have downloaded and installed FPC but cannot determine how to get usable units from the files.

    Thanks!

    Robert

    ReplyDelete
  9. Thanks for the informative post. I was thinking about this subject just yesterday. I am wanting to learn creating an easy fast site, so like it a lot ! I will check back regularly. Keep up the good work!

    ReplyDelete
  10. stamped concrete joints...

    The Hacker’s Corner » Blog Archive » Super simple SQLite application for iOS FireMonkey...

    ReplyDelete
  11. What are the best colleges for creative writing and screenwriting?

    ReplyDelete
  12. I have been wanting to start blogging and/or vlogging for a while, and I was wondering what to do to prepare to start vlogging and what type of camera to get and just anything information you could give me on the subject. This would be a tremendous help to me. Serious answers only please. Thank you..

    ReplyDelete
  13. The Hacker’s Corner » Blog Archive » Super simple SQLite application for iOS FireMonkey
    akehvpfih
    [url=http://www.g074qk188wa1ky9ox0udzu37vl753l72s.org/]ukehvpfih[/url]
    kehvpfih http://www.g074qk188wa1ky9ox0udzu37vl753l72s.org/

    ReplyDelete
  14. Where can I find free images?. Which images are usually considered free?. When is it ok to insert a picture from a website?.

    ReplyDelete
  15. I need a computer program that will automatically start another program and not let that program close for a work computer. So basically, when the computer starts up, this program I am looking for will start a work-related program and force it to stay open, so that the only use of that computer would be to use the work program. Thank you!.

    ReplyDelete
  16. I recently started a blog, and I was just wondering how people have promoted their blogs online to get more followers so you aren't just typing to nothing out in the internet?. Oh, and for my blog, I don't really want my friends to be my followers, unless they find it on their own. My blog is about something that most of my friend's can't really help me with. (Photography is the main subject).

    ReplyDelete
  17. Does everyone like blogspot or is there a better way to go?

    ReplyDelete
  18. The Hacker’s Corner » Blog Archive » Super simple SQLite application for iOS FireMonkey
    [url=http://www.g08vs4f131tyh3a3na5czp4226e0n43zs.org/]uvsbltiterv[/url]
    vsbltiterv http://www.g08vs4f131tyh3a3na5czp4226e0n43zs.org/
    avsbltiterv

    ReplyDelete
  19. you are in reality a excellent webmaster. The website loading speed is amazing. It seems that you are doing any unique trick. Also, The contents are masterpiece. you have performed a wonderful task on this matter!|

    ReplyDelete
  20. Hello just wanted to give you a quick heads up and let you know a few of the pictures aren't loading correctly. I'm not sure why but I think its a linking issue. I've tried it in two different web browsers and both show the same results.|

    ReplyDelete
  21. It's awesome designed for me to have a web page, which is valuable in support of my know-how.
    thanks admin

    ReplyDelete
  22. When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get four e-mails with the same comment. Is there any way you can remove people from that service? Many thanks!|

    ReplyDelete
  23. I think that everything posted made a great deal of sense. But, what about this? what if you were to create a killer headline? I ain't suggesting your content is not solid., but suppose you added something to possibly get a person's attention? I mean %BLOG_TITLE% is kinda plain. You might peek at Yahoo's front page and see how they create article headlines to grab people to click. You might add a video or a picture or two to get people interested about what you've got to say. In my opinion, it would bring your website a little livelier.|

    ReplyDelete
  24. Appreciating the hard work you put into your blog and detailed information you present. It's great to come across a blog every once in a while that isn't the same unwanted rehashed information. Wonderful read! I've bookmarked your site and I'm including your RSS feeds to my Google account.|

    ReplyDelete
  25. Hello exceptional website! Does running a blog such as this take a massive amount work? I've no knowledge of computer programming but I had been hoping to start my own blog soon. Anyhow, should you have any recommendations or techniques for new blog owners please share. I know this is off topic nevertheless I just had to ask. Thanks!|

    ReplyDelete
  26. Supercharge your marketing campaign with our social media promotion services. Buy high quality likes/followers/views at highly competitive prices and with fast delivery.

    Get 5,000 Facebook Fans/Likes from:- http://khalaghor.com/


    You can also order smaller or bigger package from our official website.

    ReplyDelete
  27. I just could not go away your web site prior to suggesting that I actually loved the usual information a person supply on your guests? Is going to be again regularly to check out new posts|

    ReplyDelete
  28. I simply couldn't depart your site prior to suggesting that I extremely loved the usual info a person provide on your guests?
    Is gonna be back continuously in order to investigate cross-check new posts

    Here is my homepage porn

    ReplyDelete
  29. Wonderful blog! Do you have any hints for aspiring writers?
    I'm planning to start my own site soon but I'm a little lost on everything.
    Would you propose starting with a free platform like
    Wordpress or go for a paid option? There are so many choices out there that I'm totally confused ..

    Any recommendations? Thank you!

    Also visit my web-site; iron knights Hack

    ReplyDelete
  30. Spot on with this write-up, I honestly think this amazing site needs far more attention. I'll probably be returning to
    read more, thanks for the information!

    Look at my web-site ... Line Ranger Hack

    ReplyDelete
  31. According to a recent blog post by Brown, "Drivers, both professional and amateur, who have driven this awesome CSR-69 concept car, agree it is arguably the best handling Mustang they have ever driven. By dumping your man, you've made a wise and powerful decision. Qualifying for the Honda Indy Grand Prix of Alabama takes place Saturday, April 26 at 2 p.

    Here is my blog post; cheats for Csr racing android

    ReplyDelete
  32. It's remarkable to pay a quick visit this web page and reading the views of all friends about this
    article, while I am also eager of getting experience.

    ReplyDelete