免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3211 | 回复: 4
打印 上一主题 下一主题

sco informix 里怎样使用detach [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-25 16:39 |只看该作者 |倒序浏览
父进程打开了数据库,fork()后子进程,为什么不能操作数据库,停在哪里,据说要用detach ,怎么用呢?

论坛徽章:
11
金牛座
日期:2015-03-19 16:56:22数据库技术版块每日发帖之星
日期:2016-08-02 06:20:00数据库技术版块每日发帖之星
日期:2016-04-24 06:20:00数据库技术版块每日发帖之星
日期:2016-04-13 06:20:00IT运维版块每日发帖之星
日期:2016-04-13 06:20:00数据库技术版块每日发帖之星
日期:2016-02-03 06:20:00数据库技术版块每日发帖之星
日期:2015-08-06 06:20:00季节之章:春
日期:2015-03-27 15:54:57羊年新春福章
日期:2015-03-27 15:54:37戌狗
日期:2015-03-19 16:56:41数据库技术版块每日发帖之星
日期:2016-08-18 06:20:00
2 [报告]
发表于 2008-11-25 16:48 |只看该作者
detach 跟..............没看明白~~

论坛徽章:
0
3 [报告]
发表于 2008-11-25 17:02 |只看该作者
也就是多进程怎么操作数据库.

论坛徽章:
0
4 [报告]
发表于 2008-11-25 18:36 |只看该作者
sqldetach ()
是个函数

论坛徽章:
0
5 [报告]
发表于 2008-11-27 14:28 |只看该作者
  1. informix 自带例子.......................................

  2. $INFORMIXDIR/demo/esqlc/sqldetach.ec


  3. /****************************************************************************
  4. *
  5. *                               IBM INC.
  6. *
  7. *                           PROPRIETARY DATA
  8. *
  9. * Licensed Material - Property Of IBM
  10. *
  11. * "Restricted Materials of IBM"
  12. *
  13. * IBM Informix Client SDK
  14. *
  15. * (c)  Copyright IBM Corporation 2002. All rights reserved.
  16. *
  17. ****************************************************************************
  18. */

  19. main()
  20. {
  21.     EXEC SQL BEGIN DECLARE SECTION;
  22.         mint pa;
  23.     EXEC SQL END DECLARE SECTION;

  24.     printf("SQLDETACH Sample ESQL Program running.\n\n");

  25.     printf("Beginning execution of parent process.\n\n");
  26.     printf("Connecting to default server...\n");
  27.     EXEC SQL connect to default;
  28.     chk("CONNECT");
  29.     printf("\n");

  30.     printf("Creating database 'aa'...\n");
  31.     EXEC SQL create database aa;
  32.     chk("CREATE DATABASE");
  33.     printf("\n");

  34.     printf("Creating table 'tab1'...\n");
  35.     EXEC SQL create table tab1 (a integer);
  36.     chk("CREATE TABLE");
  37.     printf("\n");

  38.     printf("Inserting 4 rows into 'tab1'...\n");
  39.     EXEC SQL insert into tab1 values (1);
  40.     chk("INSERT #1");
  41.     EXEC SQL insert into tab1 values (2);
  42.     chk("INSERT #2");
  43.     EXEC SQL insert into tab1 values (3);
  44.     chk("INSERT #3");
  45.     EXEC SQL insert into tab1 values (4);
  46.     chk("INSERT #4");
  47.     printf("\n");

  48.     printf("Selecting rows from 'tab1' table...\n");
  49.     EXEC SQL declare c cursor for select * from tab1;
  50.     chk("DECLARE");
  51.     EXEC SQL open c;
  52.     chk("OPEN");

  53.     printf("\nForking child process...\n");
  54.     fork_child();

  55.     printf("\nFetching row from cursor 'c'...\n");
  56.     EXEC SQL fetch c into $pa;
  57.     chk("Parent FETCH");
  58.     if (sqlca.sqlcode == 0)
  59.         printf("Value selected from 'c' = %d.\n", pa);      
  60.     printf("\n");

  61.     printf("Cleaning up...\n");
  62.     EXEC SQL close database;
  63.     chk("CLOSE DATABASE");
  64.     EXEC SQL drop database aa;
  65.     chk("DROP DATABASE");
  66.     EXEC SQL disconnect all;
  67.     chk("DISCONNECT");

  68.     printf("\nEnding execution of parent process.\n");
  69.     printf("\nSQLDETACH Sample Program over.\n\n");
  70. }

  71. fork_child()
  72. {
  73.     mint rc, status, pid;

  74.     EXEC SQL BEGIN DECLARE SECTION;
  75.         mint cnt, ca;
  76.     EXEC SQL END DECLARE SECTION;

  77.     pid = fork();
  78.     if (pid < 0)
  79.         printf("can't fork child.\n");

  80.     else if (pid == 0)
  81.         {
  82.         printf("\n**********************************************\n");
  83.         printf("* Beginning execution of child process.\n");
  84.         rc = sqldetach();
  85.         printf("* sqldetach() call returns %d.\n", rc);

  86.         /* Verify that the child is not longer using the parent's
  87.          * connection and has not inherited the parent's connection
  88.          * environment.
  89.          */
  90.         printf("* Trying to fetch row from cursor 'c'...\n");
  91.         EXEC SQL fetch c into $ca;
  92.         chk("* Child FETCH");
  93.         if (sqlca.sqlcode == 0)
  94.             printf("* Value from 'c' = %d.\n", ca);

  95.         /* startup a connection for the child, since
  96.          * it doesn't have one.
  97.          */
  98.         printf("\n* Establish a connection, since child doesn't have one\n");
  99.         printf("* Connecting to database 'aa'...\n");
  100.         EXEC SQL connect to 'aa';
  101.         chk("* CONNECT");
  102.         printf("* \n");

  103.         printf("* Determining number of rows in 'tab1'...\n");
  104.         EXEC SQL select count(*) into $cnt from tab1;
  105.         chk("* SELECT");
  106.         if (sqlca.sqlcode == 0)
  107.             printf("* Number of entries in 'tab1' =  %d.\n", cnt);
  108.         printf("* \n");

  109.         printf("* Disconnecting from 'aa' database...\n");
  110.         EXEC SQL disconnect current;
  111.         chk("* DISCONNECT");
  112.         printf("* \n");
  113.         printf("* Ending execution of child process.\n");
  114.         printf("**********************************************\n");
  115.         
  116.         exit();
  117.         }

  118.     /* wait for child process to finish */
  119.     while ((rc = wait(&status)) != pid && rc != -1);

  120. }

  121. chk(s)
  122. char *s;
  123. {
  124.     mint msglen;
  125.     char buf1[200], buf2[200];

  126.     if (SQLCODE == 0)
  127.         {
  128.         printf("%s was successful\n", s);
  129.         return;
  130.         }
  131.     printf("\n%s:\n", s);
  132.     if (SQLCODE)
  133.         {
  134.         printf("\tSQLCODE =  %6d: ", SQLCODE);
  135.         rgetlmsg(SQLCODE, buf1, sizeof(buf1), &msglen);
  136.         sprintf(buf2, buf1, sqlca.sqlerrm);
  137.         printf(buf2);
  138.         if (sqlca.sqlerrd[1])
  139.             {
  140.             printf("\tISAM Error =  %6hd: ", sqlca.sqlerrd[1]);
  141.             rgetlmsg(sqlca.sqlerrd[1], buf1, sizeof(buf1), &msglen);
  142.             sprintf(buf2, buf1, sqlca.sqlerrm);
  143.             printf(buf2);
  144.             }
  145.         }
  146. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP