Create table from FieldDef's (SQLDB) lazarus / 16-Databases ---------------------------------------------------------------------- fpcoder · 2017-05-04 06:34 Is there a way for SQLDB to create a database table from the TSQLQuery's FieldDefs ? If I remember right, in Delphi you could call Dataset.CreateTable and a table with the designed structure will result from the FieldDef items? ---------------------------------------------------------------------- GAN · 2017-05-04 07:35 In SQLite I use Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i CREATE TABLE new_table AS  SELECT expressions  FROM existing_tables  [WHERE conditions]; You could create and then delete and have the same result (I think). Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i CREATE TABLE table_2 ASSELECT * FROM table_1LIMIT 1; DELETE FROM table_2; ---------------------------------------------------------------------- Thaddy · 2017-05-04 08:04 Since it descents from TCustomBufDataSet you can call CreateDataset after you have setup the fielddefs. ---------------------------------------------------------------------- fpcoder · 2017-05-04 10:47 @GAN Thanks for the reply. However, there is no table to select from yet. I want to create the table from scratch first time the application runs. At the moment I use a TSQLScript to create the application tables. But it needs to load some text files with the SQL commands in. So I thought why not replace that and do it in a more RAD way: Add table definitions to FieldDefs at design time and then just call CreateTable at runtime. But it seems that similar to CreateTable is not implemented in SQLDB. @Thaddy CreateDataset doesn't create tables, does it? It creates the TField objects from available FieldDefs. I actually just wanted a more RAD way of creating tables than running a sqlscript. Maybe I should try implement CreateTables. But I don't see where NOT NULL can be specified in the FieldDefs. ---------------------------------------------------------------------- Thaddy · 2017-05-04 11:23 Yes, it DOES create the table (actually it can only create one table at a time, so it is a bit misleadingly named) ... in memory... so you will have to persist it. It does not create the table on storage (either on disk or in an actual database).  But that can be solved by either saving to file or assigning to another dataset. ---------------------------------------------------------------------- sky_khan · 2017-05-04 12:42 Mapping of lazarus field datatype to SQL column datatype would be database specific. Some of databases, especially old ones might not support all types. Still it is not hard building "create table" query if you know which database you will use. Btw, I think you can use faRequired attribute in TFieldDef.Attributes for "not null"