AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Datenbanken Zugriff auf MySQL ohne Komponenten mit Delphi XE2
Thema durchsuchen
Ansicht
Themen-Optionen

Zugriff auf MySQL ohne Komponenten mit Delphi XE2

Ein Thema von Ykcim · begonnen am 1. Okt 2011 · letzter Beitrag vom 18. Okt 2011
Antwort Antwort
Ykcim

Registriert seit: 29. Dez 2006
Ort: NRW
804 Beiträge
 
Delphi 10.4 Sydney
 
#1

Zugriff auf MySQL ohne Komponenten mit Delphi XE2

  Alt 1. Okt 2011, 21:26
Datenbank: MySQL • Version: 5 • Zugriff über: mysql.pas
Hallo Zusammen,

ich habe bislang auf den MySQL ohne Komponenten zugegriffen, mit dem nachfolgenden Code:

Delphi-Quellcode:
procedure connect;
var Datei: TextFile;
    MyString: String;
begin
  AssignFile(Datei, ExtractFilePath(Application.ExeName) + 'SQL.set');
  Reset(Datei);
  try
    ReadLn(Datei, MyString);
    host := Pchar(copy(MyString, pos(':', MyString)+1, length(MyString)));
    ReadLn(Datei, MyString);
    User := Pchar(copy(MyString, pos(':', MyString)+1, length(MyString)));
    ReadLn(Datei, MyString);
    Pass := Pchar(copy(MyString, pos(':', MyString)+1, length(MyString)));
    ReadLn(Datei, MyString);
    DB := Pchar(copy(MyString, pos(':', MyString)+1, length(MyString)));
  finally
    CloseFile(Datei);
  end;
  _myCon := mysql_init(nil);
  if _mycon=nil then
  begin
    showmessage('Nicht genügend freier Speicher!');
    Exit;
  end;
  if mysql_real_connect(_mycon, host, user, pass,db, 3306, nil,0)=nil then
  begin
    showmessage('Die Verbindung konnte nicht hergestellt werden! Ursache:' + mysql_error(_mycon));
    exit;
  end;
end;
Das funktionierte auch wunderbar und zuverlässig. Jetzt versuche ich gerade den Umstieg von TurboDelphi auf Delphi xe2 starter. Und das ist das Problem. Ich bekomme keine Verbindung mehr zu der Datenbank.

An dieser Stelle im Code macht er einen Fehler:
_myCon := mysql_init(nil);
Hat jemand eine Idee, woran das liegen kann? Die Datenbank hat sich nicht geändert, es muss mit Delphi xe2 zusammenhängen.

Vielen Dank
Ykcim
Patrick
  Mit Zitat antworten Zitat
mkinzler
(Moderator)

Registriert seit: 9. Dez 2005
Ort: Heilbronn
39.851 Beiträge
 
Delphi 11 Alexandria
 
#2

AW: Zugriff auf MySQL ohne Komponenten mit Delphi XE2

  Alt 1. Okt 2011, 21:47
Zitat:
An dieser Stelle im Code macht er einen Fehler:
Welcher?
Markus Kinzler
  Mit Zitat antworten Zitat
Ykcim

Registriert seit: 29. Dez 2006
Ort: NRW
804 Beiträge
 
Delphi 10.4 Sydney
 
#3

AW: Zugriff auf MySQL ohne Komponenten mit Delphi XE2

  Alt 1. Okt 2011, 21:51
Delphi-Quellcode:
procedure connect;
var Datei: TextFile;
     MyString: String;
begin
   AssignFile(Datei, ExtractFilePath(Application.ExeName) + 'SQL.set');
   Reset(Datei);
   try
     ReadLn(Datei, MyString);
     host := Pchar(copy(MyString, pos(':', MyString)+1, length(MyString)));
     ReadLn(Datei, MyString);
     User := Pchar(copy(MyString, pos(':', MyString)+1, length(MyString)));
     ReadLn(Datei, MyString);
     Pass := Pchar(copy(MyString, pos(':', MyString)+1, length(MyString)));
     ReadLn(Datei, MyString);
     DB := Pchar(copy(MyString, pos(':', MyString)+1, length(MyString)));
   finally
     CloseFile(Datei);
   end;

  //AN DIESER STELLE
   _myCon := mysql_init(nil);


   if _mycon=nil then
   begin
     showmessage('Nicht genügend freier Speicher!');
     Exit;
   end;
   if mysql_real_connect(_mycon, host, user, pass,db, 3306, nil,0)=nil then
   begin
     showmessage('Die Verbindung konnte nicht hergestellt werden! Ursache:' + mysql_error(_mycon));
     exit;
   end;
end;

Ich bekomme einfach nur eine Meldung, dass die Verbindung nicht hergestellt werden kann...
Patrick
  Mit Zitat antworten Zitat
Benutzerbild von geskill
geskill

Registriert seit: 17. Feb 2007
Ort: NRW
420 Beiträge
 
Delphi 2010 Professional
 
#4

AW: Zugriff auf MySQL ohne Komponenten mit Delphi XE2

  Alt 1. Okt 2011, 22:03
Sieht wie ein Unicode/Ansi Problem aus. TurboDelphi benutzt ja standardmäßig AnsiString.

Delphi-Quellcode:
procedure connect;
var Datei: TextFile;
      MyString: Ansistring;
begin
    AssignFile(Datei, ExtractFilePath(Application.ExeName) + 'SQL.set');
    Reset(Datei);
    try
      ReadLn(Datei, MyString);
      host := PAnsiChar(copy(MyString, pos(':', MyString)+1, length(MyString)));
      ReadLn(Datei, MyString);
      User := PAnsiChar(copy(MyString, pos(':', MyString)+1, length(MyString)));
      ReadLn(Datei, MyString);
      Pass := PAnsiChar(copy(MyString, pos(':', MyString)+1, length(MyString)));
      ReadLn(Datei, MyString);
      DB := PAnsiChar(copy(MyString, pos(':', MyString)+1, length(MyString)));
    finally
      CloseFile(Datei);
    end;

    //...

