Create Database + Table From Scratch (Programatically)

Forum related to SQLite

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
kwanbis
Fresh Boarder
Fresh Boarder
Posts: 6
Joined: 04.09.2006, 20:26

Create Database + Table From Scratch (Programatically)

Post by kwanbis »

I'm making a very simple "portable" (and open source), application.

I have not programed in Delphi DB in the last 8 years, so i don't remember much.

The good thing is that now there is "the internets", but i can not find some answers.

I want my application to start, and if it does not finds a database, create it and create the tables.

So far, i have:

Code: Select all

  ZConnection.Disconnect;
  ZConnection.Protocol := 'sqlite-3';
  ZConnection.Database := ExtractFilePath(Application.ExeName) + '/database.SQLite3';
  if not FileExists(ZConnection.database) then
  begin
    ZConnection.Properties.Add('createnewdatabase=create database '''+ZConnection.Database+''' user ''sysdba'' password ''masterkey'' page_size 4096 default character set iso8859_2;');
    try
      ZConnection.Connect;
    except
      ShowMessage('no pude conectar (y no existia!)');
      application.Terminate;
    end;
  end else
  begin
    try
      ZConnection.connect;
    except
      ShowMessage('no pude conectar (y SI existia!)');
      application.Terminate;
    end;
  end;
But, how do i create a simple table, that has 1 text field, 1 data field, and one memo field, indexed by the 1st text field?

Thanks.
Wild_Pointer
Expert Boarder
Expert Boarder
Posts: 164
Joined: 18.03.2008, 13:03
Contact:

Post by Wild_Pointer »

kwanbis,
But, how do i create a simple table, that has 1 text field...
I would recommend using ZConnection.ExecuteDirect('CREATE TABLE FOO( ....'


Good luck!
kwanbis
Fresh Boarder
Fresh Boarder
Posts: 6
Joined: 04.09.2006, 20:26

Post by kwanbis »

Now, that worked. I have to get used to make it using SQL commands.

ZConnection.ExecuteDirect('CREATE TABLE NOTES (noteTitle TEXT PRIMARY KEY,noteDate DATE,noteNote TEXT)');

The problem i have now, is that the only way of displaying the TEXT fields, is under a DBmemo.

How can i display the TITLE field on a DBLitBox or a DBgrid?
kwanbis
Fresh Boarder
Fresh Boarder
Posts: 6
Joined: 04.09.2006, 20:26

Post by kwanbis »

If i create the TEXT field with noteTitle CHAR(255) i can see it on a dbgrid.

Now, i can not show anything, not even the date field, on the dblistbox.

Has anyone done that?
User avatar
Pitfiend
Senior Boarder
Senior Boarder
Posts: 68
Joined: 12.12.2009, 07:27

Post by Pitfiend »

get a look in this post http://zeos.firmos.at/viewtopic.php?t=2627 you also need to know that not all datatypes can be used in sqlite.
kwanbis
Fresh Boarder
Fresh Boarder
Posts: 6
Joined: 04.09.2006, 20:26

Post by kwanbis »

Thanks for your reply. But the post you link to, has no reference to my current problem:

If i create the TEXT field with noteTitle CHAR(255) i can see it on a dbgrid.

Now, i can not show anything, not even the date field, on the dblistbox.

Has anyone done that?
User avatar
Pitfiend
Senior Boarder
Senior Boarder
Posts: 68
Joined: 12.12.2009, 07:27

Post by Pitfiend »

are you aware of the dynamic typing of sqlite? that means you can store anything inside any field. also, there are some restrictions related to primary keys. you need to be aware that there are 4 generic datatypes only and a NULL value. more info here.
Post Reply