我正在尝试从 Delphi 查询其中一张 SQLite 3 表。(我的数据库是世界,我在其中有一个名为 City 的表)。我的代码是:
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;
当我编译并执行这段代码时,它正在连接到数据库;但抛出此错误。“消息引发异常:没有这样的表:城市” 我花了好几个小时来弄清楚为什么我会遇到这个错误。也尝试了该代码的多个版本。似乎也不起作用。非常感谢这方面的任何帮助。