I can't make a Database project work lazarus / 16-Databases ---------------------------------------------------------------------- panoss · 2014-10-06 12:03 I 'm trying to work with databases. I 've made a simple project, a form with 5 controls:    SQLite3Connection   SQLTransaction   SQLQuery1   DataSource   DBGrid1 But it gives me an error (exception class 'EDatabase error' with message: Database not assigned) and shows no record in the grid (the database has 1 record). What am I doing wrong? This is the code of the form: Code: [Select]unit Unit1; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, sqlite3conn, sqldb, db, BufDataset, memds, SdfData, dbf,   FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, DbCtrls, DBGrids; type   { TForm1 }   TForm1 = class(TForm)     Button1: TButton;     DataSource: TDataSource;     DBEdit1: TDBEdit;     DBGrid1: TDBGrid;     DBNavigator1: TDBNavigator;     DBText2: TDBText;     Label1: TLabel;     Label2: TLabel;     Label3: TLabel;     Label4: TLabel;     Label5: TLabel;     SQLite3Connection: TSQLite3Connection;     SQLQuery1: TSQLQuery;     SQLTransaction: TSQLTransaction;     procedure DataSourceDataChange(Sender: TObject; Field: TField);     procedure Label1Click(Sender: TObject);     procedure StartDatabase;     procedure FormCreate(Sender: TObject);   private     { private declarations }   public     { public declarations }   end; var   Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject); begin   //SQLiteLibraryName:='/usr/lib/libsqlite3.so.0';   StartDatabase; end; procedure TForm1.StartDatabase; var createTables:boolean; begin   {$IFDEF UNIX}  // Linux     {$IFNDEF DARWIN}       SQLiteLibraryName := '/usr/lib/libsqlite3.so.0';     {$ENDIF}   {$ENDIF}   {$IFDEF WINDOWS} // Windows   SQLiteLibraryName := 'sqlite3.dll';   {$ENDIF}   SQLite3Connection.DatabaseName:= 'mydatabase.db';                                   //         ShowMessage(GetAppConfigDir(false));   if not FileExists('mydatabase.db') then  // Check if config directory exists   createTables := not FileExists(SQLite3Connection.DatabaseName); // no file = create new tables   //FileCreateUTF8('mydatabase.db');                 // if not: create it   SQLite3Connection.Open;   SQLTransaction.Active:=true;                                         //ShowMessage('createTables=' + createTables.ToString;   if createTables then     begin       SQLite3Connection.ExecuteDirect('CREATE TABLE "customers"('+                     ' "id" Integer NOT NULL PRIMARY KEY AUTOINCREMENT,'+                     ' "category" DateTime,'+                     ' "selerid" Numeric,'+                     ' "name" Text,'+                     ' "shopid" Numeric,'+                     ' "phone1" Numeric,'+                     ' "phone2" Numeric,'+                     ' "NoSoutien" Numeric,'+                     ' "NoSShorts" Numeric,'+                     ' "email" Text,'+                     ' "comments" Text);');       SQLite3Connection.ExecuteDirect('CREATE INDEX "customers_name_idx" ON "customers"( "name" );');       SQLite3Connection.ExecuteDirect('CREATE UNIQUE INDEX "customers_id_idx" ON "customers"( "id" );');       SQLTransaction.Commit;     end;   //SQLQuery1.SQL.Text:= 'select * from customers';   //SQLTransaction.Active:= True;   SQLite3Connection.DatabaseName:= 'mydatabase.db';   SQLite3Connection.Connected:= True;   SQLTransaction.DataBase:= SQLite3Connection;   SQLQuery1.DataBase:= SQLite3Connection;   SQLQuery1.SQL.Text:= 'select * from customers';   SQLQuery1.Open;   DataSource.DataSet:=SQLQuery1;   DBGrid1.DataSource:=DataSource;   DBGrid1.AutoFillColumns:=true; end; procedure TForm1.Label1Click(Sender: TObject); begin end; procedure TForm1.DataSourceDataChange(Sender: TObject; Field: TField); begin end; end. ---------------------------------------------------------------------- Mike.Cornflake · 2014-10-06 12:24 I'm unfamiliar with SQLLite, so am really only just asking questions that I hope others will find useful.  I admit, the bulk of your code looks good to me... After you've run the above, does the file mydatabase.db exist?  (I'm wondering if the create part is failing) The only other thing I can think is your sequence of calls.  I would make the following change... Code: [Select]  SQLite3Connection.DatabaseName:= 'mydatabase.db';   //  SQLite3Connection.Connected:= True;   // <---   Move this line   SQLTransaction.DataBase:= SQLite3Connection;   SQLQuery1.DataBase:= SQLite3Connection;   DataSource.DataSet:=SQLQuery1;   DBGrid1.DataSource:=DataSource;   DBGrid1.AutoFillColumns:=true;   SQLite3Connection.Connected:= True;  //  <--- to here   //And move these two lines to the end   SQLQuery1.SQL.Text:= 'select * from customers';   SQLQuery1.Open; The changes above are minor only.  Moving your SQLQuery1.SQL.Text/SQLQuery1.Open certainly won't make a difference, but the .Connected := True might.  I'm wondering if perhaps one of the subsequent configuration steps in your original code might close the connection, so I've moved the .Connected := True so it's after all the configuration... ---------------------------------------------------------------------- panoss · 2014-10-06 13:41 Quote from: Mike.Cornflake on October 06, 2014, 02:24:35 pmAfter you've run the above, does the file mydatabase.db exist?  (I'm wondering if the create part is failing) The creation of the database works fine, the mydatabase.db gets created. I tried the changes you suggest but nothing changed. ---------------------------------------------------------------------- Mike.Cornflake · 2014-10-06 13:50 In which case we need to play around with your subsequent code.  I note you're calling Calling Code: [Select]SQLite3Connection.Open;   SQLTransaction.Active:=true; and then later the SQLiteConnection.Connected := True.  I think that SQLiteConnection.Open and SQLiteConnection.Connected := True; probably achieve the same thing. Which line is the Exception raised on (set a breakpoint, and step through the code)?  Did that line change with the re-ordering of code? UPDATE:  I also note a discrepancy between the code you posted and the image you posted.  No Labels or DBNavigator on the image you posted.  Not sure how that would be relevant, but just making sure the code you posted is indeed the one you're stepping through... ---------------------------------------------------------------------- panoss · 2014-10-06 14:44 The error happens on the line:  SQLTransaction.Active:=true; ---------------------------------------------------------------------- Mike.Cornflake · 2014-10-06 14:49 Well, that's before your creation code, so although the file exists I doubt it's correctly populated.  Might be worth deleting it, you never know if it's got corrupted during testing. Try replacing that line with SQLTransaction.StartTransaction (or it might be SQLTransaction.Start; - sorry I'm away from Lazarus ATM).  I've never seen someone open a Tranaction by setting Active:=True;. In about 3 hours I'll be in front of Lazarus - I'll try your code then myself and see how I go... ---------------------------------------------------------------------- BigChimp · 2014-10-06 14:51 Quote from: Mike.Cornflake on October 06, 2014, 04:49:28 pmTry replacing that line with SQLTransaction.StartTransaction Quote from: Mike.Cornflake on October 06, 2014, 04:49:28 pmI've never seen someone open a Tranaction by setting Active:=True;. ... but it does work even though StartTransaction IMO is much clearer... ---------------------------------------------------------------------- panoss · 2014-10-06 14:52 I removed the code that creates the database (and opens connection with db etc) AND IT WORKS!! Thanks a lot Mike! You really helped me to find it! (thanks to all of course) It works! It took me only ( ) 2 days!! ---------------------------------------------------------------------- Mike.Cornflake · 2014-10-06 15:03 :-)  Glad you solved it, but you did it on your own.  I honestly had no idea... ---------------------------------------------------------------------- panoss · 2014-10-06 15:15 You gave me the idea to search the error in the code execution of the form. I thought it was raised before the execution of the code started! That's why I didn't look at it earlier. ---------------------------------------------------------------------- Mike.Cornflake · 2014-10-06 15:20 Quote from: BigChimp on October 06, 2014, 04:51:34 pmQuote from: Mike.Cornflake on October 06, 2014, 04:49:28 pmTry replacing that line with SQLTransaction.StartTransaction Quote from: Mike.Cornflake on October 06, 2014, 04:49:28 pmI've never seen someone open a Tranaction by setting Active:=True;. ... but it does work even though StartTransaction IMO is much clearer... Many thanks for this info. ---------------------------------------------------------------------- BigChimp · 2014-10-07 06:59 TBH, I've just replaced some .Active:=true calls with .StartTransaction in code that I wrote when I started out with Lazarus