我的代码引发了异常:
没有这样的表:Table_Name
我花了一整天时间试图解决它,但到目前为止没有成功。
我使用SQLite3.exe
创建数据库,创建表person
并插入数据。当我查询表person
的内容时,它会向我显示正面结果。
不幸的是,当我使用此代码显示表内容时,会引发异常。
这是代码:
procedure TForm1.connectButtonClick(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('Database=D:\testdb.db');
try
// Establish the connection.
SQLConnection1.Connected := true;
executeButton.Enabled := true;
outputMemo.Text := 'Connection established!';
except
on E: EDatabaseError do
ShowMessage('Exception raised with message' + E.Message);
end;
end;
procedure TForm1.executeButtonClick(Sender: TObject);
var
results: TDataSet;
query: String;
begin
outputMemo.Clear;
// A random query
query := 'SELECT * FROM person;';
try
// Execute the query on the database.
SQLConnection1.Execute(query, nil, results);
except
on E: Exception do
outputMemo.Text := 'Exception raised with message: ' + E.Message;
end;
// Show the results of the query in a TMemo control.
ShowSelectResults(results);
end;
procedure TForm3.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;
outputMemo.Lines.Add(currentLine);
results.Next;
end;
end;
end;
答案 0 :(得分:1)
您的数据库和/或表是否已损坏,或者您的代码的某些部分出现了错误,但是您没有向我们展示。
以下代码创建并填充新表,在Delphi Seattle上完美运行并生成预期结果,即outputMemo
中的一个数据行。
我建议你关闭&在尝试之前,重新启动Delphi并关闭可能使用与DB相同的Sqlite3.Dll的任何其他应用程序。
(我已将outputMemo
和ShowSelectResults
移至Form1)
procedure TForm1.executeButtonClick(Sender: TObject);
var
results: TDataSet;
query: String;
begin
outputMemo.Clear;
query := 'CREATE TABLE TESTTABLE (ID BIGINT, NAME NVARCHAR(80) )';
SQLConnection1.Execute(query, nil, Nil);
query := 'INSERT INTO TESTTABLE(ID, NAME) VALUES(1, ''One'')';
SQLConnection1.Execute(query, nil, Nil);
query := 'SELECT * FROM TESTTABLE';
try
// Execute the query on the database.
SQLConnection1.Execute(query, nil, results);
except
on E: Exception do
outputMemo.Text := 'Exception raised with message: ' + E.Message;
end;
// Show the results of the query in a TMemo control.
ShowSelectResults(results);
query := 'DROP TABLE TESTTABLE';
SQLConnection1.Execute(query, nil, Nil);
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;
try
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;
outputMemo.Lines.Add(currentLine);
results.Next;
end;
finally
names.Free;
end;
end;
end;