home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer) / NeXT_Developer-3.3.iso / NextLibrary / Documentation / Sybase / DBLIB / Section3 / bcp_bind.nr < prev    next >
Encoding:
Text File  |  1993-04-22  |  6.9 KB  |  215 lines

  1. .Na "bcp_bind"
  2. .mc |
  3. .Aa
  4. .Fu
  5. Bind data from a program variable to a \*S table.
  6. .Ih "bcp, bind data"
  7. .Sy
  8. .Sf "RETCODE bcp_bind (dbproc, varaddr, prefixlen, varlen,"
  9. .sp -1v
  10. .Sf "                  terminator, termlen, type, table_column)"
  11. .Sp "DBPROCESS" "*dbproc"
  12. .Sp "BYTE" "*varaddr"
  13. .Sp "int" "prefixlen"
  14. .Sp "DBINT" "varlen"
  15. .Sp "BYTE" "*terminator"
  16. .Sp "int" "termlen"
  17. .Sp "int" "type"
  18. .Sp "int" "table_column"
  19. .Co
  20. .Bl
  21. There may be times when you want to copy data directly from a
  22. program variable into a table in \*S, without having to
  23. first place the data in a host file or use the SQL INSERT command.
  24. The \f2bcp_bind()\f1 function is a fast and efficient way to do this.
  25. .Bl
  26. You must call \f2bcp_init()\f1 before calling this or any other bulk copy functions.
  27. .Bl
  28. There must be a separate \f2bcp_bind()\f1 call for every column in the
  29. \*S table into which you want to copy. After the necessary
  30. \f2bcp_bind()\f1 calls have been made, you then call \f2bcp_sendrow()\f1
  31. to send a row of data from your program variables to \*S.
  32. The table to be copied into is set by calling \f2bcp_init()\f1.
  33. .Bl
  34. Whenever you want \*S to checkpoint the rows already
  35. received, call \f2bcp_batch()\f1. For example, you may want to call \f2bcp_batch()\f1 once
  36. for every 1000 rows inserted, or at any other interval.
  37. .Bl
  38. When there are no more rows to be inserted, call \f2bcp_done()\f1. Failure
  39. to do so will result in an error.
  40. .Bl
  41. When using \f2bcp_bind()\f1, the host filename parameter
  42. used in the call to \f2bcp_init()\f1, \f2hfile\f1, must be set to NULL, and  the direction
  43. parameter, \f2direction\f1, must be set to DB_IN. 
  44. .Bl
  45. Control parameter settings, specified with \f2bcp_control()\f1, have no effect on
  46. \f2bcp_bind()\f1 row transfers.
  47. .Bl
  48. It is an error to call \f2bcp_columns()\f1 when using \f2bcp_bind()\f1.
  49. .Bl
  50. The following program fragment illustrates \f2bcp_bind()\f1:
  51. .SD
  52. .ta +4n +4n +4n +4n +4n +4n +4n
  53. .ne 5
  54. LOGINREC        *login;
  55. DBPROCESS       *dbproc;
  56. char            co_name[MAXNAME];
  57. DBINT           co_id;
  58. DBINT           rows_sent;
  59. DBBOOL            more_data;
  60. char            *terminator = "\et\et";
  61.  
  62. /* Initialize DB-Library. */
  63. if (dbinit() == FAIL)
  64.     exit(ERREXIT);
  65.  
  66. /* Install error-handler and message-handler. */
  67. dberrhandle(err_handler);
  68. dbmsghandle(msg_handler);
  69.  
  70. /* Open a DBPROCESS. */
  71. login = dblogin();
  72. BCP_SETL(login, TRUE);
  73. dbproc = dbopen(login, NULL);
  74.  
  75. /* Initialize bcp. */
  76. if (bcp_init(dbproc, "comdb..accounts_info", NULL, NULL, DB_IN) == FAIL)
  77.     exit(ERREXIT);
  78.  
  79. /* Bind program variables to table columns. */
  80. if (bcp_bind(dbproc, &co_id, 0, -1, (BYTE *)NULL, 0, 0, 1) == FAIL)
  81. {
  82.     fprintf(stderr, "bcp_bind, column 1, failed.\en");
  83.     exit(ERREXIT);
  84. }
  85.  
  86. if (bcp_bind
  87.     (dbproc, co_name, 0, -1, (BYTE *)terminator, strlen(terminator), 0, 2)
  88.         == FAIL)
  89. {
  90.     fprintf(stderr, "bcp_bind, column 2, failed.\en");
  91.     exit(ERREXIT);
  92. }
  93.  
  94. while (TRUE)
  95. {
  96.     /* Process/retrieve program data. */
  97.     more_data = getdata(&co_id, co_name);
  98.     
  99.     if (more_data == FALSE)
  100.         break;
  101.  
  102.     /* Send the data. */
  103.     if (bcp_sendrow(dbproc) == FAIL)
  104.         exit(ERREXIT);
  105. }
  106.  
  107. /* Terminate the bulk copy operation. */
  108. if ((rows_sent = bcp_done(dbproc)) == -1)
  109.     printf("Bulk-copy unsuccessful.\en");
  110. else
  111.     printf("%ld rows copied.\en", rows_sent);
  112. .ED
  113. .Bl
  114. For information on the \f2bcp\f1 utility program, see its manual page
  115. in the \f2Commands Reference\f1.
  116. .Bz
  117. .Pa
  118. .Pi dbproc
  119. A pointer to the DBPROCESS structure that provides the connection
  120. for a particular front-end/\*S process.  It contains all the
  121. information that \*L uses to manage communications and data between the
  122. front end and \*S.
  123. .Pi varaddr
  124. The address of the program variable from which the data
  125. will be copied.
  126. If \f2type\f1 is SYBTEXT or SYBIMAGE, \f2varaddr\f1 can be NULL.
  127. A NULL \f2varaddr\f1 indicates 
  128. that text and image values will be
  129. sent to \*S in chunks by \f2bcp_moretext()\f1, rather than all
  130. at once by \f2bcp_sendrow()\f1.
  131. .Pi prefixlen
  132. The length, in bytes, of any length prefix this column may
  133. have. 
  134. For example, strings in some non-C programming languages are made up of a one-byte
  135. length prefix, followed by the string data itself.
  136. If the data does not have a length prefix, set \f2prefixlen\f1 to 0.
  137. .Pi varlen
  138. The length of the data in the program variable, \f2not\f1 including
  139. the length of any length prefix and/or terminator. 
  140. Setting \f2varlen\f1 to 0 signifies that the data is null.
  141. Setting \f2varlen\f1 to -1 indicates that the system should ignore
  142. this parameter.
  143. .sp 0.5v
  144. For fixed-length datatypes, such as integers, the datatype itself
  145. indicates to the system the length of the data.
  146. Therefore, for fixed-length datatypes, \f2varlen\f1 must always
  147. be -1, except when the data is null, in which case \f2varlen\f1
  148. must be 0.
  149. .sp 0.5v
  150. For character, text, binary, and image data, \f2varlen\f1 can be
  151. -1, 0, or some positive value.
  152. If \f2varlen\f1 is -1, the system will use either a length prefix
  153. or a terminator sequence to determine the length.
  154. (If both are supplied, the system will use the one that results in 
  155. the shortest amount of data being copied.)
  156. If \f2varlen\f1 is -1 and neither a prefix length nor a 
  157. terminator sequence is specified, the system will return an error message.
  158. If \f2varlen\f1 is 0, the system assumes the data is null.
  159. If \f2varlen\f1 is some positive value, the system uses \f2varlen\f1 as
  160. the data length.
  161. However, if, in addition to a positive \f2varlen\f1,
  162. a prefix length and/or terminator sequence is provided,
  163. the system determines the data length by using the method 
  164. that results in the shortest amount of data being copied.
  165. .Pi terminator
  166. A pointer to the byte pattern, if any, that marks the end of this program variable.
  167. For example, C strings usually have a one-byte terminator, whose value is 0.
  168. If there is no terminator for the variable, set \f2terminator\f1 to NULL.
  169. .sp 0.5v
  170. If you want to designate the C null terminator as the program variable terminator,
  171. the simplest way is to
  172. use an empty string ("") as \f2terminator\f1 and set \f2termlen\f1 to 1, since
  173. the null terminator constitutes a single byte.
  174. For instance,
  175. the second \f2bcp_bind()\f1 call in the previous example uses two tabs
  176. as the program variable terminator.
  177. It could be rewritten to use a C null terminator instead, as follows:
  178. .SD
  179. .in +3n
  180. (bcp_bind (dbproc, co_name, 0, -1, "", 1, 0, 2)
  181. .in -3n
  182. .ED
  183. .Pi termlen
  184. The length of this program variable's terminator, if any.
  185. If there is no terminator for the variable, set \f2termlen\f1 to 0.
  186. .Pi type
  187. The datatype of your program variable, expressed as a \*S datatype. 
  188. The data in the
  189. program variable will be automatically converted to the
  190. type of the database column. 
  191. If this parameter is 0, no conversion will be performed. 
  192. See the \f2dbconvert()\f1 manual page for a list of
  193. supported conversions. 
  194. That manual page also contains a list of \*S datatypes.
  195. .Pi table_column 
  196. The column in the database table to which the data will
  197. be copied. Column numbers start at 1.
  198. .in -.375i
  199. .Re
  200. .br
  201. SUCCEED or FAIL.
  202. .Sa
  203. bcp_batch,
  204. bcp_colfmt,
  205. bcp_collen,
  206. bcp_colptr,
  207. bcp_columns,
  208. bcp_control,
  209. bcp_done,
  210. bcp_exec,
  211. bcp_init,
  212. bcp_moretext,
  213. bcp_sendrow
  214. .mc
  215.