EDIT:
Quatsch der Fehler kommt ja schon vorher. Denke mal du hast dann die neue mysql.pas (Version 2011-09-15)
für XE2. Im Header steht da folgendes:

Zitat:
procedure mysql_server_end;
{ "Jeremiah Gowdy" <jgowdycox.net> wrote on 10/11/2005 03:08:40 AM:
The Windows DLL is thread safe. You do not have to call my_init()
and my_thread_init() because Windows DLLs receive events when they
are attached to a new process and when they are attached to a new
thread in a process. This is one of the nicer features of Windows
shared libraries. Other than that, you don't have to do anything
special. I am a heavy user of libmysql under Win32. You simply
mysql_init() your MYSQL struct, and then mysql_real_connect() and
you're ready to mysql_query().
New on February 17, 2009 02:27AM: This is true until 5.0.77 -
since this version this nice feature ist removed from dll.c.
To obtain the previous behavior (DLL initialization code will be
called), set the LIBMYSQL_DLLINIT environment variable to
any value. http://forums.mysql.com/read.php?3,248207,248207 }
function mysql_thread_init: my_bool; //called internal by mysql_init or mysql_server_init
{ New on February 17, 2009 02:27AM: Since 5.0.77 mysql_thread_end
is not called during DllMain() if the LIBMYSQL_DLLINIT environment variable is
not set. So it is necessary to call mysql_thread_end to avoid memory leaks. }
procedure mysql_thread_end;
Aber das wäre ja nur der Fall für Threadsicherheit.

Aber du hast ja nicht die Unit von TurboDelphi für XE2 benutzt, weil da brauchst du denke ich
die neue welche extra dafür kompatibel gemacht wurde.
Sebastian

Geändert von geskill ( 1. Okt 2011 um 22:18 Uhr)
  Mit Zitat antworten Zitat
Ykcim

Registriert seit: 29. Dez 2006
Ort: NRW
804 Beiträge
 
Delphi 10.4 Sydney
 
#5

AW: Zugriff auf MySQL ohne Komponenten mit Delphi XE2

  Alt 1. Okt 2011, 22:23
Vielen Dank für die Antwort!

Ich habe den Code jetzt soweit geändert:

Delphi-Quellcode:
var
  Form1: TForm1;
  _myCon: PMYSQL;
  host, user, pass, db: PChar;
  ErrorCode: Integer;
  MySQLRes: PMYSQL_RES;
  MySQLRow: PMYSQL_ROW;
  AffectedRows: Int64;
  Field: PMYSQL_FIELD;

//...

procedure connect;
var Datei: TextFile;
       MyString: Ansistring;
begin
     AssignFile(Datei, ExtractFilePath(Application.ExeName) + 'SQL.set');
     Reset(Datei);
     try
       ReadLn(Datei, MyString);
       host := PChar(copy(MyString, pos(':', MyString)+1, length(MyString)));
       ReadLn(Datei, MyString);
       User := PChar(copy(MyString, pos(':', MyString)+1, length(MyString)));
       ReadLn(Datei, MyString);
       Pass := PChar(copy(MyString, pos(':', MyString)+1, length(MyString)));
       ReadLn(Datei, MyString);
       DB := PChar(copy(MyString, pos(':', MyString)+1, length(MyString)));
     finally
       CloseFile(Datei);
     end;
  end;
{73}  _myCon := mysql_init(nil);
  if _mycon=nil then
  begin
    showmessage('Nicht genügend freier Speicher!');
{78}    Exit;
  end;
{80}  if mysql_real_connect(_mycon, host, user, pass,db, 3306, nil,0)=nil then
  begin
    showmessage('Die Verbindung konnte nicht hergestellt werden! Ursache:' + mysql_error(_mycon));
    exit;
{83}  end;
end;

Leider habe ich es jetzt noch nicht geschafft, wieder zu kompilieren, da ich diverse Fehler bekomme:
Zitat:
[DCC Fehler] Unit1.pas(73): E2029 Deklaration erwartet, aber Bezeichner '_myCon' gefunden
[DCC Fehler] Unit1.pas(78): E2029 '.' erwartet, aber ';' gefunden
[DCC Fehler] Unit1.pas(80): E2004 Bezeichner redefiniert: 'Finalization'
[DCC Warnung] Unit1.pas(83): W1011 Text hinter dem abschließenden 'END.' - wird vom Compiler ignoriert
[DCC Fehler] Unit1.pas(13): E2065 Ungenügende Forward- oder External-Deklaration: 'TForm1.Button1Click'
[DCC Fataler Fehler] Project1.dpr(5): F2063 Verwendete Unit 'Unit1.pas' kann nicht compiliert werden
Misslungen
Verstrichene Zeit: 00:00:00.2
Könnt Ihr mir sagen, wo diese Fehler liegen?

Vielen Dank
Ykcim
Patrick
  Mit Zitat antworten Zitat
Ykcim

Registriert seit: 29. Dez 2006
Ort: NRW
804 Beiträge
 
Delphi 10.4 Sydney
 
#6

AW: Zugriff auf MySQL ohne Komponenten mit Delphi XE2

  Alt 1. Okt 2011, 22:45
Vergesst das!

Ich habe mir die neuste mysql.pas heruntergeldaen, aber da bekomme ich Kompilierungsfehler...

Versuche es nachzuvollziehen.
Patrick
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 16:52 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz