问题
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;
And this is the SQLite database:
Any help you can provide me with will be greatly appreciated. Thank you.
回答1:
You Taged Delphi-XE2 so my answer belongs to XE2
From version RAD Studio XE3 TSQLMonitor supports SQLite databases. ...
With XE2 You can not use SQLConnection1: TSQLConnection; with Driver Sqlite !
Will not work SQLConnection1.Execute(query, nil, results);
According to the code used , it seems to be the simple Delphi wrapper for Sqlite 3 to act.
So you can use the just created sldb .
slDBPath := ExtractFilePath(paramstr(0)) + 'ComputerName.db';
sldb := TSQLiteDatabase.Create(slDBPath);
In the procedure CheckForDatabase;
remove
var
sldb: TSQLiteDatabase;
and put it to the interface of your Application
private
{ Private declarations }
sltb: TSQLIteTable;
sldb: TSQLiteDatabase;
also remove sldb.Free; form the finally block
finally
sldb.Free;
end;
In your 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
....
how to display field values : look here
来源:https://stackoverflow.com/questions/16585953/sqlite-exception-raised-with-message-no-such-table