Go to the first, previous, next, last section, table of contents.


19 MySQL ODBC Support

MySQLMyODBC プログラムで ODBC 機能を提供します。 This chapter will teach you how to install MyODBC, and how to use it. Here, you will also find a list of common programs that are known to work with MyODBC.

19.1 How To Install MyODBC

MyODBC は 32-bit ODBC (2.50) level 0 driver (with level 1 and level 2 features) で、 ODBC 化されたアプリケーションが MySQL に接続する場合に使用します。 MyODBC は、Windows95, Windows98, NT とほとんどの UNIX で動きます。

通常、MyODBC は Windows マシンにだけ、インストールします。 、あなたのプログラムが ColdFusion のようにデータベースへの接続に ODBC だけを使用する場合に限り、UNIX に MyODBC が必要です。

MyODBC はパブリック・ドメインで、最新の物は以下にあります: http://www.mysql.com/downloads/api-myodbc.html.

日本語文字コードに対応させた物は: http://www.SoftAgency.co.jp/.

If you have problem with MyODBC and your program also works with OLEDB, you should try the OLEDB driver that you can find in the Contrib section. 「D Contributed Programs」節参照.

Normally you only need to install MyODBC on Windows machines. You only need MyODBC for Unix if you have a program like ColdFusion that is running on the Unix machine and uses ODBC to connect to the databases.

もし、UNIX に MyODBC をインストールしたいなら、 ODBC マネージャーも必要でしょう。 MyODBC は UNIX の ODBC マネージャー でも動作することが知られています。 MySQL useful links page の ODBC-related links 節に、見つけることができます。 「1.10 便利な MySQL 関連リンク」節参照.

To install MyODBC on windows, you should download the appropriate MyODBC .zip file (for Windows or NT/Win2000), unpack it with WINZIP, or some similar program, and execute the SETUP.EXE file.

Windows/NT の場合、MyODBC をインストール時にいかのような エラーになるかもしれません:

An error occurred while copying C:\WINDOWS\SYSTEM\MFC30.DLL. Restart
Windows and try installing again (before running any applications which
use ODBC)

The problem in this case is that some other program is using ODBC and because of how Windows is designed, you may not in this case be able to install a new ODBC drivers with Microsoft's ODBC setup program. In most cases you can continue by just pressing Ignore to copy the rest of the MyODBC files and the final installation should still work. If this doesn't work, これを解決するには、一度 ’セーフモード’でコンピュータを立ち上げ直し、 (windows リブート時に、F8キーを押すことで、セーフモードの選択が可能です) MyODBC をインストールして、リブートします:

MyODBC の設定画面には、MySQL 接続時のオプションがいくつか 設定できるようになっています。 もし問題がある場合は、これらを試します。

19.2 ODBC 管理プログラムの各種項目を埋めるには?

Windows95 上のサーバ名の記述には3つの可能性があります:

ODBC setup を埋める方法の例:

Windows DSN name:   test
Description:        This is my test database
MySql Database:     test
Server:             194.216.84.21
User:               monty
Password:           my_password
Port:

Windows DSN name 項目の値は、あなたのwindows ODBC setup における 一意の名前です。

ODBC setup 画面で、Server, User, Password, Port フィールドを設定する必要はありません。 しかし、ここで設定をすると、後でサーバーに接続する際に、 設定した値がデフォルト値として使用されます。 使用時に値を変更するオプションはあります。

ポート番号が設定されていないなら、デフォルトポート (3306) が使用されます。

