gpt4 book ai didi

sqlite - 以编程方式使用firedac在Delphi XE6中显示sqlite数据库

转载 作者:行者123 更新时间:2023-12-03 18:15:21 27 4
gpt4 key购买 nike

我是使用Delphi的数据库新手。实际上,我需要使用firedac在gridview中显示所有表及其数据。 youtube和docwiki上的一些视频介绍了如何通过可视组件访问数据库,即在表单上抛出FDConnetion,FDTable,DataSource等。

但是我想通过编程来做同样的事情,以便我可以更加专注于编码和将形式与业务逻辑分开。

这是我在C#中访问MSAccess数据时所做的事情:

public void LoadUsersInfo()
{

try {
OleDbConnection Connection = new OleDbConnection();
Connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;DataSource=dbCyberNet.accdb;Persist Security Info=False;";
Connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = Connection;
String query = "SELECT * FROM tblUsersInfo";
command.CommandText = query;

OleDbDataAdapter adapter = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
adapter.Fill(dt);
dbGrid.DataSource = dt;
}
catch (Exception ex) {
MessageBox.Show("Error occured while loading users information . " + ex.Message.ToString());
}
finally { Connection.Close(); }
}


我想在delphi中为sqlite数据库做类似的事情。这是我尝试过的内容(已编辑)。

procedure TForm1.Button1Click(Sender: TObject);
var
con: TFDConnection;
table: TFDTable;
ds: TDataSource;
begin
con := TFDConnection.Create(nil);
con.LoginPrompt := False;
table := TFDTable.Create(nil);
table.TableName := 'Students';
ds := TDataSource.Create(nil);
ds.DataSet := table;

try
con.Open('DriverID=SQLite;Database=‪studentsDB.sqlite;');
table.Connection := con;
table.Active := True;
con.Connected := True;
DBGrid1.DataSource := ds;
finally
con.Close;
con.Free;
table.Free;
ds.Free;
end;
end;


每当我运行此代码时,就会出现错误对话框,显示:$ 759AF097的第一次机会异常。消息为[[FireDAC] [Phys] [SQLite]的异常类ESQLiteNativeException错误:没有这样的表:STUDENTS。处理Project1.exe(3836)

表中存在相同的表名,并使用可视化的firedac组件,我可以看到数据网格很容易填充。

最佳答案

您可以执行与C#相同的操作:将TFDQuery实例分配为VCL网格的DataSource!

TDataSource添加到窗体,将其DataSet属性设置为您的TFDQuery,然后将其分配给网格。

请参见the official Delphi documentation和其他SO questions

关于sqlite - 以编程方式使用firedac在Delphi XE6中显示sqlite数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26097805/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com