笔记

来源:互联网 发布:单片机脉冲计数程序 编辑:程序博客网 时间:2024/06/11 20:20
 
  1. 2008-9月
  2. 2008-9-22 Monday
  3. 1. sql语句
  4. select * from student where sno in ('001','002')
  5. select * from student where sno in (001,002)
  6. 效果是相同的
  7. 2. ExecSql和Query的区别
  8. baseConn->ExecSql(sql); Query
  9. 只执行SQL,无返回记录集
  10. c_Records.Query(szSql);
  11. 执行SQL,且返回记录集,可用于内容的导出Query
  12. 2008-9-23 Tuesday
  13. 1. 统计元组数
  14.     char szSql[256];
  15.     CRecords c_Records(*baseConn, 1);
  16.     sprintf(szSql, 
  17.       "select count(*) from %s ", tableName);
  18.     j = c_Records.Query(szSql);
  19.     if (j > 0)
  20.     {
  21.         countNum = (int)c_Records.Field(0).Float();
  22.     }
  23. 2. Date数据
  24. TO_CHAR(optdate,'yyyymmddhh24miss')
  25. TO_DATE(‘20080923’, ‘yyyymmdd’)
  26. Hh24  24小时制
  27. Hh12  12小时制
  28. Mi      分
  29. Ss      秒
  30. 3. strncpy
  31.     char* a = "ahelloworld";
  32.     char b[10];
  33.     strncpy(b, a, 4);
  34.     
  35.     cout << b << endl;
  36. 执行时出错,第5个没有赋值,就导致乱码出现
  37. 4. g_struGlobalParam.szRegion
  38. void SetGlobalParam()
  39. 函数里进行了赋值
  40. Main()函数中的long InitialProc()调用void SetGlobalParam()
  41. 2008-9-24 Wednesday
  42. 1. 连接问题
  43. VC中的连接有时出现问题,注意添加,并且书写要正确
  44. 在使用时,函数打不开,自己去添加头文件试试
  45. 2. 类型转换
  46. "WHERE BILLING_CYCLE_ID IN(%s,%s,%s)", m_cym1, m_cym2, m_cym3,
  47. BILLING_CYCLE_ID为整型
  48. 因为整个句子会被转换为一个字符串,字符型m_cym1会自动表现为整型;
  49. 2008-9-25 Thursday
  50. 1. 编译问题
  51. 编译时出现问题,重新编译之后,执行的还是上次的程序
  52. 解决:build,可以写个错误等编译提示后再改正
  53. 还有:先build,再工具栏build->Rebuild All
  54. 可以设置个输出标记进行判断
  55. 2. 程序问题
  56. 用   j = m_CRecord.Query(sql);可以
  57. 用   j = amsConn->ExecSql(sql);出现问题
  58. 用   j = amsConn->ExecSql("create table ha(aa char(10))");去作替换,发现还是可以执行的
  59. 在ams1中创建了表
  60. 3. 出现问题
  61.     sprintf(sql, "CREATE TABLE %s NOLOGGING AS "
  62.             " SELECT DISTINCT KEY_NUM FROM ACCT_ITEM_%s "
  63.             " WHERE BILLING_CYCLE_ID IN(%s,%s,%s) "
  64.             " MINUS "
  65.             " (select bill_id from ins_prod_%s "
  66.             " union all "
  67.             " select bill_id from ins_prod_move_%s) "//
  68.             , tableName, g_struGlobalParam.szRegion, m_cym1, m_cym2, m_cym3, 
  69.             g_struGlobalParam.szRegion, g_struGlobalParam.szRegion);
  70. 在分行写SQL语句的时候,必须要在分行处加上空格,不然上下行会连在一起,造成语句的错误;
  71. 4. 空指针的检查
  72.     if ( ymonth == NULL || strlen(ymonth) != 6 )//先检查是否为NULL
  73.     {
  74.         return -1;
  75.     }
  76. Char* p;
  77. Strlen(p);  会出错
  78. 2008-9-26 Friday
  79. 1. date类型的使用
  80. create table stime(aa date)
  81. insert into stime values(to_date('20080926',  'yyyymmdd'))
  82. select * from stime
  83. 2008-9-27 Saturday
  84. 1. cvs的提交使用
  85. 选中文件->右键->cvs add -> 选中文件->右键->commit
  86. 2. 删除表中的数据
  87. delete from tableName
  88. 这个语句用来清楚表中的内容,但表还是存在的
  89. insert into Stu values('02''aa',null, 20, NULL);
  90. 2008-9-28 Sunday
  91. 1. 数据库导入数据
  92. 上网查询的方法  手工操作 可以先从txt等文件中导入到Excel中
  93. 使用PL/SQL Developer工具。
  94. 在单个文件不大的情况下(少于100000行),并且目的表结构已经存在的情况下。
  95. 操作步骤如下:
  96. 1 可以全选excel数据复制,
  97. 2 在PL/SQL Developer的sql window里输入select * from tablename for update;
  98. 3 按F8执行;
  99. 4 打开锁, 再按一下加号. 鼠标点到第一列的列头,使全列成选中状态,然后粘贴,最后commit提 
  100. 交即可。
  101. Excel导入数据,直接在工具栏选择->数据->导入数据,
  102. 注意在导入的时候可以对每列进行设置,如果要在单元格中显示001,单元格格式中应该设置为文本
  103. 2. 导入数据程序
  104. 从文件中读取每个字段,依次对表中的各字段进行赋值
  105. 用循环去依次读取赋值
  106. 要注意类型
  107. 方案:用    j = amsConn->ExecSql(sql); 来直接执行插入语句
  108. 而不用表类的操作
  109. 3. 长字符串转换为数字_atoi64
  110.     char buf[13] = "138999999999";
  111.     long double a;
  112. //  a = 138999999999;
  113.     a =  _atoi64(buf);
  114.     cout << a << endl;
  115. _atoi64  在msdn中查找得到
  116. 这样可以解决转换问题
  117. 不过又有一个新的问题,精度 结果为1.39e+010
  118. 4. Excel最大页面行数
  119. 65536
  120. 2008-9-29 Monday
  121. 1. 导入程序完成过程
  122. 开始的想法是用表类去进行插入操作,后来发现amsConn->ExecSql(str);这种方式更好,直接执行语句,不用去管类型转换,方便实用
  123.     long j = Login("ORACLE");也不可少
  124.     DbFactory.Initial();// 不可忘记
  125.     j = DbFactory.GetConnection("ams""1", amsConn);
  126.     CFileReader freader("F://INCREACCTBOOK_20080831_NULL.DAT"","" ");
  127.     freader.Open();// 不可忘记
  128. long InsertValues()
  129. {
  130.     Connection* amsConn = NULL;
  131.     long j = DbFactory.GetConnection(DB_INDEX_AMS, "2", amsConn);//
  132.     if (j <= 0)
  133.     {
  134.         cout << amsConn->GetMsg() << endl;
  135.         return -1;
  136.     }
  137.     CFileReader freader("F://INCREACCTBOOK_20080831_NULL.DAT"","" ");
  138.     freader.Open();//
  139.     char str[1024];
  140.     do {
  141. sprintf(str, "insert into ACCT_BALANCE_old_g(ACCTID, PAYSN, ACCTBOOKITM, BALANCE, "
  142.         " EFFDATE, EXPDATE) "
  143.         " values('%s','%s', '%s', %s, TO_DATE('%s', 'yyyymmdd'),"
  144.         "TO_DATE('%s', 'yyyymmdd') )",
  145.         freader.GetField(0), freader.GetField(2), freader.GetField(3), 
  146.         freader.GetField(4), freader.GetField(6), freader.GetField(7));
  147.     j = amsConn->ExecSql(str);
  148.         if (j < 1)
  149.         {
  150.             cout << amsConn->GetMsg() ;
  151.             return -1;
  152.         }
  153.     } while(freader.Next() > 0);
  154.     cout << "ok" << endl;
  155.     freader.Close();
  156.     DbFactory.FreeConnection(amsConn);
  157.     return 1;
  158. }
  159. int main()
  160. {
  161.     long j = Login("ORACLE");
  162.     DbFactory.Initial();
  163.     InsertValues();
  164.     Logout();
  165.     return 0;
  166. }
  167. 2. 导入时间测试
  168. 用amsConn->ExecSql(str);方式 插入5543行记录用时13.391秒
  169. 用   CRecords cRecord(*amsConn, 1);
  170.         cRecord.Query(str);
  171. 方式,插入5543行记录用时26.61秒
  172. 从数据库中获取时间
  173.   c_Time.GetSystemTime(连接)
  174. 2008-9-30 Tuesday
  175. 1. select 1的用法
  176. select 1 from  tableName where id = 1
  177. 返回n条记录的1,n是通过条件筛选后的记录数
  178. 用法,如:
  179. SELECT KEY_NUM FROM PAYMENT_A_200808 a  
  180. where opt_code in ('GJFQ','GJFP') and amount>50000 
  181. and not exists 
  182. (select 1 from acct_item_his_A_200808 
  183. where product_instance_id = a.product_instance_id)
  184. 用来判断子查询结果是否为空
  185. 2. 查询语句
  186. 请用SQL语句写出: 
  187. 表tblDept(ID,DeptName) 
  188. 表tblSalary(ID,DeptID,Name,Salary) 
  189. 超过半数的部门员工工资大于3000元的部门
  190. select * from tblDept 
  191. where id in(
  192.     select dptid 
  193.     from 
  194.       (select dptid,count(*) cntemp from tblsalary where Salary>3000 group by dptid)) a,
  195.        (select dptid,count(*) cntemp from tblsalary group by dptid) b
  196.     where a.dptid=b.dptid and a.cntemp*2>b.cntemp
  197.         )
  198. select DeptName from (
  199. select DeptID,count(*) as c1,(select count(*) from tblSalary where DeptID=s.DeptID and Salary>=3000 ) as c2
  200. from tblSalary as s 
  201. group by DeptID 
  202. ) a join tblDept on id=DeptID
  203. where c2>c1/2
  204. SQL语句中用--来注释单行语句
  205. 3. 读文件操作
  206. 在导入数据到数据库的操作中,第一步就是从文件中读取数据,采用了调用类方法的方式
  207.     char buf[255] = "";
  208.     FILE* file = fopen("D://to//station_name.txt""rb");
  209.     if (file == NULL)
  210.     {
  211.         return;
  212.     }
  213.     int i = 0;
  214.     int lenth = 0;
  215.     while ( i < stationNum )
  216.     {
  217.         fgets(buf,255,file);//一行行的去读取
  218.         strcpy(stationName[i], buf);
  219.         lenth = strlen(stationName[i]);
  220.         if(lenth < 2)
  221.         {
  222.             return;
  223.         }
  224.         stationName[i][lenth - 2] = '/0';//13,10, -2;
  225.         i++;
  226.     }
  227. fclose(file);
  228. 上面的程序有问题,因为最后一行很可能没有回车换行符,要进行判断,还有对fgets()要进行返回值的检查。
  229. feof(file) 判断是否结束
  230. 2008-10月 第1周
  231. 2008-10-1 Wednesday
  232. 1. SQL语句NULL
  233. 提取字符的后5位
  234. select substr('5000123456',-5) str from dual;
  235. 1.NULL 使用详解 
  236.     常常会有人问到,什么是NULL?顾名思义,NULL就是空,ORACLE中以及其他的数据库中,含有空值的表的列的长度为零。ORACLE允许任何一种数据类型的字段为空,除了以下两种情况:
  237.     1、定义该列为主键字段(primary key);
  238.     2、定义该列时已显式的加了 NOT NULL 的限制条件的字段。
  239. 1.1.具体说明:
  240.     1、等价于没有任何值、是未知数;
  241.     2、NULL与0、空字符串、空格都不同;
  242.     3、对空值做加、减、乘、除等运算操作,结果仍为空;
  243.     4、NULL的处理使用NVL函数;
  244.     5、查询、比较时使用关键字用“is null”和“is not null”;
  245.     6、空值不能被索引,所以查询时有些符合条件的数据可能查不出来,比方在count(*)中,用nvl(列名,0)处理后再查;
  246. 7、排序时比其他数据都大(索引默认是降序排列,小→大),所以NULL值总是排在最后。
  247. 1.2.使用方法举例: 
  248. SQL> select 1 from dual where null=null;
  249. 没有查到记录
  250. SQL> select 1 from dual where null='';
  251. 没有查到记录
  252. SQL> select 1 from dual where ''=''
  253. 没有查到记录
  254. SQL> select 1 from dual where null is null;
  255.         1
  256. ---------
  257.         1
  258. SQL> select 1 from dual where nvl(null,0)=nvl(null,0);
  259.         1
  260. ---------
  261.         1
  262. --对空值做加、减、乘、除等运算操作,结果仍为空。
  263. SQL> select 1+null from dual;
  264. SQL> select 1-null from dual;
  265. SQL> select 1*null from dual;
  266. SQL> select 1/null from dual;
  267. 查询到一个记录.
  268. 1.3.设置某些列为空值
  269. update table1 set col1=NULL where col1 is not null;
  270. 熟练使用Oracle的空值用法,熟悉它的约定,以确保查出的结果OK。
  271. update  student set age = 21 where age is null
  272. 对空值进行设置
  273. 2.Dual伪列
  274. 含义解释: 
  275. Dual 是 Oracle中的一个实际存在的表,任何用户均可读取,常用在没有目标表的Select语句块中。
  276. 2.1.使用方法: 
  277. --查看当前连接用户
  278. SQL> select user from dual;
  279. USER
  280. ------------------------------
  281. SYSTEM
  282. --查看当前日期、时间
  283. SQL> select sysdate from dual;
  284. SYSDATE
  285. ----------
  286. 18-4月 -03
  287. SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
  288. TO_CHAR(SYSDATE,'YY
  289. -------------------
  290. 2003-04-18 22:37:56
  291.   --当作计算器用
  292. SQL> select 1+2 from dual;
  293.        1+2
  294. ----------
  295.          3
  296. --查看序列值
  297. SQL> create sequence aaa increment by 1 start with 1;
  298. SQL> select aaa.nextval from dual;
  299.        NEXTVAL
  300. ----------
  301.          1
  302.     SQL> select aaa.currval from dual;
  303.    CURRVAL
  304. ----------
  305.          1
  306. 2008-10-2 Thursday
  307. 1. flush
  308. Flush清理内存缓冲区
  309. 2008-10-3 Friday
  310. 1. fgets
  311.   函数名: fgets
  312.   功 能: 从流中读取一字符串
  313.   用 法: char *fgets(char *stringint n, FILE *stream);
  314. 形参注释:*string结果数据的首地址;n-1:一次读入数据块的长度,其默认值为1k,即1024;stream文件指针
  315. fgets函数fgets函数用来从文件中读入字符串。fgets函数的调用形式如下:fgets(str,n,fp);此处,fp是文件指针;str是存放在字符串的起始地址;n是一个int类型变量。函数的功能是从fp所指文件中读入n-1个字符放入str为起始地址的空间内;如果在未读满n-1个字符之时,已读到一个换行符或一个EOF(文件结束标志),则结束本次读操作,读入的字符串中最后包含读到的换行符。因此,确切地说,调用fgets函数时,最多只能读入n-1个字符。读入结束后,系统将自动在最后加'/0',并以str作为函数值返回。
  316. Stream指针自动移位
  317. 2. PL/SQL数据导出操作
  318. select * from acct_balance_a  得到结果
  319. 选中所有的查询结果
  320. 右键->Export Results  
  321. 可以选csv file 方式,导出文件,在把后缀名改为txt就可以得到用逗号分格的字段
  322. 注意:要选所有的行
  323. 3. 查询所有的表名
  324. select * from user_tables;
  325. select table_name from user_tables;
  326. user_tables 是查看当前用户所拥有的表
  327. all_tables 是当前用户和他可以访问的表
  328. 2008-10-4 Saturday
  329. 1. 空值函数NVL
  330. 对NULL值上进行任何算术操作,结果都是NULL
  331. NVL函数提供了对空值运算的正确方法
  332. NVL(e1, e2), 当 e1是NULL时,返回e2,否则返回e1
  333. insert into student(sno) values('004')
  334. select nvl(sname, 'n') from student
  335. 2. 一道查询语句例题
  336. Csdn上看到的
  337. 有一表结构如下: 
  338. table1 
  339. gsdm    gsmc         
  340. 1100    汉口分公司    
  341. 1200  武昌分公司     
  342. 1300  汉阳分公司 
  343. select gsdm,gsmc from table1 where gsdm='1400' 查询没有结果,而我希望看到的结果是 
  344. gsdm    gsmc 
  345. 1400     
  346. select a.gsdm,b.gsmc
  347. from (
  348. select '1400'  gsdm from dual) a,
  349. (select gsdm,gsmc from table1 where gsdm='1400') b
  350. where a.gsdm=b.gsdm(+)
  351. 2008-10-5 Sunday
  352. 1. PL/SQL连接本地
  353. Database XE
  354. Connect as SYSDBA
  355. Username, Password 都为空
  356. 2. 找中位数
  357. Csdn上看到的
  358. 两个数组x[]、y[],长度为n,而且都经过排序从小到大排列,请编写C程序求出两个数组合并后(仍然从小到大排列)的中位数,要求比较次数少于n。
  359. 方法:分别定位到x,y的中间的数字,从这里开始进行比较,向左向右
  360. 这个题目看上去虽然没有什么实用性,但是也能够体现出一些技巧,对数组有序这个条件的充分利用
  361. 明天计划:实现连接数据库
  362. 2008-10月 第2周
  363. 2008-10-6 Monday
  364. 1. 实现VC和SQL Server数据库的连接
  365. 1. 创建ODBC数据源
  366. 控制面板->管理工具->数据源  
  367. 用户DSN选项卡  添加  选择SQL Server  完成
  368. 数据源名:MyDataBase
  369. 选择服务器:USASUN (机器名)
  370. 还要设置要连接的数据源
  371. 设置登录用户名u1,口令123
  372. 如果没有,可以先在SQL Server中建个用户
  373. 这样,在ODBC数据源中可以看到新建的数据源
  374. 2. 在VC中连接,使用CDatabase类,要添加头文件#include <afxdb.h>
  375.     CDatabase m_dataBase;
  376.     m_dataBase.Open(_T("MyDataBase"), FALSE, FALSE, _T("ODBC;UID=u1;PSW=123"), FALSE);
  377.     CString sql(_T("insert into student values('Jam','009','男','Math')"));
  378.     m_dataBase.ExecuteSQL(sql);
  379. 这样就可以对数据源中的数据进行操作了
  380. 2008-10-7 Tuesday
  381. 1. 数据库授权操作
  382. 回顾
  383. 在 创建用户 时,直接在 安全性  登录   中 点击右键, 选择 新建登录 即可;
  384. 之后,进行授权操作
  385. grant select 
  386. on S
  387. to u2
  388. 注意,加了table反而是错的
  389. grant select 
  390. on table S
  391. to u2
  392. 2. 指针问题
  393. struct Node* insert(struct Node* L, struct Node* p)
  394. {
  395.     if(L == NULL)   
  396.     {
  397.        L = p;
  398.        L->next = NULL;
  399.     }
  400.     else
  401.     {
  402.        p->next = L;
  403.        L = p;
  404.     }
  405.     return L;
  406. }    
  407. 这里用到了返回指针的方法,参数就可以不用引用
  408. 3. decode函数
  409. DECODE函数相当于一条件语句(IF).它将输入数值与函数中的参数列表相比较,根据输入值返回一个对应值。函数的参数列表是由若干数值及其对应结果值组成的若干序偶形式。当然,如果未能与任何一个实参序偶匹配成功,则函数也有默认的返回值。区别于SQL的其它函数,DECODE函数还能识别和操作空值. 
  410. 其具体的语法格式如下: 
  411. DECODE(input_value,value,result[,value,result…][,default_result]); 
  412. 其中: 
  413. input_value 试图处理的数值。DECODE函数将该数值与一系列的序偶相比较,以决定最后的返回结果 
  414. value 是一组成序偶的数值。如果输入数值与之匹配成功,则相应的结果将被返回。对应一个空的返回值,可以使用关键字NULL于之对应 
  415. result 是一组成序偶的结果值 
  416. default_result 未能与任何一序偶匹配成功时,函数返回的默认值 
  417. 下面的例子说明了,如何读取用户CHECKUP表SEAPARK中的BLOOD_TEST_FLAG列下的项目,作为DECODE函数的实参支持值。 
  418. SELECT checkup_type, 
  419. DECODE(blood_test_flag,’Y’,’Yes’,’N’,’No’,NULL,’None’,’Invalid’) 
  420. FROM checkup;
  421. --建表
  422. create table stud
  423. (
  424. sid varchar2(10),
  425. kcbm varchar2(10),
  426. cj int
  427. );
  428. --插入测试数据
  429. insert into stud values('1','语文',80);
  430. insert into stud values('2','数学',90);
  431. insert into stud values('3','英语',100);
  432. commit;
  433. --创建视图,decode用法
  434. create or replace view cjd as
  435. select sid,
  436. decode(kcbm,'语文',cj,0) 语文,
  437. decode(kcbm,'数学',cj,0) 数学,
  438. decode(kcbm,'英语',cj,0) 英语
  439. from stud
  440. order by sid;
  441. --显示数据
  442. select * from cjd;
  443. SID              语文       数学       英语                                     
  444. ---------- ---------- ---------- ----------                                     
  445. 1                  80          0          0                                     
  446. 2                   0         90          0                                     
  447. 3                   0          0        100    
  448. http://topic.csdn.net/u/20081007/11/40e7b6a9-aeaf-4667-89e7-dbd66f3c7d72.html
  449. 表名AAA,这个表没有主键的 
  450. 其中有两个column , AE_ID 和 ACC_NUM,这两组column中的数据都是有重复的. 
  451. 我想在这两个column 中找出 同一个ACC_NUM 中有两个不同的AE_ID  , 然后用 'Y' 表示. 
  452. 如果一个ACC_NUM 中相对应只有一个AE_ID , 则用'N' 表示. 
  453. 最后显示  AE_ID,ACC_NUM,'Y' or 'N' 的内容.
  454. select acc_num,decode(num,1,'N','Y') num
  455. from (
  456. select acc_num,count(*) num
  457. from aaa
  458. group by acc_num)
  459. 近期计划:课程设计,熟悉工作
  460. 2008-10-8 Wednesday
  461. 1. CRecords BatchSize
  462. 在执行Insert操作时,BatSize应该设置为1, CRecords record(DefaultConnect, 1);
  463. 不然会执行BatSize条插入操作
  464. 其他操作时没有限制
  465. 2. 练习
  466. int main()
  467. {
  468.     long j = Login("ORACLE");
  469.     if (j <= 0)
  470.     {
  471.         cout << "数据库连接失败" << endl;
  472.         return -1;
  473.     }
  474.     CRecords record(DefaultConnect, 1);
  475.     char str[256];
  476.     int i = 0;
  477. //  strcpy(str, "insert into student_a values('002','Jeny')");
  478. //  strcpy(str, "delete from student where sno = '002'");
  479. //  strcpy(str, "select count(*) from student");
  480.     strcpy(str, "select * from student");
  481.     j = record.Query(str);
  482.     if (j <= 0)
  483.     {
  484.         cout << DefaultConnect.GetMsg() << endl;
  485.         cout << "Query" << endl;
  486.     }
  487.     do {
  488.         i++;
  489.         cout << record.Field(0).Char() << endl;
  490.     } while(record.Next() > 0);
  491. //  j = DefaultConnect.ExecSql(str);
  492. //  if (j <= 0)
  493. //  {
  494. //      cout << DefaultConnect.GetMsg();
  495. //      
  496. //  }
  497.     cout << i << endl;
  498.     Logout();
  499.     return 0;
  500. }
  501. 3. SQL语句
  502. http://topic.csdn.net/u/20081007/17/b85a4fa1-2702-4065-84c0-3aa7734fdb16.html
  503. 求一个字段内的字符在另一个字段的字符串内没有出现过的记录 
  504. 如有A、B两个字段: 
  505. A    B 
  506. 13  234 
  507. 23  323 
  508. 33  234 
  509. 44  344 
  510. 要求返回记录集为 
  511. 13、234 
  512. 33、234
  513. create table tb(A varchar(10),B varchar(10))
  514. insert into tb select '13','234' 
  515. union all select '23','323' 
  516. union all select '33','234' 
  517. union all select '44','344' 
  518. union all select '32','355' 
  519. select a,b from tb a where not exists (select 1 from tb where charindex(ltrim(a.a),ltrim(b))>0)
  520. go
  521. drop table tb
  522. /*
  523. a          b
  524. ---------- ----------
  525. 13         234
  526. 33         234
  527. (2 行受影响)
  528. */
  529. 2008-10-9 Thursday
  530. 1. commit语句
  531. 在PL/SQL 中执行命令语句后,在连接程序中发现更新并没有执行,得到的数据还是以前的数据,原因:要执行commit;命令进行提交
  532. Commit不可忘记
  533. 2. nologging
  534. http://www.techwind.net/viewthread.php?tid=6
  535. insert into student nologging values('008','Bill')
  536. 不写日志
  537. 数据量大的时候可以提高速度
  538. 3. 访问数据库示例
  539. int main()
  540. {
  541.     long j = Login("ORACLE");
  542.     if (j <= 0)
  543.     {
  544.         cout << "数据库连接失败" << endl;
  545.         return -1;
  546.     }
  547.     DbFactory.Initial();
  548.     DbFactory.GetConnection("ams""1", amsConn);
  549.     
  550.     CRecords record(*amsConn, 1);
  551.     char str[256];
  552.     strcpy(str, "select * from student");
  553.     j = record.Query(str);
  554.     while (j > 0)
  555.     {
  556.         cout << record.Field("SNO").Char() << " ";
  557.         cout << record.Field("SNAME").Char() << endl;// 注意SNAME应该是大写的
  558.         j = record.Next();
  559.     }
  560.     DbFactory.FreeConnection(amsConn);
  561.     Logout();
  562.     return 0;
  563. }
  564. 配置文件.config.ini内容
  565. [ORACLE]
  566. ConnectString = hadev
  567. PassWord = base
  568. UserName = base
  569. 4. 点滴
  570. create table aa as select * from ins_prod_a where 1 > 2
  571. 新建一个表,只使用另一个表的字段,在条件上进行限制
  572. 内存操作类如DB_USER_FEE_DETAIL
  573. 在执行insert,update时,只是把更新的内容放到队列中,在最后执行save的时候才开始从队列中读取保存到数据库中
  574. 2008-10-10 Friday
  575. 1. C++读写文件
  576. #include <iostream.h>  
  577. #include <fstream.h>
  578. #include <stdlib.h>
  579. void main()
  580. {
  581. //  char a[1000];int i = 0;
  582.     char filename1[256], filename2[256];
  583.     cout<<"输入源文件名:";    /////  如   D:/from.txt
  584.     cin>>filename1;
  585.     cout<<"输入目的文件名";
  586.     cin>>filename2;
  587.     ifstream infile(filename1);
  588.     ofstream outfile(filename2);
  589.     if (!infile)
  590.     {
  591.         cout<<"不能打开输入文件:"<<filename1<<'/n';
  592.         exit(1);
  593.     }
  594.     if (!outfile)
  595.     {
  596.         cout<<"不能打开目的文件: "<<filename2<<'/n';
  597.         exit(2);
  598.     }
  599.     char ch;
  600.     while (infile.get(ch))
  601.     {
  602. //      a[i++] = ch;
  603. //      ch = ch^*'1';
  604. //      ch = ch - 1;
  605.         outfile.put(ch);
  606.     }
  607. //  a[i] = '/0';
  608.     infile.close();
  609.     outfile.close();
  610. //  cout<<a;
  611.     
  612. }
  613. 还有  FILE * fopen(const char * path,const char * mode);  方式
  614. 2. 内存导入测试
  615. #include <time.h>
  616. #include <signal.h>
  617. #include "seetime.h"
  618. #include "writeoff_inc.h"
  619.     DB_USER_FEE_DETAIL db_user_fee_detail;
  620.     Connection* amsConn;
  621.     char sql[1024];
  622.     IND_USER_FEE_DETAIL_BY_INSPRODID ind_user_fee_detail_by_insprodid;
  623. long InitUserInfo()
  624. {
  625.   long j = 0;
  626.   strcpy(g_struGlobalParam.szRegion, "a");
  627.   sprintf(sql, 
  628.       " SELECT PRODUCT_INSTANCE_ID, 0 HIS_AMOUNT, 0 LAST_AMOUNT, 0 HIS_BALANCE, 0 CUR_AMOUNT, "
  629.       "        0 CUR_BALANCE, 0 INVALID_BALANCE, 0 CALL_FEE, 0 RENT_FEE, 0 FAVOR_FEE, "
  630.       "        0 FAVOR_PFEE, 0 PAYMENT_FEE, 0 LATE_FEE, 0 APPORTION_FEE, 0 PRESENT_FEE,"
  631.       "        0 DELMON_FEE, TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') OPT_DATE "
  632.       "   FROM INS_PROD_%s "
  633.       "  WHERE STATE NOT IN('C', 'D', 'E') ", g_struGlobalParam.szRegion);
  634. //  sprintf(sql, "SELECT PRODUCT_INSTANCE_ID FROM INS_PROD_A WHERE 1 = 1");
  635.   g_cLogFile.Info(0, "加载用户资料,SQL=[%s]", sql);
  636.   j = db_user_fee_detail.LoadByRecord(*amsConn, sql);
  637.   if (j <= 0)
  638.   {
  639.     g_cLogFile.Error(0, "加载用户资料到内存失败");
  640.     cout << amsConn->GetMsg() << endl;
  641.     return -1;
  642.   }
  643.   g_cLogFile.Info(0, "装载用户资料成功,共计[%d]条", db_user_fee_detail.GetNodeNumber());
  644. //  CRecords c_record(*amsConn, 1);
  645. //  c_record.Query(sql);
  646. //  for (int i = 0; i < 10; i++)
  647. //  {
  648. //    cout << c_record.Field(0).Float() << endl;
  649. //    c_record.Next();
  650. //  }
  651.   return j;
  652. }
  653. long OutputFeeDetail()
  654. {
  655.   long j = 0;
  656.   int nTotalCounts = 0;
  657.   T_USER_FEE_DETAIL tUserFeeDetail;
  658.   T_LIST<STRU_USER_FEE_DETAIL> *plistUserFeeDetail = NULL;
  659.   tUserFeeDetail.SetConn(*amsConn);
  660.   tUserFeeDetail.SetBatSize(MAX_BATCH_SIZE);
  661.   tUserFeeDetail.SetTableName("aa");
  662.   plistUserFeeDetail = db_user_fee_detail.GetList();
  663.   plistUserFeeDetail->GoHead();
  664.   while (j >= 0 && plistUserFeeDetail->Position())
  665.   {
  666.     nTotalCounts ++;
  667.     db_user_fee_detail.ConvToTable(tUserFeeDetail, plistUserFeeDetail->Current());
  668.     j = tUserFeeDetail.Insert();
  669.     if (j <= 0)
  670.     {
  671.       g_cLogFile.Error(0, "帐单变化表[%s]插入失败,失败原因:[%s]",
  672.           tUserFeeDetail.Name(), amsConn->GetMsg());
  673.       j = -1;
  674.       break;
  675.     }
  676.     plistUserFeeDetail->Next();  
  677.     if (!plistUserFeeDetail->Position())
  678.     {
  679.       g_cLogFile.Info(0, "已生成[%d]条帐单变化记录", nTotalCounts);
  680.     }
  681.     if (!plistUserFeeDetail->Position())
  682.     {
  683.       j = tUserFeeDetail.Flush();
  684.       if (j <= 0)
  685.       {
  686.         j = -1;
  687.         g_cLogFile.Error(0, "帐单变化表[%s]FLUSH失败,失败原因:[%s]"
  688.             tUserFeeDetail.Name(), amsConn->GetMsg());
  689.       }
  690.       if (j >= 0)
  691.       {
  692.         amsConn->Commit();
  693.       }
  694.       else
  695.       {
  696.         amsConn->Rollback();
  697.       }
  698.     }
  699.   }
  700.   return j;
  701. }
  702. int main()
  703. {
  704.     Settime();
  705.     long j = Login("ORACLE");
  706.     if (j <= 0)
  707.     {
  708.         cout << "数据库连接失败" << endl;
  709.         return -1;
  710.     }
  711.     DbFactory.Initial();
  712.     DbFactory.GetConnection("ams""1", amsConn);
  713.     
  714.     db_user_fee_detail.SetBatchSize(MAX_BATCH_SIZE);
  715.     db_user_fee_detail.AddIndex(&ind_user_fee_detail_by_insprodid);
  716.     InitUserInfo();
  717.     OutputFeeDetail();
  718.     STRU_USER_FEE_DETAIL* stru;
  719.     ind_user_fee_detail_by_insprodid.Query(3804338, stru);
  720.     cout << stru->PRODUCT_INSTANCE_ID << endl;
  721.     CRecords record(*amsConn, 1);
  722.     char str[256];
  723.     strcpy(str, "select * from student");
  724.     j = record.Query(str);
  725.     while (j > 0)
  726.     {
  727.         cout << record.Field("SNO").Char() << " ";
  728.         cout << record.Field("SNAME").Char() << endl;
  729.         j = record.Next();
  730.     }
  731.     DbFactory.FreeConnection(amsConn);
  732.     Logout();
  733.     cout << Gettime() << endl;
  734.     return 0;
  735. }
  736. 运行结果
  737. 2008-10-10 11:27:52 [Unknown][THREAD-4080032]INFO:  加载用户资料,SQL=[ SELECT PR
  738. ODUCT_INSTANCE_ID, 0 HIS_AMOUNT, 0 LAST_AMOUNT, 0 HIS_BALANCE, 0 CUR_AMOUNT,
  739.      0 CUR_BALANCE, 0 INVALID_BALANCE, 0 CALL_FEE, 0 RENT_FEE, 0 FAVOR_FEE,
  740.     0 FAVOR_PFEE, 0 PAYMENT_FEE, 0 LATE_FEE, 0 APPORTION_FEE, 0 PRESENT_FEE,
  741.     0 DELMON_FEE, TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') OPT_DATE    FROM INS_PROD
  742. _a   WHERE STATE NOT IN('C''D''E') ]
  743. 2008-10-10 11:27:52 [Unknown][THREAD-4080032]INFO: 装载用户资料成功,共计[3737]条
  744. 2008-10-10 11:27:52 [Unknown][THREAD-4080032]INFO:  已生成[3737]条帐单变化记录
  745. 3.80434e+006
  746. 001        Tom
  747. 004
  748. 005        google
  749. 007        Jim
  750. 008        Bill
  751. 002        Mary
  752. 003        Jack
  753. 0.75
  754. Press any key to continue
  755. 用时0.75秒
  756. 3. 日期数据导入到表中
  757. 先在ams1中创建表cc
  758.    create table cc as select * from sms_cur_balance_a  where 1 > 2
  759. int main()
  760. {
  761.     long j = Login("ORACLE");
  762.     if (j <= 0)
  763.     {
  764.         cout << "Login failed" << endl;
  765.         return 1;
  766.     }
  767.     Connection* amsConn = NULL;
  768.     DbFactory.Initial();
  769.     DbFactory.GetConnection("ams""1", amsConn);
  770.     T_SMS_CUR_BALANCE table_a(*amsConn, 1);
  771.     table_a.SetTableName("cc");
  772.     table_a.ACCT_ID = 1;
  773.     table_a.BALANCE = 12;
  774.     table_a.OPT_DATE = "20081010";
  775.     j = table_a.Insert();
  776.     if (j <= 0)
  777.     {
  778.         cout << amsConn->GetMsg() << endl;
  779.         return 1;
  780.     }
  781.     amsConn->Commit();  // 不可缺少
  782.     Logout();
  783.     return 0;
  784. }
  785. 如果  table_a.OPT_DATE = "2008091012";
  786. 执行后,2008-9-10 12:00:00
  787. 自动进行匹配
  788. table_a.OPT_DATE = "20081010";
  789. 则表中的对应的数据项2008-10-10
  790. 4. SQL语句
  791. 来自csdn
  792. select * from v_box_label 
  793. 显示如下: 
  794. a  3 
  795. b  2 
  796. 如果将select * from v_box_label做成一个视图v1,是否可针对视图v1 select出这样的结果: 
  797. a  3 
  798. a  3 
  799. a  3 
  800. b  2 
  801. b  2 
  802. 即,每条记录显示为相应条数(第2列的值)的重复记录
  803. CREATE TABLE t2(a char(1),c INT);
  804. INSERT INTO t2 VALUES('a',3);
  805. INSERT INTO t2 VALUES('b',2);
  806. INSERT INTO t2 VALUES('c',5);
  807. INSERT INTO t2 VALUES('d',2);
  808. COMMIT;
  809. select * from t2
  810. SELECT T2.*
  811.   FROM T2,
  812.        (SELECT ROWNUM RN
  813.           FROM DUAL, (SELECT MAX(C) C FROM T2) X
  814.         CONNECT BY ROWNUM <= X.C)
  815.  WHERE T2.C >= RN
  816.  ORDER BY A;  
  817. 5. 表类导入方法测试
  818. int main()
  819. {
  820.     Settime();
  821.     long j = Login("ORACLE");
  822.     if (j <= 0 )
  823.     {
  824.         cout << "Connect lost" << endl;
  825.         return 1;
  826.     }
  827.     Connection* amsConn = NULL;
  828.     DbFactory.Initial();
  829.     j = DbFactory.GetConnection("ams""1", amsConn);
  830.     if (j <= 0)
  831.     {
  832.         cout << "Db lost" << endl;
  833.         return 1;
  834.     }
  835.     T_ACCT_BALANCE table_a(*amsConn, 1);
  836.     table_a.SetTableName("cc");
  837.     
  838.     CFileReader cread("F://test.txt"","" " );
  839.     j = cread.Open();
  840.     int i = 0;
  841.     while (j > 0)
  842.     {
  843.         table_a.ACCT_BALANCE_ID = (long int)_atoi64(cread.GetField(4));
  844.         table_a.ACCT_ID = (long int)_atoi64(cread.GetField(5));
  845.         table_a.PAYMENT_ID = 1;
  846.         table_a.EFF_DATE = cread.GetField(6);
  847.         table_a.EXP_DATE = cread.GetField(7);
  848.         table_a.OPT_DATE = cread.GetField(8);
  849.         table_a.Insert();
  850.         j = cread.Next();
  851.         i++;
  852.     }
  853.     j = amsConn->Commit();
  854.     if (j <= 0)
  855.     {
  856.         cout << amsConn->GetMsg() << endl;
  857.         return 1;
  858.     }
  859.     cout << "OK" << endl;
  860.     cout << i << endl;
  861.     cout << Gettime() << endl;
  862.     return 0;
  863. }   
  864. OK
  865. 5543
  866. 23.844      
  867. 5543条用时23.844秒
  868. 出现问题:表类ACCT_BALANCE 
  869. PAYMENT_ID( "PAYMENT_ID", FloatT, 15, 0, 1 ), 整型
  870. 和数据库中字段的字符串类型不符合
  871. 新版本中已经同步修改了PAYMENT_ID( "PAYMENT_ID", CharT, 25, 0, 1 ),
  872. 2008-10-11 Saturday
原创粉丝点击