Lazarus(Free Pascal)中MDB转MySQL数据库报错求助
读取.mdb文件导入MySQL时持续报错,求解决办法
我尝试读取.mdb文件并将数据填充到现有MySQL服务器的新表中,但每次都会弹出相同的报错:
[Microsoft][ODBC Microsoft Access Driver]常见错误 不能打开注册表关键字 'Temporary (volatile) Jet DSN for process 0x15c0 Thread 0x16dc DBC 0x9f7f4c Jet'。
我已经检查过所有驱动,确认安装正确:
- 已安装MySQL ODBC 8.0 Unicode Driver
- 已安装Microsoft Access Driver (*.mdb)
以下是我的代码:
program MDBtoMySQL; {$mode objfpc}{$H+} uses Classes, SysUtils, odbcconn, db, sqldb; var ConnMDB, ConnMySQL: TODBCConnection; QueryMDB, QueryMySQL: TSQLQuery; TableExists: boolean; MDBFileName, MySQLHost, MySQLDatabase, MySQLUsername, MySQLPassword, MySQLTable: string; begin // Set the file path of the MDB file MDBFileName := 'motor-0.mdb'; // Set MySQL connection details MySQLHost := 'localhost'; MySQLDatabase := 'TestingSQLConnection'; MySQLUsername := 'root'; MySQLPassword := 'Areva123'; MySQLTable := 'new_table'; // Create MDB connection ConnMDB := TODBCConnection.Create(nil); ConnMDB.Driver := 'Microsoft Access Driver (*.mdb)'; ConnMDB.Params.Add('DBQ=' + MDBFileName); ConnMDB.LoginPrompt := False; // Create MySQL connection ConnMySQL := TODBCConnection.Create(nil); ConnMySQL.Driver := 'MySQL ODBC 8.0 Unicode Driver'; ConnMySQL.Params.Add('SERVER=' + MySQLHost); ConnMySQL.Params.Add('DATABASE=' + MySQLDatabase); ConnMySQL.Params.Add('USER=' + MySQLUsername); ConnMySQL.Params.Add('PASSWORD=' + MySQLPassword); ConnMySQL.LoginPrompt := False; try // Connect to MDB ConnMDB.Connected := True; // Connect to MySQL ConnMySQL.Connected := True; // Check if the table already exists in MySQL QueryMySQL := TSQLQuery.Create(nil); QueryMySQL.Database := ConnMySQL; QueryMySQL.SQL.Text := 'SHOW TABLES LIKE "' + MySQLTable + '"'; QueryMySQL.Open; TableExists := not QueryMySQL.IsEmpty; QueryMySQL.Close; QueryMySQL.Free; // Create a new table if it doesn't exist if not TableExists then begin QueryMySQL := TSQLQuery.Create(nil); QueryMySQL.Database := ConnMySQL; QueryMySQL.SQL.Text := 'CREATE TABLE ' + MySQLTable + ' (field1 int, field2 VARCHAR(255))'; QueryMySQL.ExecSQL; QueryMySQL.Free; end; // Retrieve data from MDB QueryMDB := TSQLQuery.Create(nil); QueryMDB.Database := ConnMDB; QueryMDB.SQL.Text := 'SELECT * FROM your_table_in_mdb'; QueryMDB.Open; // Populate data into MySQL QueryMySQL := TSQLQuery.Create(nil); QueryMySQL.Database := ConnMySQL; QueryMySQL.SQL.Text := 'INSERT INTO ' + MySQLTable + ' (field1, field2, ...) VALUES (:field1, :field2, ...)'; QueryMDB.First; while not QueryMDB.EOF do begin QueryMySQL.ParamByName('field1').AsString := QueryMDB.FieldByName('field1').AsString; QueryMySQL.ParamByName('field2').AsString := QueryMDB.FieldByName('field2').AsString; // Add more parameters for other fields as needed QueryMySQL.ExecSQL; QueryMDB.Next; end; // Cleanup QueryMDB.Close; QueryMDB.Free; QueryMySQL.Close; QueryMySQL.Free; // Disconnect from the databases ConnMDB.Connected := False; ConnMySQL.Connected := False; finally // Free resources ConnMDB.Free; ConnMySQL.Free; end; end.
我已经试过各种办法,但始终得到相同的报错,实在不知道问题出在哪,求帮忙排查!
内容的提问来源于stack exchange,提问作者Anthony Marraccini
相关产品推荐
相关产品推荐

