SOLVED Sqlite database export lazarus / 16-Databases ---------------------------------------------------------------------- xinyiman · 2011-01-26 08:21 How can I get a file into the sqlite database structure and related data for possible future re-importation? For example, I pippo.S3DB the database and I want to extrapolate the structure and contents to be saved in a file named poppo.sql Thanks for the help ---------------------------------------------------------------------- Lacak2 · 2011-01-26 08:40 In Windows you can use backup.bat: echo .dump | sqlite3.exe pippo.S3DB > poppo.sql ---------------------------------------------------------------------- xinyiman · 2011-01-26 09:16 One crossplatform solution and internal to lazarus, no external process ---------------------------------------------------------------------- DirkS · 2011-01-26 11:08 There is a lazdbexport component available. It should already be in the lazarus components directory (subdir dbexport). Gr. Dirk. ---------------------------------------------------------------------- xinyiman · 2011-01-26 12:17 Soved with one my unit unit ExportSqLite; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, HTTPSend, sqldb, sqlite3conn,db;   function EsportaDBSqLiteSuFile(ConnLite: TSQLite3Connection; FlagDati: boolean; NomeFile: string; var Error: string): boolean; implementation function EsportaDBSqLiteSuFile(ConnLite: TSQLite3Connection; FlagDati: boolean; NomeFile: string; var Error: string): boolean; var    ListaTabelle: TStringList;    ListaRisultato: TStringList;    MiaQuery: TSqlQuery;    ret: boolean;    i,j: integer;    Riga, Dati: string; begin      ret:=TRUE;      try        try           ListaTabelle:=TStringList.Create;           ListaRisultato:=TStringList.Create;           MiaQuery:=TSQLQuery.Create(nil);           MiaQuery.DataBase:=ConnLite.Transaction.DataBase;           ConnLite.GetTableNames(ListaTabelle,FALSE);           { STAMPO SU FILE LA STRUTTURA DELLE TABELLE }           for i:=0 to ListaTabelle.Count-1 do           begin                 MiaQuery.SQL.Text:='SELECT * FROM sqlite_master WHERE tbl_name=''' + ListaTabelle.ValueFromIndex + ''';';                 MiaQuery.Open;                 if not MiaQuery.EOF then                 begin                      MiaQuery.First;                      While Not MiaQuery.EOF do                      begin                           ListaRisultato.Add(StringReplace(MiaQuery.Fi elds.FieldByName('sql').AsString,'"','', [rfReplaceAll]) + ';');                           MiaQuery.Next;                      end;                 end;                 MiaQuery.Close;           end;           { STAMPO SU FILE I DATI DELLE TABELLE }           if FlagDati=TRUE then           begin                for i:=0 to ListaTabelle.Count-1 do                begin                     MiaQuery.SQL.Text:='SELECT * FROM ' + ListaTabelle.ValueFromIndex + ';';                     MiaQuery.Open;                     if not MiaQuery.EOF then                     begin                          MiaQuery.First;                          While Not MiaQuery.EOF do                          begin                               Riga:='INSERT INTO ' + ListaTabelle.ValueFromIndex + '(';                               Dati:='';                               for j:=0 to MiaQuery.Fields.Count-1 do                               begin                                    //stampo il nome del campo                                    Riga:=Riga + MiaQuery.Fields.Fields[j].DisplayName;                                    if (j<(MiaQuery.Fields.Count-1)) then                                       Riga:=Riga + ', '                                    else                                        Riga:=Riga + ')VALUES(';                                    if MiaQuery.Fields.Fields[j].DataType=ftInteger then                                    begin                                         //è numerico intero                                         Dati:=Dati + MiaQuery.Fields.Fields[j].AsString;                                         if (j<(MiaQuery.Fields.Count-1)) then                                            Dati:=Dati + ', '                                         else                                             Dati:=Dati + ');';                                    end                                    else                                    begin                                         if MiaQuery.Fields.Fields[j].DataType=ftString then                                            Dati:=Dati + '''' + StringReplace(MiaQuery.Fields.Fields[j].AsString,'''','''''', [rfReplaceAll]) + ''''                                         else                                             Dati:=Dati + '''' + MiaQuery.Fields.Fields[j].AsString + '''';                                         if (j<(MiaQuery.Fields.Count-1)) then                                            Dati:=Dati + ', '                                         else                                             Dati:=Dati + ');';                                    end;                               end;                               ListaRisultato.Add(Riga+Dati);                               MiaQuery.Next;                          end;                     end;                     MiaQuery.Close;                end;           end;           { METTO I DATI SU FILE }           ListaRisultato.SaveToFile(NomeFile);        finally               MiaQuery.Free;               ListaTabelle.Free;               ListaRisultato.Free;        end;      except            on E: Exception do            begin                 Error:=E.Message;                 ret:=FALSE;            end;      end;      EsportaDBSqLiteSuFile:=ret; end; end. ---------------------------------------------------------------------- fabienwang · 2011-01-26 13:47 nice xinyiman, but you don't export the sql that can recreate the sqlite db (i see no CREATE TABLE with fields). ---------------------------------------------------------------------- xinyiman · 2011-01-26 17:36 Do not worry, what you say is within the scope of the sql table sqlite_master