Delphi 7: ADO, potrzeba podstawowego przykładu kodowania

Jestem tu całkowicie początkujący. Czy ktoś może wrzucić jakiś kod Delphi do

  • Tworzenie bazy danych
  • Dodaj prostą tabelę
  • Zamknij bazę danych

Then, later

  • otwórz bazę danych
  • przeczytaj każdą tabelę
  • odczytaj każde pole podanej tabeli
  • wykonaj proste wyszukiwanie

Przepraszam, że nie mam pojęcia. Zrobiłem google, ale nie znalazłem przydatnego samouczka ...

Ponadto, przydałoby się, gdyby podstawowe bazy danych były MySql (5.1.36) (nawet nie wiem, czy to robi jakąś różnicę)

 11
Author: Aarthi, 2010-05-27

3 answers

@mawg, napisałem dla Ciebie prosty program do zilustrowania pracy z ADO i Delphi. jest to aplikacja konsolowa, ale wyjaśnia podstawy.

Przed wykonaniem tego kodu należy pobrać i zainstalować złącze odbc z tej lokalizacji.

Możesz poprawić i dostosować ten kod do swoich wymagań.

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.
 27
Author: RRUZ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-05-27 06:02:07

Jednym z najlepszych miejsc na przykłady Delphi jest www.delphi.about.com. mają mnóstwo samouczków i przykładów. Ich fora też są naprawdę dobre. Dave

 4
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-05-27 02:06:52

Potrzebujesz MyODBC w swoim systemie, lepiej Użyj zeos do podłączenia bazy danych mysql

 -1
Author: Andre,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-05-27 04:40:03