![background image](http://html1.mh-extra.com/html/oracle/5-0/5-0_reference-manual_16461962244.webp)
Common Questions and Problems When Using the C API
2224
You can always check whether the statement should have produced a nonempty result by calling
mysql_field_count()
. If
mysql_field_count()
returns zero, the result is empty and the
last query was a statement that does not return values (for example, an
INSERT
or a
DELETE
). If
mysql_field_count()
returns a nonzero value, the statement should have produced a nonempty
result. See the description of the
mysql_field_count()
function for an example.
You can test for an error by calling
mysql_error()
or
mysql_errno()
.
20.6.13.2. What Results You Can Get from a Query
In addition to the result set returned by a query, you can also get the following information:
•
mysql_affected_rows()
returns the number of rows affected by the last query when doing an
INSERT
,
UPDATE
, or
DELETE
.
For a fast re-create, use
TRUNCATE TABLE
.
•
mysql_num_rows()
returns the number of rows in a result set. With
mysql_store_result()
,
mysql_num_rows()
may be called as soon as
mysql_store_result()
returns. With
mysql_use_result()
,
mysql_num_rows()
may be called only after you have fetched all the
rows with
mysql_fetch_row()
.
•
mysql_insert_id()
returns the ID generated by the last query that inserted a row into a table with
an
AUTO_INCREMENT
index. See
Section 20.6.6.37, “
mysql_insert_id()
”
.
• Some queries (
LOAD DATA INFILE ...
,
INSERT INTO ... SELECT ...
,
UPDATE
)
return additional information. The result is returned by
mysql_info()
. See the description for
mysql_info()
for the format of the string that it returns.
mysql_info()
returns a
NULL
pointer if
there is no additional information.
20.6.13.3. How to Get the Unique ID for the Last Inserted Row
If you insert a record into a table that contains an
AUTO_INCREMENT
column, you can obtain the value
stored into that column by calling the
mysql_insert_id()
function.
You can check from your C applications whether a value was stored in an
AUTO_INCREMENT
column
by executing the following code (which assumes that you've checked that the statement succeeded). It
determines whether the query was an
INSERT
with an
AUTO_INCREMENT
index:
if ((result = mysql_store_result(&mysql)) == 0 &&
mysql_field_count(&mysql) == 0 &&
mysql_insert_id(&mysql) != 0)
{
used_id = mysql_insert_id(&mysql);
}
When a new
AUTO_INCREMENT
value has been generated, you can also obtain it by executing a
SELECT LAST_INSERT_ID()
statement with
mysql_query()
and retrieving the value from the
result set returned by the statement.
For
LAST_INSERT_ID()
[961]
, the most recently generated ID is maintained in the server on a per-
connection basis. It is not changed by another client. It is not even changed if you update another
AUTO_INCREMENT
column with a nonmagic value (that is, a value that is not
NULL
and not
0
). Using
LAST_INSERT_ID()
[961]
and
AUTO_INCREMENT
columns simultaneously from multiple clients is
perfectly valid. Each client will receive the last inserted ID for the last statement that client executed.
If you want to use the ID that was generated for one table and insert it into a second table, you can use
SQL statements like this:
INSERT INTO foo (auto,text)
VALUES(NULL,'text'); # generate ID by inserting NULL
INSERT INTO foo2 (id,text)
Содержание 5.0
Страница 1: ...MySQL 5 0 Reference Manual ...
Страница 18: ...xviii ...
Страница 60: ...40 ...
Страница 396: ...376 ...
Страница 578: ...558 ...
Страница 636: ...616 ...
Страница 844: ...824 ...
Страница 1234: ...1214 ...
Страница 1426: ...MySQL Proxy Scripting 1406 The following diagram shows an overview of the classes exposed by MySQL Proxy ...
Страница 1427: ...MySQL Proxy Scripting 1407 ...
Страница 1734: ...1714 ...
Страница 1752: ...1732 ...
Страница 1783: ...Configuring Connector ODBC 1763 ...
Страница 1793: ...Connector ODBC Examples 1773 ...
Страница 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Страница 1842: ...Connector Net Installation 1822 5 Once the installation has been completed click Finish to exit the installer ...
Страница 1864: ...Connector Net Visual Studio Integration 1844 Figure 20 24 Debug Stepping Figure 20 25 Function Stepping 1 of 2 ...
Страница 2850: ...2830 ...
Страница 2854: ...2834 ...
Страница 2928: ...2908 ...
Страница 3000: ...2980 ...
Страница 3122: ...3102 ...
Страница 3126: ...3106 ...
Страница 3174: ...3154 ...
Страница 3232: ...3212 ...