0

I am trying to query one of the SQLite 3 tables from Delphi. (My databse is world and I ahve a table called City in it). My code is:

procedure TForm1.Button1Click(Sender: TObject);
begin
// Set the path of your database file.
  // Replace "full_path_to_your_database_file" with the absolute path
  // to your SQLite database file.
  SQLConnection1.Params.Add('World.db3');
  try
    // Establish the connection.
    SQLConnection1.Connected := true;
    Button1.Enabled := true;
    Memo1.Text := 'Connection established!';
  except
    on E: EDatabaseError do
      ShowMessage('Exception raised with message' + E.Message);
  end;
end;

procedure TForm1.ShowSelectResults(results: TDataSet);
var
  names: TStringList;
  i: Integer;
  currentField: TField;
  currentLine: string;
begin
  if not results.IsEmpty then
  begin
    results.First;
    names := TStringList.Create;
    results.GetFieldNames(names);
    while not results.Eof do
    begin
      currentLine := '';
      for i := 0 to names.Count - 1 do
      begin
        currentField := results.FieldByName(names[i]);
        currentLine := currentLine + ' ' + currentField.AsString;
      end;
      memo1.Lines.Add(currentLine);
      results.Next;
    end;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  results: TDataSet;
  query: String;
begin
  Memo1.Clear;
  // A random query
  query := 'SELECT * FROM City;';

  try
  // Execute the query on the database.
    SQLConnection1.Execute(query, nil, results);
  except
    on E: Exception do
      Memo1.Text := 'Exception raised with message: ' + E.Message;
  end;
  // Show the results of the query in a TMemo control.
  ShowSelectResults(results);
end;

When I compile and execute this code, it is getting connected to the database; but throws this error. "Exception raised with message: no such table: City" I have spent hours and hours to figure out why I am hitting on this error. Tried out numerous versions of this code too. Doesn't seem to work either. Any help in this regard is highly appreciated.

1 Answer 1

3

When SQLite cannot find a database file, it is happy to create and open a new one.
You should always use an absolute path for your database file.

Furthermore, you forgot to include the parameter name of the connection parameter:

SQLConnection1.Params.Add('Database=' + ExtractFilePath(ParamStr(0)) + 'World.db3');
Sign up to request clarification or add additional context in comments.

8 Comments

is "the same directory" the current directory of your application at runtime?
As the path to the database contains spaces it might help to surround it with double quotes when you add it to the SQLConnection Params. IE 'Database="' + ExtractFilePath(ParamStr(0)) + 'World.db3"'
@MarjanVenema won't it then create another database file, where it is split by space ? And topicstarter told there are no alien database files... Anyway when in doubt if the correct file was open, the answer is always the same: run SysInternals Process Monitor and set its filters to track file I/O of your applications and get list of actual operations when connecting to database. PS. Using DBXpress for embedded databases like SQLite and Firebird Embedded/Interbase ToGo seems quite strange choice to me...
@Arioch'The: That is what I would expect and what would explain the table not being found. And Process Monitor is indeed your friend!
@MarjanVenema There may be possibility that his database designer ide uses wrong file. And i hope "Rad studio\projects\Win32\debug " is in Documents, not in Program Files
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.