Delphi 7:ADO,需要基本的编码示例

时间:2010-05-27 01:56:46

标签: delphi ado

我是一个完全的初学者。有人可以将一些Delphi代码发布到

  • 创建数据库
  • 添加一个简单的表格
  • 关闭数据库

然后,稍后

  • 打开数据库
  • 阅读每张表
  • 阅读给定表格的每个字段
  • 执行简单搜索

很抱歉这么无能为力。我做了谷歌,但没有找到有用的教程...

此外,如果底层数据库是MySql(5.1.36)(我甚至不知道这是否有任何区别)将会很有用。

3 个答案:

答案 0 :(得分:27)

@mawg,我为你写了一个简单的程序来说明如何使用ADO和Delphi。这是一个控制台应用程序,但解释了基础知识。

在执行此代码之前,您必须从此location下载并安装odbc连接器。

您可以根据自己的要求改进和调整此代码。

program ProjectMysqlADO;

{$APPTYPE CONSOLE}

uses
  ActiveX,
  DB,
  ADODB,
  SysUtils;

const
//the connection string
StrConnection='Driver={MySQL ODBC 3.51 Driver};Server=%s;Database=%s;User=%s; Password=%s;Option=3;';


var
AdoConnection : TADOConnection;

procedure SetupConnection(DataBase:String);//Open a connection
begin
  Writeln('Connecting to MySQL');
  AdoConnection:=TADOConnection.Create(nil);
  AdoConnection.LoginPrompt:=False;//dont ask for the login parameters
  AdoConnection.ConnectionString:=Format(StrConnection,['your_server',DataBase,'your_user','your_password']);
  AdoConnection.Connected:=True; //open the connection
  Writeln('Connected');
end;

procedure CloseConnection;//Close an open connection
begin
  Writeln('Closing connection to MySQL');
  if AdoConnection.Connected then
  AdoConnection.Close;
  AdoConnection.Free;
  Writeln('Connection closed');
end;

procedure CreateDatabase(Database:string);
begin
  Writeln('Creating Database '+database);
  AdoConnection.Execute('CREATE DATABASE IF NOT EXISTS '+Database,cmdText);
  Writeln('Database '+database+' created');
end;

procedure CreateTables;
begin
  Writeln('Creating Tables');
  AdoConnection.Execute(
  'CREATE TABLE IF NOT EXISTS customers ('+
  'id      INT,'+
  'name    VARCHAR(100),'+
  'country VARCHAR(25) )',cmdText);
  Writeln('Tables Created');
end;


procedure DeleteData;
begin
  Writeln('Deleting dummy data');
  AdoConnection.Execute('DELETE FROM customers');
  Writeln('Data deleted');
end;

procedure InsertData;

    Procedure InsertReg(id:integer;name,country:string);
    var
    ADOCommand : TADOCommand;
    begin
      ADOCommand:=TADOCommand.Create(nil);
      try
       ADOCommand.Connection:=AdoConnection;
       ADOCommand.Parameters.Clear;
       ADOCommand.CommandText:='INSERT INTO customers (id,name,country) VALUES (:id,:name,:country)';
       ADOCommand.ParamCheck:=False;
       ADOCommand.Parameters.ParamByName('id').Value      := id;
       ADOCommand.Parameters.ParamByName('name').Value    := name;
       ADOCommand.Parameters.ParamByName('country').Value := country;
       ADOCommand.Execute;
      finally
      ADOCommand.Free;
      end;
    end;

begin
    Writeln('Inserting Data');
    InsertReg(1,'Lilian Kelly','UK');
    InsertReg(2,'John and Sons','USA');
    InsertReg(3,'William Suo','USA');
    InsertReg(4,'MARCOTEC','UK');
    Writeln('Data Inserted');
end;

procedure ReadData;
var
  AdoQuery : TADOQuery;
begin
   AdoQuery:=TADOQuery.Create(nil);
   try
    AdoQuery.Connection:=AdoConnection;
    AdoQuery.SQL.Add('SELECT * FROM customers');
    AdoQuery.Open;
    while not  AdoQuery.eof do
    begin
      Writeln(format('%s %s %s',[AdoQuery.FieldByname('id').AsString,AdoQuery.FieldByname('name').AsString,AdoQuery.FieldByname('country').AsString]));
      AdoQuery.Next;
    end;
   finally
   AdoQuery.Free;
   end;
end;

begin
  CoInitialize(nil); // call CoInitialize()
  try
       Writeln('Init');
       try
         SetupConnection('mysql'); //first will connect to the  mysql database , this database always exist
         CreateDatabase('Mydb'); //now we create the database
         CloseConnection; //close the original connection
         SetupConnection('Mydb'); //open the connection pointing to the Mydb database
         CreateTables; //create a sample table
         DeleteData; //Delete the dummy data before insert
         InsertData; //insert a dummy data
         ReadData; //read the inserted data
         CloseConnection; //close the connection
       except
         on E : Exception do
           Writeln(E.Classname, ': ', E.Message);
       end;
      Readln;
  finally
   CoUnInitialize; // free memory
  end;
end.

答案 1 :(得分:4)

Delphi示例的最佳位置之一是www.delphi.about.com。他们有大量的教程和示例。他们的论坛也非常好。 戴夫

答案 2 :(得分:-1)

您的系统中需要MyODBC,最好使用zeos连接mysql数据库