Sign in Join
Wiki copytemporary

ODBCConn/ja: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11**ODBCConn** ユニットはOpen Database Connectivity (ODBC)データソースにSQLbの接続を実装している。
22
33## 概要
44
55### ODBC
66
77ODBC (Open Database Connectivity) とは単純なAPIである、ODBC APIを用いて、さまざまなデータベース全体に接続することのできる技術である。
88
99さまざまなプラットフォームに向けて、ODBCの実装があり、ほとんどのデータベースマネジメントシステムにドライバがある。 公式のODBCドキュメンテーションは\[<http://msdn.microsoft.com/en-us/library/ms710252(VS.85>).aspx MSDN ODBC site\]である。
1010
1111### TODBCConnection
1212
1313FreePascal ships with ODBC headers; they are in the `odbcsql` and `odbcsqldyn` units. [[TODBCConnection]] is an [[TSQLConnection]] descendant providing a nice OOP wrapper for ODBC using the SQLdb framework.
1414
1515In Lazarus, you can find the `TODBCConnection` component on the SQLdb component tab. You can also use the `TODBCConnection` component in your code by adding `ODBCConn` to your uses clause.
1616
17-<figure>
18-<img src="sqldbcomponents-n.png" title="sqldbcomponents-n.png" />
19-<figcaption>sqldbcomponents-n.png</figcaption>
20-</figure>
17+![](/wiki-images/c/c1/Sqldbcomponents-n.png)
2118
2219What has been implemented:
2320
2421- executing queries and retrieving result sets
2522- most field types, including blobs
2623- query parameters (string and integer types)
2724- preparing queries
2825- UpdateIndexDefs (so you can use ApplyUpdates)
2926
3027What is left to be implemented:
3128
3229- proper transaction support; currently each connection corresponds to one transaction
3330- some field types
3431 - SQL_TYPE_UTC* (these are mentioned in the ODBC docs, but seem not to be used in implementations)
3532 - SQL_INTERVAL_* (what would be the corresponding TFieldType?)
3633 - SQL_GUID (TGUIDField was not implemented, until recently)
3734
3835### なぜ ODBC なのか ?
3936
4037FreePascal ships with components for connecting to several databases, such as MySQL, PostGreSQL, Firebird, Microsoft SQL Server (since 2.6.1), Oracle, etc.
4138
4239For those databases missing from the list, like [[MS Access]] and perhaps Microsoft SQL Server, ODBC is an acceptable and well established solution. (VB.net/C# developers are recommended to use SQLClient rather than OLEDB or ODBC for the ultimate in performance, but here in Lazarus ODBC will work adequately and reliably)
4340
4441The TODBCConnection component was developed originally to circumvent the strict MySQL license for applications that are not GPLed or do not obey MySQL AB's [FLOSS exception](http://www.mysql.com/company/legal/licensing/foss-exception.html).
4542
4643## ODBC と ODBC ドライバをインストールする
4744
4845Before you can connect to your database using ODBC, you need to install
4946
5047- an ODBC Driver Manager
5148- an ODBC driver specific to the DBMS you want to connect to
5249
5350This section will give a brief overview of the steps involved. However, please consult the relevant documentation for a definitive reference.
5451
5552### ODBC ドライバマネージャ
5653
5754#### Windows
5855
5956Windows has an ODBC Driver Manager built in, which allows DSNs to be set up, and other configuration. It is found in the *Control Panel*, in later Windows versions with categorised sections it was moved into the *Administrative Tools* area. Or you may simply click the Start button and enter *ODBC* into the Run box (in Windows 7 this appears as "*Search programs and files*").
6057
6158You can, of course, create a desktop shortcut to the ODBC dialog if you find yourself using it frequently.
6259
63-<figure>
64-<img src="WinXPODBCDataSourceAdministratorDrivers.png" title="WinXPODBCDataSourceAdministratorDrivers.png" />
65-<figcaption>WinXPODBCDataSourceAdministratorDrivers.png</figcaption>
66-</figure>
60+![](/wiki-images/3/39/WinXPODBCDataSourceAdministratorDrivers.png)
6761
6862#### Unices
6963
7064Two popular ODBC Driver Managers for Unix-based platforms are [unixODBC](http://www.unixodbc.org) and [iODBC](http://www.iodbc.org). `ODBCConn` is known to work with unixODBC; iODBC compatibility still has to be tested.
7165
7266##### Debian
7367
7468For Debian, you can install the *unixodbc* package:
7569
7670``` bash
7771aptitude install unixodbc
7872aptitude install unixodbc-bin # if you want some GUI tools
7973```
8074
8175The `odbcsqldyn` unit, and hence `odbcconn`, will search for a file called `libodbc.so`. It will *not* accept a file named like `libodbc.so.1` or `libodbc.so.1.0.0`. Debian's *unixodbc* package does not create a symlink with the name `/usr/lib/libodbc.so`; you must either
8276
8377- create the link yourself: `ln -s libodbc.so.1 /usr/lib/libodbc.so`, or
8478- install the *unixodbc-dev* package, which does create the symlink.
8579
8680If you installed the *unixodbc-bin* package, you can run the `ODBCConfig` program to configure ODBC drivers and DSNs.
8781
88-<figure>
89-<img src="ODBCDataSourceAdministratorAbout.PNG" title="ODBCDataSourceAdministratorAbout.PNG" />
90-<figcaption>ODBCDataSourceAdministratorAbout.PNG</figcaption>
91-</figure>
82+![](/wiki-images/b/bf/ODBCDataSourceAdministratorAbout.PNG)
9283
9384##### Ubuntu
9485
9586For Ubuntu, follow the instruction for Debian. Note: the *unixodbc-bin* package might not be available from the default package repository.
9687
9788### ODBC Drivers
9889
9990訳注:以下にリンクがありますが、保存の時にはじかれましたので、https://は省いてあります You can download latest ODBC drivers using the following links:
10091
10192www.devart.com/odbc/sqlserver/download.html Download SQL Server 32/64 bit ODBC driver - latest version of SQL Server ODBC driver which supports Windows, Mac OS X, Linux both on 32 and 64 versions. Compatible with SQL Server 2014\2012\2008 R2\\ 2008\2005 (including Express edition), SQL Server 2000 (including MSDE), SQL Server 7, SQL Server Compact 4.0\3.5\3.1. Works on all Lazarus versions.
10293
10394www.devart.com/odbc/oracle/download.html Download Oracle 32/64 bit ODBC driver - latest version of Oracle ODBC driver which supports Windows, Mac OS X, Linux both on 32 and 64 versions. Compatible with Oracle servers: 12c, 11g, 10g, 9i, 8i, 8.0, including Oracle Express Edition 11g and 10g. Oracle Clients: 12c, 11g, 10g, 9i, 8i, 8.0. Works on all Lazarus versions.
10495
10596www.devart.com/odbc/mysql/download.html Download MySQL 32/64 bit ODBC driver - latest version of MySQL ODBC driver which supports Windows, Mac OS X, Linux both on 32 and 64 versions. Compatible with MySQL servers: 6.0, 5.6, 5.5, 5.1, 5.0, 4.1, 4.0, and 3.23. MariaDB 5.x Works on all Lazarus versions.
10697
10798www.devart.com/odbc/postgresql/download.html Download PostgreSQL 32/64 bit ODBC driver - latest version of PostgreSQL ODBC driver which supports Windows, Mac OS X, Linux both on 32 and 64 versions. Compatible with PostgreSQL server versions since 7.1 up to 9.4. Works on all Lazarus versions.
10899
109100www.devart.com/odbc/postgresql/download.html Download SQLite 32/64 bit ODBC driver - latest version of SQLite ODBC driver which supports Windows, Mac OS X, Linux both on 32 and 64 versions. Compatible with SQLite versions since 3.0 and higher. Works on all Lazarus versions.
110101
111102www.devart.com/odbc/firebird/download.html Download Firebird 32/64 bit ODBC driver - latest version of Firebird ODBC driver which supports Windows, Mac OS X, Linux both on 32 and 64 versions. Compatible with Firebird versions 3, 2.x, 1.x. Works on all Lazarus versions.
112103
113104www.devart.com/odbc/interbase/download.html Download Interbase 32/64 bit ODBC driver - latest version of Interbase ODBC driver which supports Windows, Mac OS X, Linux both on 32 and 64 versions. Compatible with all Interbase versions. Works on all Lazarus versions.
114105
115106www.devart.com/odbc/sqlazure/download.html Download SQL Azure 32/64 bit ODBC driver - latest version of SQL Azure ODBC driver which supports Windows, Mac OS X, Linux both on 32 and 64 versions. Compatible with all SQL Azure versions. Works on all Lazarus versions.
116107
117108## ODBC データソースに接続する
118109
119110The parameters for connecting to an ODBC data source are described in a *connection string*. This is a string of the form `NAME=VALUE;NAME=VALUE`.
120111
121112`TODBCConnection` provides a wrapper around this connection string. Some of its properties are mapped to name-value pairs in the connection string, and custom parameters can be specified in the `Params` property (which is a `TStrings`).
122113
123114Before going to the details of this wrapper, you must first have a basic understanding of how an ODBC data source is identified.
124115
125116### DSN 経由で ODBC 接続
126117
127118An ODBC driver manager provides alternative ways to make DSN shortcuts for a set of parameters:
128119
129120- DSN (**D**ata**S**ource **N**ame): a system or user defined data source, identified by a (unique) name. DSNs can be configured using the ODBC Data Source Administrator or by manually editing the `odbc.ini` file (or registry).
130121- File DSN: a file which contains connection parameters. An ODBC Data Source Administrator usually allows you to create File DSNs from the GUI.
131122
132123The parameters in a DSN or File DSN can always be combined with additional parameters in the connection string, for example to specify a password.
133124
134125By setting up a DSN the connection details can be verified to work within the manager dialog, and then the named DSN is all that is needed to use the connection later. The connection details are therefore *decoupled* from your application, as only the DSN name is used in your software - the DSN acting as a go-between.
135126
136127An advantage to using a named DSN is that you may easily switch between databases using the ODBC Manager, without changing your code. This is useful in commercial development to test for bugs using a number of sets of your clients' data.
137128
138129### DSN なしで
139130
140131You may also connect via ODBC without using a DSN, simply supplying all the details in the Connection String that you would otherwise set up in a DSN (driver,server,database,login details). This more direct method avoids the need to set up a DSN when installing your application on a new machine.
141132
142133The ODBC specification defines a few parameters that can be used in a connection string:
143134
144135- Two special parameters, `DSN` and `FILEDSN`, allow one to select a set of pre-defined parameters, as described above.
145136- The `DRIVER` specifies which ODBC driver to use. Obviously, this is a very important parameter.
146137- The `UID` and `PWD` parameters are used to supply a username and password.
147138
148139All other parameters are driver dependent. Please refer to the documentation of the specific driver to learn more about available parameters and their names.
149140
150141### TODBCConnection プロパティ
151142
152143The following table describes the mapping of `TODBCConnection` properties to ODBC connection string parameters:
153144
154-<table>
155-<thead>
156-<tr>
157-<th><p>Property</p></th>
158-<th><p>Type</p></th>
159-<th><p>Connection string parameter</p></th>
160-</tr>
161-</thead>
162-<tbody>
163-<tr>
164-<td><p><code>Driver</code></p></td>
165-<td><p><code>string</code></p></td>
166-<td><p><code>DRIVER</code></p></td>
167-</tr>
168-<tr>
169-<td><p><code>DatabaseName</code></p></td>
170-<td><p><code>string</code></p></td>
171-<td><p><code>DSN</code>, <em>not</em> to something like <code>DATABASENAME</code>, which is not part of the ODBC standard</p></td>
172-</tr>
173-<tr>
174-<td><p><code>FileDSN</code></p></td>
175-<td><p><code>string</code></p></td>
176-<td><p><code>FILEDSN</code></p></td>
177-</tr>
178-<tr>
179-<td><p><code>Password</code></p></td>
180-<td><p><code>string</code></p></td>
181-<td><p><code>PWD</code></p></td>
182-</tr>
183-<tr>
184-<td><p><code>UserName</code></p></td>
185-<td><p><code>string</code></p></td>
186-<td><p><code>UID</code></p></td>
187-</tr>
188-<tr>
189-<td><p><code>HostName</code></p></td>
190-<td><p><code>string</code></p></td>
191-<td><p><em>none</em>; there is no corresponding parameter in the ODBC standard</p></td>
192-</tr>
193-<tr>
194-<td><p><code>Params</code></p></td>
195-<td><p><code>TStrings</code></p></td>
196-<td><p>Use this to specify custom parameters. Each item must be of the form <code>NAME=VALUE</code>.</p>
197-<p>One important parameter that can be used is <strong><code>AUTOCOMMIT</code></strong> which determines if SQL statements are directly executed (without possibility of a rollback) (default setting, or set explicitly using <code>AUTOCOMMIT=1</code>) or whether you need to manually call StartTransaction and Commit, CommitRetaining or Rollback (set using <code>AUTOCOMMIT=0</code>)</p></td>
198-</tr>
199-</tbody>
200-</table>
145+**Property**
146+
147+**Type**
148+
149+**Connection string parameter**
150+
151+`Driver`
152+
153+`string`
154+
155+`DRIVER`
156+
157+`DatabaseName`
158+
159+`string`
160+
161+`DSN`, *not* to something like `DATABASENAME`, which is not part of the ODBC standard
162+
163+`FileDSN`
164+
165+`string`
166+
167+`FILEDSN`
168+
169+`Password`
170+
171+`string`
172+
173+`PWD`
174+
175+`UserName`
176+
177+`string`
178+
179+`UID`
180+
181+`HostName`
182+
183+`string`
184+
185+*none*; there is no corresponding parameter in the ODBC standard
186+
187+`Params`
188+
189+`TStrings`
190+
191+Use this to specify custom parameters. Each item must be of the form `NAME=VALUE`.
192+
193+One important parameter that can be used is **`AUTOCOMMIT`** which determines if SQL statements are directly executed (without possibility of a rollback) (default setting, or set explicitly using `AUTOCOMMIT=1`) or whether you need to manually call StartTransaction and Commit, CommitRetaining or Rollback (set using `AUTOCOMMIT=0`)
201194
202195Note that `TODBCConnection` will take care of escaping parameter values in the connection string (when required).
203196
204197The `LoginPrompt` boolean property is not implemented yet. It would require finding the correct window handle, so a driver can show a GUI dialog to specify parameters. Note that this is not controlled by the connection string, but rather by the last parameter to the ODBC API function `SQLDriverConnect`.
205198
206199## 例
207200
208201In this section, examples are given of connecting to certain DBMSs using their specific ODBC drivers.
209202
210203### Excel に接続する
211204
212205Probably works on Windows only.
213206
214207To avoid errors like: Could not start transaction! ODBC error details: LastReturnCode: SQL_ERROR; Record 1: SqlState: HYC00; NativeError: 106; Message: \[Microsoft\]\[ODBC Excel Driver\]Optional feature not implemented ;
215208
216209... you'll have to enable autocommit, something like
217210
218211``` pascal
219212ODBCConnection1.Params.Add('AUTOCOMMIT=1');
220213```
221214
222215### MySQL に接続する
223216
224217For a reference of supported parameters by the MyODBC driver, see [1](https://dev.mysql.com/doc/connector-odbc/en/connector-odbc-configuration-connection-parameters.html).
225218
226219Additionally you may check this www.devart.com/odbc/mysql/docs/using_odbc_driver.htm mysql odbc connection guide. It has a detail explanation of how to connect to MySQL using ODBC.
227220
228221The driver name differs a bit depending on the platform and MyODBC version; examples are :
229222
230223``` pascal
231224 { properties of connection object, i.e. conn: TODBCConnection; }
232225 conn.Driver := 'MySQL'; // (Unix)
233226 conn.Driver := 'MySQL ODBC 3.51 Driver'; // (Windows)
234227 conn.Driver := 'MySQL Connector/ODBC v5'; // (Windows)
235228 // note: driver name doesn't need {} like it does in SQL Connection Strings elsewhere
236229```
237230
238231Other parameters (when not using a DSN) :
239232
240233``` pascal
241234 conn.UserName := 'myUsername';
242235 conn.Password := 'myPassword';
243236 // conn.Params (TStrings) set using .add method :
244237 conn.Params.Add('server=example.com');
245238 conn.Params.Add('port=3306');
246239 conn.Params.Add('database=myDatabase');
247240 conn.Params.Add('charset=utf8');
248241```
249242
250243### MS Access に接続する
251244
252245Please see [[MS Access]].
253246
254247### Connecting to Microsoft SQL Server
255248
256249> **Note:** Newer versions than FPC 2.6.0 and Lazarus versions using those support a direct SQLDB connection to MS SQL Server; see [[Lazarus Database Overview|MSSQL]]
257250
258251Use this www.devart.com/odbc/sqlserver/docs/using_odbc_driver.htm mssql odbc driver connection examples which include step by step tutorial and connection strings.
259252
260253See www.connectionstrings.com/sql-server#p7 for more details on connection strings.
261254
262255Microsoft SQL Server ODBC connection string example:
263256
264257``` pascal
265258Server=<server name>; // or <server name>\<instance name>
266259Database=<database name>;
267260```
268261
269262### Oracle に接続する
270263
271264Use this www.devart.com/odbc/oracle/docs/using_odbc_driver.htm odbc oracle connection guide to perform easy and painless connection to Oracle.
272265
273266Oracle ODBC connection string example:
274267
275268``` pascal
276269Login Prompt=False;
277270Data Source=ORCL;
278271User ID=scott;
279272Password=tiger
280273```
281274
282275### PosgreSQLに接続する
283276
284277Use this www.devart.com/odbc/postgresql/docs/using_odbc_driver.htm postgresql odbc connection guide to perform easy and painless connection to PostgreSQL database.
285278
286279PostgreSQL ODBC connection string example:
287280
288281``` pascal
289282Login Prompt=False;
290283Data Source=localhost;
291284User ID=postgres;
292285Password=postgres;
293286Database=postgres;
294287Schema=public
295288```
296289
297290### SQLite に接続する
298291
299292Use this www.devart.com/odbc/sqlite/docs/using_odbc_driver.htm sqlite odbc connection guide to perform easy and painless connection to SQLite database.
300293
301294SQLite ODBC connection string example:
302295
303296``` pascal
304297Login Prompt=False;
305298Database=c:\test.db3
306299```
307300
308301### Firebird に接続する
309302
310303Use this www.devart.com/odbc/firebird/docs/using_odbc_driver.htm firebird odbc connection guide to perform easy and painless connection to Firebird database.
311304
312305Firebird ODBC connection string example:
313306
314307``` pascal
315308Data Source=127.0.0.1;
316309User ID=sysdba;
317310Password=masterkey;
318311Client Library=fbclient.dll;
319312Database=c:\fbd.fdb
320313```
321314
322315#### Named DSN
323316
324317On Windows, you can use something like this with an existing named DSN:
325318
326319``` pascal
327320 { properties of connection object, i.e. conn: TODBCConnection; }
328321 conn.DatabaseName := '<Your_DSN_Name>';
329322 { Leave all other conn. properties empty }
330323```
331324
332325#### DSN-less
333326
334327Without a DSN:
335328
336329``` pascal
337330 { properties of connection object, i.e. conn: TODBCConnection; }
338331 conn.Driver := 'SQL Server';
339332 // note: driver name doesn't need {} like it does in SQL Connection Strings elsewhere
340333 { Leave Hostname and DatabaseName properties empty }
341334 { set conn.Params (TStrings) : }
342335 conn.Params.Add('Database=<yourdatabasename>');
343336 conn.Params.Add('Server=.\SQLEXPRESS');
344337 conn.Params.Add('Trusted_Connection=Yes'); // or 'Integrated Security=SSPI'
345338```
346339
347340Note: In this example, we connect to the local machine (server=.), on the instance SQLEXPRESS. You can use a hostname instead of ., and you can omit the instance to connect to the default instance. See MS documentation for details.
348341
349342Using a trusted connection (also known as integrated security or SSPI) means you login using your Windows user credentials. You can omit the Trusted_Connection line, but then you need to specify user ID (Uid=...) and password (Pwd=...).
350343
351344#### DSN example
352345
353346The example code below selects the rows from a MS SQL Server table called 'journal_entries' and displays all the values of column 'journal_entry' in a Memo control called Memo1.
354347
355348``` pascal
356349// uses ODBCConn, sqldb;
357350procedure TForm1.Button1Click(Sender: TObject);
358351var
359352 S: String;
360353 conn : TODBCConnection; // uses ODBCConn
361354 query: TSQLQuery; // uses sqldb
362355 transaction: TSQLTransaction; // uses sqldb
363356
364357begin
365358 conn := TODBCCOnnection.Create(nil);
366359 query := TSQLQuery.Create(nil);
367360 transaction := TSQLTransaction.Create(nil);
368361 try
369362 try
370363 conn.DatabaseName := 'sqlserverdsn'; {replace this with your DSN, if you use any}
371364 conn.UserName:= 'sa'; //replace with your user name
372365 conn.Password:= 'thepassword'; //replace with your password
373366 // You can override the properties defined in the DSN, e.g.
374367 //conn.Params.Add('Database=some_other_db');
375368
376369 conn.Transaction := transaction;
377370 query.DataBase := conn;
378371 { To avoid "could not retrieve primary key metadata":
379372 - either use query.UsePrimaryKeyAsKey:=false
380373 or (preferred)
381374 - query.PacketRecords to -1
382375 If you are using SQL Server, you could alternatively enable MARS in your SQL Native Client ODBC driver.
383376 See http://bugs.freepascal.org/view.php?id=13241 }
384377 query.PacketRecords:=-1; //retrieve all data at once
385378 query.SQL.Text := 'select journal_entry from journal_entries';
386379 query.Open;
387380 S := '';
388381 while not query.EOF do
389382 begin
390383 S := S + query.FieldByName('journal_entry').AsString + #13#10;
391384 query.Next;
392385 end;
393386 finally
394387 query.Free;
395388 conn.Free;
396389 transaction.Free;
397390 end;
398391 except
399392 on E: Exception do
400393 ShowMessage(E.message);
401394 end;
402395 Memo1.Text:= S;
403396end;
404397```
405398
406399In fact it is possible to use conn.Params for all parts of the connection string, if we modify the above - this time showing the SQL Server version in a pop-up message :
407400
408401``` pascal
409402 conn.Params.Add('Driver=SQL Server');
410403 conn.Params.Add('Server=OURBIGSERVER');
411404 conn.Params.Add('Database=TestDataBase');
412405 conn.Params.Add('Integrated Security=SSPI'); // use Windows logon credentials
413406 // no other conn. properties set, apart from conn.Transaction := transaction;
414407
415408 query.DataBase := conn;
416409 query.SQL.Text := 'select @@version';
417410 query.Open;
418411 showmessage(query.Fields[0].AsString); // Fields are numbered starting from 0
419412```
420413
421414Functions are supported - e.g. '`SELECT * FROM MyTestFunc()`' - but there may be issues with *Stored Procedures* that return recordsets. For example, if '`EXEC MyStoredProcedure`' returns data rows (verified in SQL Server Management Studio or with the *SqlCmd* DOS command: `C:\>sqlcmd -S MyServer -E -d MyDataBase -Q "exec MySP"`) then `query.open` will give an error : "Cannot open a non-select statement" (this may be fixed after FPC 2.6)
422415
423416To call a Stored Procedure or to issue other SQL commands that return no rowsets (CREATE,INSERT,UPDATE,DELETE), use `query.ExecSQL` instead of `query.Open`
424417
425418Stored Procedure parameters work, e.g. '`EXEC MySP @param1=23`'
426419
427420The use of *Parameters* is recommended to avoid *SQL injection vulnerabilities*. In this case the SQL statement isn't built with values directly, but a named placeholder is used instead - and then the parameter is specified separately. Using this method it isn't possible to inject malicious SQL commands into variable fields in an attempt to tamper with the database; the server then knows specifically what part of the statement is a value and what is command. For example, instead of building a WHERE clause directly with the value 95 (`'... WHERE field2>95'`), instead use a named parameter like 'testval' -
428421
429422``` pascal
430423 query.SQL.Text := 'select * from mytable where field2>:testval';
431424 query.Params.ParamByName('testval').AsString := '95';
432425 query.open;
433426```
434427
435428## ODBCデバッグとエラー
436429
437430### トランザクションの問題
438431
439432In Lazarus 1.2.4 (FPC 2.6.4), ODBC transaction behaviour has changed. This may cause previously working code to stop working.
440433
441434For a solution, see [[User Changes 2.6.4|User_Changes_2.6.4#TODBCConnection_.28odbcconn.29_No_longer_autocommit]]
442435
443436### エラーメッセージ
444437
445438Each ODBC API call also returns a succes code; if there is an error, more information can be retrieved using calls to `SQLGetDiagRec`. (For the sake of completeness: diagnostic records are also available when an API call returns `SQL_SUCCES_WITH_INFO`.)
446439
447440`TODBCConnection` checks the return code of each ODBC call and contructs an `EODBCException`. The `message` of this exception consists of:
448441
449442- a message identifying what the `TODBCConnection` was doing when the error occurred
450443- the return code of the ODBC API call (e.g. `SQL_ERROR`)
451444- a number of diagnostic records (obtained using `SQLGetDiagRec`); each record consists of three fields:
452445 - a 5-character error code identifying the error
453446 - a 'native error code'
454447 - a message describing the error
455448
456449#### Function sequence error
457450
458451If you get a 'Function sequence error' in the finalization section of the `ODBCConn` unit, then you probably did not properly clean up all you queries and connections.
459452
460453#### Rollback が効かない
461454
462455If you issue
463456
464457``` pascal
465458TSQLTransaction.Rollback
466459```
467460
468461commands but your SQL is committed regardless, your ODBC connection probably is in AUTOCOMMIT mode; see [[ODBCConn|the remarks about the AUTOCOMMIT connection parameter]] on how to correct this.
469462
470463### 追跡
471464
472465Most ODBC managers have a tracing option in which all ODBC API calls are logged to a trace log. This can be very useful for debugging an ODBC application. The ODBC Data Source Administration GUI of both Windows and unixODBC have a tab where you can configure the tracing option.
473466
474467Of course the trace log is mainly useful for developers that are familiar with the ODBC API, but it can help to identify the problem. Also, you can attach a trace log if you report a problem to the bug tracker.
475468
476469## 以下も参照のこと
477470
478471- [[MS Access]] Using ODBC to access Microsoft Access databases
479472
480473---
481474*From the Free Pascal wiki, page "ODBCConn/ja", as it stood on 2024-04-03 after a change by Ariben, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history