代码以及相关文件下载地址:
 

一、环境:

Windows 7
python 2.7
py2exe for python 2.7
Microsoft Visual Studio 2010
 
二、目标与原理:
1. 当C++执行数据库写入时,首先生成wordpart.txt文件
文件格式为:时间,词,词频 如:1340958642,奥特曼,15
2. C++利用CreateProcess创建进程,调用python编译而成的.exe文件写入数据库
python的参数命令为 --p [wordpart.txt的路径]
3. C++等待进程结束,进行后续操作
 
三、数据库的格式:
 
四、C++代码如下:
 
 
  
  1. #include "stdafx.h" 
  2. #include <Windows.h> 
  3. #include <iostream> 
  4.  
  5. using namespace std; 
  6. int _tmain(int argc, _TCHAR* argv[]) 
  7.                  char* wait = "" ; 
  8.                 STARTUPINFO si = { sizeof(si) }; 
  9.  
  10.                 PROCESS_INFORMATION pi; 
  11.                 TCHAR szCommandLine[] = TEXT( "F:\\backup\\4\\dist\\writeCSV.exe --p F:\\wordpart.txt" ); 
  12.                  
  13.                  TCHAR szCmd[16] = {0}; 
  14.                  if(!CreateProcess(NULL, szCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) 
  15.                 { 
  16.                                 cout<< "fail!"<<GetLastError()<<endl; 
  17.                 }; 
  18.                 WaitForSingleObject(pi.hProcess,INFINITE); 
  19.                 cout<< "finished!"
  20.                 cin>>wait; 
  21.                  return 0; 

五、python代码如下:
 
 
  
  1. #coding:utf-8 
  2. import os, sys 
  3. import MySQLdb 
  4.  
  5.  
  6. def parseCSV(outPath): 
  7.     '''''parse the .csv/.out file''' 
  8.     wordGroups = []#return the [[word,time,frequency]...] 
  9.     outFile = open(outPath,'r'
  10.     allLines = outFile.readlines() 
  11.     for eachline in allLines: 
  12.         lineParts = eachline.split(','
  13.         if len(lineParts) != 3
  14.             print 'wrong line format!' 
  15.         else
  16.             group = lineParts#[word, time, frequency] 
  17.             wordGroups.append(group) 
  18.     outFile.close() 
  19.     return wordGroups 
  20.  
  21. def writeToDB(wordGroups): 
  22.     conn = MySQLdb.connect(host = "localhost",user="root",\ 
  23.                            passwd = '111111',db='microblog',use_unicode=1
  24.                            charset='utf8'
  25.     cursor = conn.cursor() 
  26.     sql = "select word from word_frequency" 
  27.     count = cursor.execute(sql) 
  28.     rows = cursor.fetchall() 
  29.     wordAlreadyExists = rows[0:]["word"]#all words 
  30.     cursor.close() 
  31.  
  32.     cursor = conn.cursor() 
  33.     for group in wordGroups: 
  34.         try:#in case of duplicated status 
  35.             sql = "insert into word_frequency(word,time,frequency) values (%s,%s,%d)" 
  36.             param = (group[0],group[1],group[2]) 
  37.             cursor.execute(sql,param) 
  38.         except MySQLdb.Error, e: 
  39.             print(e) 
  40.     cursor.close() 
  41.     conn.commit() 
  42.  
  43. if __name__ == '__main__'
  44.     '''''Write the c++ output .csv/.out to db 
  45. example: --p [the path of .csv/.out] 
  46. ''' 
  47.     if len(sys.argv) < 3
  48.         #no argument, use the default path 
  49.         print 'wrong! --p [the path of .csv/.out]' 
  50.     elif sys.argv[1].startwith('--'): 
  51.         #parse sys.argv[2] execute the write statement 
  52.         option = sys.argv[1][2:] 
  53.         if option == 'p'
  54.             outPath = sys.argv[2
  55.             
  56.     else
  57.         print 'unknown option' 

六、python 代码需要编译成.exe文件才能被C++调用执行,为此可使用py2exe工具
在cmd下运行python mysetup.py py2exe命令
 
mysetup.py如下:

 
    
  1. # setup.py 
  2. from distutils.core import setup 
  3. import py2exe 
  4. setup(console=["writeCSV.py"]) 

生成dist文件夹,其中有writeCSV.exe
此时使用C++ 运行即可。