windows 与linux 下用C++读取sqlite实现文件复制(二)

2.分别查询读取sqlite表中的字段。

 1 //在数据库中查询表:
 2     // 执行SQL 
 3     char **dbResult; //是 char ** 类型,两个*号
 4     int nRow, nColumn;
 5     sprintf(sql, "select * from test1");
 6     int result = sqlite3_get_table( conndb, sql, &dbResult, &nRow, &nColumn, &err_msg );
 7     if( SQLITE_OK == result )//查询成功
 8     {
 9         if ( dbResult != 0)
10         {
11             for (int index = 1; index <= nRow; index++)
12             {    
13                 printf( "第 %d 条记录 \n", index );
14                 if (nColumn != 3)
15                     return -1;
16                 char  SourcePath[256], DesPath[256];  
17                 strcpy(SourcePath, dbResult[index*nColumn  + 1]);
18                 strcpy(DesPath, dbResult[index*nColumn + 2]);
19                 printf("SourcePath: %s\n", SourcePath);
20                 printf("DesPath: %s\n", DesPath);
21 
22                 CheckDirectory( DesPath );
23                 strcpy(DesPath, dbResult[index*nColumn + 2]);
24                 if(CopyFile(SourcePath,DesPath))
25                 {
26                     cout<<"文件已成功复制..."<<endl;
27                 }
28                 else
29                 {
30                     cout<<"文件复制失败..."<<endl;
31                 }
32 
33 
34                 sprintf(sql ,"update test1 set des=src where id =%d", index);
35                 int bRet = sqlite3_exec(conndb, sql,NULL,NULL,&err_msg);
36                 /* 如果执行成功,则删除记录 */
37                 if (bRet == SQLITE_OK)
38                 {
39                     sprintf(sql, "delete from test1 where id =%d", index);
40                     sqlite3_exec(conndb, sql, NULL, NULL, &err_msg);
41                 }
42             }
43         }
44 
45     }else//查询失败
46     {
47         Sleep(100000);  
48     }
49     //到这里,不论数据库查询是否成功,都释放 char** 查询结果,使用 sqlite 提供的功能来释放
50     sqlite3_free_table( dbResult );    

3.检查目标路径是否存在,不存在则创建。linux和windows下都可以用该函数

 1 // 遍历目录
 2 bool CheckDirectory( char* pszPath )
 3 {
 4     vector< std::string > vtPath;
 5 
 6     const char* sep = "\/";
 7     char* next_token;
 8     char* token =  strtok_s( pszPath, sep, &next_token);
 9     while( token != NULL )
10     {
11         vtPath.push_back( token );
12         token = strtok_s(NULL, sep, &next_token);
13     }
14 
15     if ( vtPath.size() > 0 )
16     {
17         if ( vtPath[0] == "." )
18             vtPath.erase( vtPath.begin() );
19     }
20 
21     // 核查所有路径是否存在
22     std::string strCurPath;
23     for( size_t i = 0; i  < (int)vtPath.size(); ++i )
24     {
25 
26 
27         //cout << vtPath[i] << endl;
28 
29         if (i==0)
30         {
31             strCurPath += vtPath[i];
32         }
33         else if (i < (int)vtPath.size()-1)
34         {
35             strCurPath +='\/';
36             strCurPath += vtPath[i];
37 
38 
39         }
40         if ( !CreateDirectory(strCurPath.c_str(),777))
41         {
42             return false;
43         }
44 
45     }
46     strCurPath +='\/';
47     strCurPath += vtPath[(int)vtPath.size()-1];
48     if( !isExistFile(strCurPath.c_str()))
49     {
50         return false;
51     }
52 
53     return true;
54 }

4.生成目录

/*!

以指定的权限创建目录.

对于指定要创建的目录,如果其父目录不存在(递归至要生成的目标上报的根目录),那么会创建其对应的父目录

例如,指定要创建的目录: "/foo/bar",如果/foo/目录不存在,那么在创建 bar 目录之前会创建 /foo 目录

对于要生成的目标目录,"./foo/bar" 等价于 "foo/bar"

@param szDirectoryPath 要创建的目录的路径

@param iDirPermission 创建目录时,为所创建的目录所指定的权限

@return 如果创建成功,返回<b>true</b>;如果创建失败,返回<b>false</b>.

*/

  1 static bool CreateDirectory( const char *szDirectoryPath , int iDirPermission )
  2 
  3 {
  4 
  5     if ( NULL == szDirectoryPath )
  6 
  7     {
  8 
  9 #ifdef DEBUG
 10 
 11         fprintf( stderr , "[%s][%d][%s][parameter < szDirectoryPath > for < CreateDirectory > should not be NULL]\n" , \
 12 
 13             __FILE__ , __LINE__ , __FUNCTION__ );
 14 
 15 #endif
 16 
 17         return false;
 18 
 19     }
 20 
 21     const int iPathLength = static_cast< int >( strlen( szDirectoryPath ) );
 22 
 23     if ( iPathLength > PATH_MAX )
 24 
 25     {
 26 
 27 #ifdef DEBUG
 28 
 29         fprintf( stderr , "[%s][%d][%s][the path length(%d) exceeds system max path length(%d)]\n" , \
 30 
 31             __FILE__ , __LINE__ , __FUNCTION__ , iPathLength , PATH_MAX );
 32 
 33 #endif
 34 
 35         return false;
 36 
 37     }
 38 
 39     char szPathBuffer[ PATH_MAX ] = { 0 };
 40 
 41     memcpy( szPathBuffer , szDirectoryPath , iPathLength );
 42 
 43     for ( int i = 0 ; i < iPathLength ; ++i )
 44 
 45     {
 46 
 47         char &refChar = szPathBuffer[ i ];
 48 
 49         //目录分隔符
 50 
 51         if ( ( '/' == refChar ) && ( 0 != i ) )
 52 
 53         {
 54 
 55             refChar = '\0';
 56 
 57             //判断当前目录是否存在
 58 
 59             int iStatus = access( szPathBuffer , F_OK );
 60 
 61             if ( 0 != iStatus )
 62 
 63             {
 64 
 65                 if ( ( ENOTDIR == errno ) || ( ENOENT == errno ) )
 66 
 67                 {
 68 
 69                     //以指定权限创建目录
 70 
 71                     iStatus = mkdir( szPathBuffer , iDirPermission );
 72 
 73                     if ( 0 != iStatus )
 74 
 75                     {
 76 
 77 #ifdef DEBUG
 78 
 79                         fprintf( stderr , "[%s][%d][%s][< mkdir > fail , ErrCode:%d , ErrMsg:%s]\n" , \
 80 
 81                             __FILE__ , __LINE__ , __FUNCTION__ , errno , strerror( errno ) );
 82 
 83 #endif
 84 
 85                         return false;
 86 
 87                     }
 88 
 89                 }
 90 
 91                 else
 92 
 93                 {
 94 
 95 #ifdef DEBUG
 96 
 97                     fprintf( stderr , "[%s][%d][%s][< access > fail , RetCode: %d , ErrCode:%d , ErrMsg:%s]\n" , \
 98 
 99                         __FILE__ , __LINE__ , __FUNCTION__ , iStatus , errno , strerror( errno ) );
100 
101 #endif
102 
103                     return false;
104 
105                 }
106 
107             }
108 
109             refChar = '/';
110 
111         }
112 
113     }
114 
115     return true;
116 
117 }
View Code

 

posted @ 2014-11-21 09:23  前进中的菜鸟  阅读(314)  评论(0编辑  收藏  举报