もしオプションを Read options from C:\my.cnf にすると, clientodbc グループは `C:\my.cnf' ファイルから読まれます。 mysql_options() で使用できる全てのオプションが利用可能です。 「24.1.3.159 mysql_options()」節参照.

19.3 Connect parameters for MyODBC

One can specify the following parameters for MyODBC on the [Servername] section of an ODBC.INI file or through the InConnectionString argument in the SQLDriverConnect() call.

Parameter Default value Comment
user ODBC (on windows) The username used to connect to MySQL.
server localhost The hostname of the MySQL server.
database The default database
option 0 A integer by which you can specify how MyODBC should work. See below.
port 3306 The TCP/IP port to use if server is not localhost.
stmt A statement that will be executed when connection to MySQL.
password The password for the server user combination.
socket The socket or windows pipe to connect to.

The option argument is used to tell MyODBC that the client isn't 100% ODBC compliant. On windows, one normally sets the option flag by toggling the different options on the connection screen but one can also set this in the opton argument. The following options are listed in the same order as they appear in the MyODBC connect screen:

Bit Meaning
1 The client can't handle that MyODBC returns the real width of a column.
2 The client can't handle that MySQL returns the true value of affected rows. If this flag is set then MySQL returns 'found rows' instead. One must have MySQL 3.21.14 or newer to get this to work.
4 Make a debug log in c:\myodbc.log. This is the same as putting MYSQL_DEBUG=d:t:O,c::\myodbc.log in `AUTOEXEC.BAT'
8 Don't set any packet limit for results and parameters.
16 Don't prompt for questions even if driver would like to prompt
32 Simulate a ODBC 1.0 driver in some context.
64 Ignore use of database name in 'database.table.column'.
128 Force use of ODBC manager cursors (experimental).
256 Disable the use of extended fetch (experimental)
512 Pad CHAR fields to full column length.
1024 SQLDescribeCol() will return fully qualifed column names
2048 Use the compressed server/client protocol
4096 Tell server to ignore space after function name and before '(' (needed by PowerBuilder). This will make all function names keywords!
8192 Connect with named pipes to a mysqld server running on NT.
16384 Change LONGLONG columns to INT columns (Some applications can't handle LONGLONG).
32768 Return 'user' as Table_qualifier and Table_owner from SQLTables (experimental)
65536 Read parameters from the client and odbc groups from my.cnf
131072 Add some extra safety checks (should not bee needed but...)

If you want to have many options, you should add the above flags! For example setting option to 12 (4+8) gives you debugging without package limits!

The default `MYODBC.DLL' is compiled for optimal performance. If you want to to debug MyODBC (for example to enable tracing), you should instead use MYODBCD.DLL. To install this file, copy `MYODBCD.DLL' over the installed MYODBC.DLL file.

19.4 MyODBCでの問題をどのように報告すべきか?

MyODBC は以下でテストされました: Access, Admndemo.exe, C++-Builder, Borland Builder 4, Centura Team Developer (formerly Gupta SQL/Windows), ColdFusion (on Solaris and NT with svc pack 5), Crystal Reports, DataJunction, Delphi, ERwin, Excel, iHTML, FileMaker Pro, FoxPro, Notes 4.5/4.6, SBSS, Perl DBD-ODBC, Paradox, Powerbuilder, Powerdesigner 32 bit, VC++, and Visual Basic.

MyODBC で動作する他のアプリケーションについても知っているなら、 myodbc@lists.mysql.com までメールをください!

With some programs you may get an error like: Another user has modifies the record that you have modified. In most cases this can be solved by doing one of the following things:

If the above doesn't help, you should do a MyODBC trace file and try to figure out why things go wrong.

19.5 MyODBCで動作することが知られているプログラム

多くのプログラムが MyODBC で動作します。これらは我々自身がテストしたか、 または何人かの他のユーザから動作するという確証を得たものです:

Program
Comment
Access
Access を動作させるには:
ADO
When you are coding with the ADO API and MyODBC you need to put attention in some default properties that aren't supported by the MySQL server. For example, using the CursorLocation Property as adUseServer will return for the RecordCount Property a result of -1. To have the right value, you need to set this property to adUseClient, like is showing in the VB code below:
Dim myconn As New ADODB.Connection
Dim myrs As New Recordset
Dim mySQL As String
Dim myrows As Long

myconn.Open "DSN=MyODBCsample"
mySQL = "SELECT * from user"
myrs.Source = mySQL
Set myrs.ActiveConnection = myconn
myrs.CursorLocation = adUseClient
myrs.Open
myrows = myrs.RecordCount

myrs.Close
myconn.Close
Another workaround is to use a SELECT COUNT(*) statement for a similar query to get the correct row count.
Active server pages (ASP)
You should use the option flag Return matching rows.
BDE applications
To get these to work, you should set the option flags Don't optimize column widths and Return matching rows.
Borland Builder 4
When you start a query you can use the property Active or use the method Open. Note that Active will start by automatically issuing a SELECT * FROM ... query that may not be a good thing if your tables are big!
ColdFusion (On Unix)
The following information is taken from the ColdFusion documentation: Use the following information to configure ColdFusion Server for Linux to use the unixODBC driver with MyODBC for MySQL data sources. Allaire has verified that MyODBC Version 2.50.26 works with MySQL Version 3.22.27 and ColdFusion for Linux. (Any newer version should also work.) You can download MyODBC at http://www.mysql.com/downloads/api-myodbc.html ColdFusion Version 4.5.1 allows you to us the ColdFusion Administrator to add the MySQL data source. However, the driver is not included with ColdFusion Version 4.5.1. Before the MySQL driver will appear in the ODBC datasources drop-down list, you must build and copy the MyODBC driver to `/opt/coldfusion/lib/libmyodbc.so'. The Contrib directory contains the program mydsn-xxx.zip which allows you to build and remove the DSN registry file for the MyODBC driver on Coldfusion applications.
DataJunction
You have to change it to output VARCHAR rather than ENUM, as it exports the latter in a manner that causes MySQL grief.
Excel
動作。Some tips:
Word
To retrieve data from MySQL to Word/Excel documents, you need to use the MyODBC driver and the Add-in Microsoft Query help. For example, create a db with a table containing 2 columns of text:
odbcadmin
ODBC のテストプログラム.
Delphi
DBE 3.2 以上を必ず使用してください。 MySQL に接続する際には、'Don't optimize column width' オプションをセットします。 ODBCエントリとBDEエントリの両方をセットアップするおりに役に立つであろう Delphi のコード例を示します ( BDE は Delphi Super Page から無償で得られる BDE Alias Editor が必要です)。 : (Thanks to Bryan Brunton bryan@flesherfab.com for this)
fReg:= TRegistry.Create;
  fReg.OpenKey('\Software\ODBC\ODBC.INI\DocumentsFab', True);
  fReg.WriteString('Database', 'Documents');
  fReg.WriteString('Description', ' ');
  fReg.WriteString('Driver', 'C:\WINNT\System32\myodbc.dll');
  fReg.WriteString('Flag', '1');
  fReg.WriteString('Password', '');
  fReg.WriteString('Port', ' ');
  fReg.WriteString('Server', 'xmark');
  fReg.WriteString('User', 'winuser');
  fReg.OpenKey('\Software\ODBC\ODBC.INI\ODBC Data Sources', True);
  fReg.WriteString('DocumentsFab', 'MySQL');
  fReg.CloseKey;
  fReg.Free;

  Memo1.Lines.Add('DATABASE NAME=');
  Memo1.Lines.Add('USER NAME=');
  Memo1.Lines.Add('ODBC DSN=DocumentsFab');
  Memo1.Lines.Add('OPEN MODE=READ/WRITE');
  Memo1.Lines.Add('BATCH COUNT=200');
  Memo1.Lines.Add('LANGDRIVER=');
  Memo1.Lines.Add('MAX ROWS=-1');
  Memo1.Lines.Add('SCHEMA CACHE DIR=');
  Memo1.Lines.Add('SCHEMA CACHE SIZE=8');
  Memo1.Lines.Add('SCHEMA CACHE TIME=-1');
  Memo1.Lines.Add('SQLPASSTHRU MODE=SHARED AUTOCOMMIT');
  Memo1.Lines.Add('SQLQRYMODE=');
  Memo1.Lines.Add('ENABLE SCHEMA CACHE=FALSE');
  Memo1.Lines.Add('ENABLE BCD=FALSE');
  Memo1.Lines.Add('ROWSET SIZE=20');
  Memo1.Lines.Add('BLOBS TO CACHE=64');
  Memo1.Lines.Add('BLOB SIZE=32');

  AliasEditor.Add('DocumentsFab','MySQL',Memo1.Lines);
C++Builder
BDE 3.0で,テストされました。唯一の知られた問題は、 テーブルスキーマが変化する時、クエリフィールドが更新されないことです。 BDE は、index PRIMARY だけを認識し、プライマリーキーは認識していないように思えます。これはさほど問題ではないですが。
Vision
You should use the option flag Return matching rows.
Visual Basic
テーブルの更新を可能にするため、テーブルにプライマリーキーを作成しなくてはなりません。 Visual Basic with ADO can't handle big integers. This means that some queries like SHOW PROCESSLIST will not work properly. The fix is to set add the option OPTION=16834 in the ODBC connect string or set the Change BIGINT columns to INT option in the MyODBC connect screen. You may also want to set the Return matching rows option.
VisualInterDev
If you get the error [Microsoft][ODBC Driver Manager] Driver does not support this parameter the reason may be that you have a BIGINT in your result. Try setting the Change BIGINT columns to INT option in the MyODBC connect screen.
Visual Objects
You should use the option flag Don't optimize column widths.

19.6 AUTO_INCREMENT フィールドの値を ODBC で得る方法

共通の問題は、INSERT で自動的に生成されるIDの値を得ることです。 ODBCでは、以下のようにします( 例中の autoAUTO_INCREMENT フィールドです):

INSERT INTO foo (auto,text) VALUES(NULL,'text');
SELECT LAST_INSERT_ID();

もしくは、他のテーブルにIDを挿入するだけなら以下のようにします:

INSERT INTO foo (auto,text) VALUES(NULL,'text');
INSERT INTO foo2 (id,text) VALUES(LAST_INSERT_ID(),'text');

24.1.4.3 最後に挿入された行のユニーク ID をどのように得られるか?」節参照.

いくつかのODBCアプリケーション(少なくともDelphiとAccess)は、 新しく挿入された行を見つけるのに、以下のクエリが使用できます:

SELECT * FROM tbl_name WHERE auto IS NULL;

19.7 Reporting Problems with MyODBC

もし MyODBC 使用時に難しい問題にあたった場合、 ODBC マネージャのログファイル (ODBCADMIN からリクエストしたときのログ) と MyODBC のログを取り始めるべきです。 これはいかなる問題にも解決の糸口になるはずです。

MyODBC のログを取るには、以下のように行ないます:

  1. Ensure that you are using myodbcd.dll and not myodbc.dll. The easiest way to do this is to get myodbcd.dll from the MyODBC distribution and copy it over the myodbc.dll, which is probably in your C:\windows\system32 or C:\winnt\system32 directory. Note that you probably want to restore the old myodbc.dll file when you have finished testing, as this is a lot faster than myodbcd.dll.
  2. MyODBC connect/configure 画面の 'Trace MyODBC' オプションをチェックします。 ログは `C:\myodbc.log' に書き出されます。 If the trace option is not remembered when you are going back to the above screen, it means that you are not using the myodbcd.dll driver (see above).
  3. Start your application and try to get it to fail.

Check the MyODBC trace file, to find out what could be wrong. You should be able to find out the issued queries by searching after the string >mysql_real_query in the `myodbc.log' file.

また、MyODBC か MySQL のエラーを見つけるためには、 同じコマンドを、mysql コマンドや admndemo でも試すべきです。

もし、問題を発見したなら、関係する行を(最大40行) myodbc@lists.mysql.com に 送ってください。 MyODBC や ODBC のログファイル全てを送らないように してください!

もし問題を見つけることが出来なかった場合、MyODBC トレースファイル、 ODBC ログファイル、問題の説明する README ファイルを含んだ アーカイブ(tar か zip) を作成します。 これを ftp://support.mysql.com/pub/mysql/secret. に送ります。 MySQL AB だけが、アップロードされたファイルにアクセスできます。

もし問題を見るためのプログラムも作っているなら、それも同時に送ります。

もしそのプログラムが他の SQL サーバーで動くなら、 他の SQL サーバーで同じことをした場合の ODBC ログファイルを作成すべきです。

多くの情報をあなたが提供すれば、我々は問題を解決しやすくなることを 念頭においてください。


Go to the first, previous, next, last section, table of contents.