1. 程式人生 > >通過ADO寫入含blob欄位MySQL表,報"Column 'xxx' cannot be null"錯誤的問題

通過ADO寫入含blob欄位MySQL表,報"Column 'xxx' cannot be null"錯誤的問題

1.場景
1.1 程式環境:MySQL 5.0/MySQL ODBC 3.51 Driver/ADO
1.2.測試表
CREATE TABLE `tb_test_12` (
  `f1` int(11) NOT NULL auto_increment,
  `f2` blob NOT NULL,
  `f3` int(11) default NULL,
  PRIMARY KEY  (`f1`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gbk;

1.3 測試程式碼
int CXBox::OnTest666() {
 GETDBC(pdbor,this->local_dbc_.c_str());
 CRecordset *prs = pdbor->Query(adCmdTable,"tb_test_12");
 bool br;
 br = prs->AddNew();
 br = prs->PutCollect("f3",10l);
 char buffer[32]="sssssss";
 unsigned long len = strlen(buffer);
 br = prs->AppendChunk("f2",(void*)buffer,len);
 ///< @note 到此為止,前面的操作都指示執行成功
 
 br = prs->Update();

 ///< Update失敗,錯誤為:
 ///< [MySQL][ODBC 5.1 Driver][mysqld-5.0.45-community-nt-log]Column 'f2' cannot be null
 const char *err = pdbor->GetLastError();

 return 0;
}

2.解決
2.1使用客戶端遊標,程式碼如下:
GET_CURSOR_DBC(pdbor,this->local_dbc_.c_str(),CLIENT_CURSOR);

其理由見http://dev.mysql.com/doc/refman/5.1/en/connector-odbc-errors.html#qandaitem-21-1-7-3-1-10


21.1.7.3.10: Using the AppendChunk() or GetChunk() ADO methods, the Multiple-step operation generated errors. Check each status value error is returned.
The GetChunk() and AppendChunk() methods from ADO doesn't work as expected when the cursor location is specified as adUseServer. On the other hand, you can overcome this error by using adUseClient.

對應中文頁面 

存疑:

.“不能按預期的方式工作”: 是必然得不到預期的結果,還是不能確定得到預期的結果.
.我所見的程式碼裡面有用adUseServer的AppendChunk,從上下文看似乎它應該在實際執行時是工作的,為什麼沒有暴露問題

.使用GetChunk的程式碼也未見得使用客戶端遊標

2.2hotfox.conf配置

 <database encrypted_password="false">
  <conn constr="DRIVER={MySQL ODBC 3.51 Driver};SERVER=127.0.0.1;;OPTION=3;charset=gbk" count="25:2" name="LATON" db_ext="mysql_ext.dll"/>

<count>屬性設定採用服務端和客戶端遊標的連線個數,兩者之間用":"分隔。