出现SQLite异常并显示消息:没有这样的表
我创建了一个从计算机收集信息(计算机名称,CPU,内存等)的应用程序,但是我很难显示存储在SQLite数据库中的信息,当我执行查询时,我得到否当我确定知道我有一个称为硬件的表时,出现 SUCH TABLE消息?
I created an application that collects information from the computer (computer name, CPU, Memory, etc) but I am having such a hard time displaying the information stored in the SQLite database, when I execute a query I get the "NO SUCH TABLE" message when I know for sure I have a table called "hardware" what am I doing wrong?
这里是我用来执行查询的代码:
Here the code I use to execute the query:
procedure TMain.executeButtonClick(Sender: TObject);
var
results: TDataSet;
query: String;
begin
outputMemo.ClearSelection;
query := 'SELECT * FROM hardware;';
try
SQLConnection1.Execute(query, nil, results);
except
on E: Exception do
outputMemo.Text := 'Exception raised with message: ' + E.Message;
end;
ShowSelectResults(results);
end;
每次运行应用程序时,都会以编程方式创建数据库文件和表
The database file and table are created programmatically created every time the application runs
procedure CheckForDatabase;
var
sldb: TSQLiteDatabase;
sSQL: string;
begin
slDBPath := ExtractFilePath(paramstr(0)) + 'ComputerName.db';
// ShowMessage(slDBPath);
sldb := TSQLiteDatabase.Create(slDBPath);
try
if sldb.TableExists('hardware') then
begin
sSQL := 'DROP TABLE hardware';
sldb.ExecSQL(sSQL);
end;
sSQL := 'CREATE TABLE hardware (id INTEGER PRIMARY KEY, compname TEXT, username TEXT, model TEXT, manufacturer TEXT, domain TEXT, ip TEXT, serialnumber TEXT)';
sldb.ExecSQL(sSQL);
sldb.ExecSQL('CREATE INDEX sHardware ON hardware(CompName);');
sldb.BeginTransaction;
sSQL := 'INSERT INTO hardware(id, compname, username, model, manufacturer, domain, ip, serialnumber) VALUES (1, "AMD8537", "OMonge", "Gigabyte", "Gigabyte", "Workgroup", "192.168.1.11", "8746652");';
sldb.ExecSQL(sSQL);
sldb.Commit;
finally
sldb.Free;
end;
end;
这是SQLite数据库:
And this is the SQLite database:
可以提供任何帮助提供给我将不胜感激。谢谢。
Any help you can provide me with will be greatly appreciated. Thank you.
您标记了Delphi-XE2,所以我的答案属于XE2
You Taged Delphi-XE2 so my answer belongs to XE2
从RAD Studio XE3版本开始,TSQLMonitor支持SQLite数据库。 ...
From version RAD Studio XE3 TSQLMonitor supports SQLite databases. ...
使用XE2不能将 SQLConnection1:TSQLConnection;
与驱动程序 Sqlite一起使用
!
将不起作用 SQLConnection1.Execute(query,nil,results);
With XE2 You can not use SQLConnection1: TSQLConnection;
with Driver Sqlite
!
Will not work SQLConnection1.Execute(query, nil, results);
根据所使用的代码,它似乎是用于Sqlite 3的
简单Delphi包装器。$ c>起作用。
According to the code used , it seems to be the simple Delphi wrapper for Sqlite 3
to act.
因此您可以使用刚刚创建的 sldb
。
So you can use the just created sldb
.
slDBPath := ExtractFilePath(paramstr(0)) + 'ComputerName.db';
sldb := TSQLiteDatabase.Create(slDBPath);
在程序CheckForDatabase中;
删除
var
sldb: TSQLiteDatabase;
并将其放置到应用程序的界面
and put it to the interface of your Application
private
{ Private declarations }
sltb: TSQLIteTable;
sldb: TSQLiteDatabase;
还删除 sldb.Free;
最终阻止
finally
sldb.Free;
end;
在您的 executeButtonClick(...
procedure TMain.executeButtonClick(Sender: TObject);
var
query: String;
begin
outputMemo.ClearSelection;
query := 'SELECT * FROM hardware;';
sltb := sldb.GetTable(query);
if sltb.Count > 0 then begin
//display first row
....
if not sltb.IsLastRow the begin
sltb.Next;
//display next row
....