混合c++ /CLI代码与非托管SQLite



我尝试使用c++/CLI的Mixed Mode来操作SQLite。我写了下面的代码:

    int rc; 
    char  *sql, *Sig, *DocName, *OrgName,*From,*To,*Date;
    sqlite3 *db; //Database handle
    sqlite3_stmt *stmt; //used to handle stmt for step(), its is name prepared stmt
    sql = "insert into Norm1Tab values (?,?,?,?,?,?);";
    sqlite3_open("TTest", &db);  
    Sig     =(char *)Marshal::StringToHGlobalAnsi(this->maskedTextBox7->Text).ToPointer();
    DocName =(char *)Marshal::StringToHGlobalAnsi(this->maskedTextBox2->Text).ToPointer();
    OrgName =(char *)Marshal::StringToHGlobalAnsi(this->maskedTextBox3->Text).ToPointer();
    From    =(char *)Marshal::StringToHGlobalAnsi(this->maskedTextBox4->Text).ToPointer();
    To      =(char *)Marshal::StringToHGlobalAnsi(this->maskedTextBox5->Text).ToPointer();
    Date    =(char *)Marshal::StringToHGlobalAnsi(this->maskedTextBox6->Text).ToPointer();
    rc= sqlite3_prepare(db, sql, strlen(sql), &stmt, NULL);//compile sql to byte code
   sqlite3_bind_text(stmt, 1, Sig, strlen(Sig),SQLITE_TRANSIENT);
   sqlite3_bind_text(stmt, 2, DocName, strlen(DocName),SQLITE_TRANSIENT);
   sqlite3_bind_text(stmt, 3, OrgName, strlen(OrgName),SQLITE_TRANSIENT);
   sqlite3_bind_text(stmt, 4, From, strlen(From),SQLITE_TRANSIENT);
   sqlite3_bind_text(stmt, 5, To, strlen(To),SQLITE_TRANSIENT);
   sqlite3_bind_text(stmt, 6, Date, strlen(Date),SQLITE_TRANSIENT);
    rc = sqlite3_step(stmt);//talk directly with VDBE and execute the bytecode

    //free all handles and objects
    Marshal::FreeHGlobal((IntPtr)Sig);
    Marshal::FreeHGlobal((IntPtr)DocName);
    Marshal::FreeHGlobal((IntPtr)OrgName);
    Marshal::FreeHGlobal((IntPtr)From);
    Marshal::FreeHGlobal((IntPtr)To);
    Marshal::FreeHGlobal((IntPtr)Date);
    sqlite3_finalize(stmt);
    sqlite3_close(db);

这段代码试图将文件TTest写入有六列的表Norm1Tab,但是它没有写任何东西!!有什么办法可以帮助解决这个问题吗?

编辑:

我注意到一些奇怪的事情,实际上我确实在Windows XP SP3机器上编译了前面的代码,它显示了所描述的问题。但是当我在Windows 7机器上编译它时,它工作得很好,没有做任何修改!此外,我试图在XP机器上编译它,然后将二进制文件与'sqlite3.dll'和'数据文件'复制到Win7,它工作得很好!

在Windows XP上,我尝试了很多修改,但没有任何效果,但我现在有了这个要点:

我的项目有OpenFileDialog对象,它由按钮Button1调用,不同于按钮Button2调用上面所示的代码。奇怪的是,当我点击Button1然后点击Button2时,SQLite的代码什么也不做,而如果我在Button1之前点击Button2,代码工作得很好!这只适用于Windows XP,它是目标系统。

以下是Button1处理程序的代码:
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
if(openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
      {
         this->maskedTextBox1->Text=openFileDialog1->FileName->ToString(); 
      }
HANDLE hFile;
HANDLE hMap;
//open the file//
hFile = ::CreateFile((LPCWSTR)Marshal::StringToHGlobalUni(this->maskedTextBox1->Text).ToPointer(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
                                 0,OPEN_EXISTING , FILE_FLAG_SEQUENTIAL_SCAN, 0);
//get the size for creating the signature and mapping it to Virtual mem//
unsigned long sifi=0;
if(hFile !=INVALID_HANDLE_VALUE){
hMap= ::CreateFileMapping(hFile, 0, PAGE_READONLY | SEC_COMMIT, 0, 0, 0);//create Mem mapping for the file in virtual memory
sifi= ::GetFileSize(hFile,NULL);
}
//load the binary form of the file to memory//
 LPVOID base;
 BYTE b1,b2,b3,b4,b5,b6,b7,b8,b9,b10;
 int inc=2;
if( hMap!=NULL){
base = ::MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);//load the mapped file into the RAM
b1= *((BYTE *)base + sifi/inc++);
b2= *((BYTE *)base + sifi/inc++);
b3= *((BYTE *)base + sifi/inc++);
b4= *((BYTE *)base + sifi/inc++);
b5= *((BYTE *)base + sifi/inc++);
b6= *((BYTE *)base + sifi/inc++);
b7= *((BYTE *)base + sifi/inc++);
b8= *((BYTE *)base + sifi/inc++);
b9= *((BYTE *)base + sifi/inc++);
b10= *((BYTE *)base + sifi/inc++);
}
inc=2;


//show the sig
 String^ HexSig;
HexSig = String::Format("{0:X2}", b1)+String::Format("{0:X2}", b2)+String::Format("{0:X2}", b3)+String::Format("{0:X2}", b4)+String::Format("{0:X2}", b5)+String::Format("{0:X2}", b6)+String::Format("{0:X2}", b7)+String::Format("{0:X2}", b8)+String::Format("{0:X2}", b9)+String::Format("{0:X2}", b10);
this->maskedTextBox7->Text=HexSig;

//free handles
  ::CloseHandle(hFile); 
  ::CloseHandle(hMap);
  ::UnmapViewOfFile(base);
         }

这里的任何人都能看出问题所在!

可能的问题:

你检查文件是否正确打开了吗?您的表是否只有六列?表是否已经创建?表的类型是否正确(如有需要则为TEXT)?

在button1代码的末尾有一行

this->maskedTextBox7->Text=HexSig;

使用maskedTextBox7->Text进行sql查询。这个字符串是否与表(第一列)中的正确值兼容?

最新更新