濮阳杆衣贸易有限公司

主頁 > 知識庫 > SQLite3的綁定函數(shù)族使用與其注意事項(xiàng)詳解

SQLite3的綁定函數(shù)族使用與其注意事項(xiàng)詳解

熱門標(biāo)簽:奧維地圖標(biāo)注字體大小修改 電話機(jī)器人錄音師薪資 智能電銷機(jī)器人教育 江西穩(wěn)定外呼系統(tǒng)供應(yīng)商 孝感銷售電銷機(jī)器人廠家 高德地圖標(biāo)注電話怎么沒了 無錫梁溪公司怎樣申請400電話 中國地圖標(biāo)注省份用什么符號 北京智能外呼系統(tǒng)供應(yīng)商家

前言

本文給大家展示的代碼實(shí)際上就是如何利用Sqlite3的參數(shù)化機(jī)制做數(shù)據(jù)插入,也可以update操作,就看你怎么玩了,這里只列出代碼,然后說一些注意事項(xiàng)。

下面的代碼,有一個問題,插入后的東西一定是:

INSERT INTO "work" VALUES('鉿','鉿鉿鉿鉿鉿',NULL,NULL,NULL,NULL,'鉿鉿鉿鉿鉿',NULL,NULL,110.0,1.0,108.9,NULL,NULL,'鉿鉿鉿鉿鉿',NULL,NULL,NULL,'鉿鉿鉿鉿鉿',NULL,NULL,NULL);

看看有問題的代碼:

sqlite3_stmt *stmt;
 CString sql = "insert into work values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
 int rc = sqlite3_prepare_v2(db, sql.GetString(), -1, stmt, NULL);

 if(rc != SQLITE_OK)
 {
 MessageBox("sqlite3_prepare_v2 Failed!");
 return;
 }

 count = 0;
 p_wnd = PrevWnd;

 while(count++  ID_TOTALCOUNT)
 {
 CString DbStr;
 
 p_wnd = CWnd::GetNextDlgTabItem(p_wnd, FALSE); 
 if(p_wnd == NULL)
 {
  return;
 }

 p_wnd->GetWindowText(DbStr);

 do
 {
  if(!DbStr.GetLength())
  {
  rc = sqlite3_bind_null(stmt, count);
  break;
  }

  //日期相關(guān)
  if( count == ID_CHUDANRIQI || 
  count == ID_CHUFARIQI || 
  count == ID_HUANKUANRIQI || 
  count == ID_HUOLIRIQI)
  {
  CDateTimeCtrl *TimeCtl = (CDateTimeCtrl *)p_wnd;  
  CString time = DateTimeToString(*TimeCtl);

  rc = sqlite3_bind_text(stmt, count, time.GetString(), time.GetLength(), SQLITE_STATIC);
  break;
  }
  else
  {
  //金錢相關(guān)的處理real類型
  if( count == ID_BAOXIANJINE || 
   count == ID_YONGJINBILV || 
   count == ID_JINGBAOFEI || 
   count == ID_HUANKUANJINE || 
   count == ID_LIRUNBILV || 
   count == ID_LIRUNJINE)
  {
   double tMoney = 0.0;
   int rtn = sscanf_s(DbStr.GetString(), "%lf", tMoney);

   ASSERT(rtn == 1);

   rc = sqlite3_bind_double(stmt, count, tMoney);
  }
  else
  {
   char *str = (char *)DbStr.GetString();
   int c = strlen(str);
   int c1 = DbStr.GetLength();

   rc = sqlite3_bind_text(stmt, count, DbStr.GetString(), -1/*DbStr.GetLength()*/, SQLITE_STATIC);
  }
  }
 }while(0);

 if(rc != SQLITE_OK)
 {
  CString ErrStr = sqlite3_errstr(rc);
  MessageBox(ErrStr);

  return;
 }
 }

 rc = sqlite3_step(stmt); 

 if(rc != SQLITE_DONE)
 {
 if(rc == SQLITE_ERROR)
 {
  CString DbErr;
  DbErr.Format("Sql Insert failed, %s", sqlite3_errmsg(db));

  MessageBox(DbErr);
 }
 else
 {
  MessageBox("sqlite3_step Failed!");
 } 
 }

 sqlite3_finalize(stmt);

為什么呢?

因?yàn)?,sqlite3_bind_text綁定的text,需要在做:

rc = sqlite3_step(stmt); 

的時候統(tǒng)一提交,而上面的代碼使用的臨時變量,rc = sqlite3_step(stmt);的時候,早就不存在了。因此亂碼也是正常的。

修改如下:

sqlite3_stmt *stmt;
 CString sql = "insert into work values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
 int rc = sqlite3_prepare_v2(db, sql.GetString(), -1, stmt, NULL);

 if(rc != SQLITE_OK)
 {
 MessageBox("sqlite3_prepare_v2 Failed!");
 return;
 }

 count = 0;
 p_wnd = PrevWnd;

 CString DbStr[ID_TOTALCOUNT + 1];

 while(count++  ID_TOTALCOUNT)
 {
 DbStr[count].Empty();
 
 p_wnd = CWnd::GetNextDlgTabItem(p_wnd, FALSE); 
 if(p_wnd == NULL)
 {
  return;
 }

 p_wnd->GetWindowText(DbStr[count]);

 do
 {
  if(!DbStr[count].GetLength())
  {
  rc = sqlite3_bind_null(stmt, count);
  break;
  }

  //日期相關(guān)
  if( count == ID_CHUDANRIQI || 
  count == ID_CHUFARIQI || 
  count == ID_HUANKUANRIQI || 
  count == ID_HUOLIRIQI)
  {
  CDateTimeCtrl *TimeCtl = (CDateTimeCtrl *)p_wnd;  
  CString time = DateTimeToString(*TimeCtl);

  DbStr[count] = time;

  rc = sqlite3_bind_text(stmt, count, time.GetString(), time.GetLength(), SQLITE_STATIC);
  }
  else
  {
  //金錢相關(guān)的處理real類型
  if( count == ID_BAOXIANJINE || 
   count == ID_YONGJINBILV || 
   count == ID_JINGBAOFEI || 
   count == ID_HUANKUANJINE || 
   count == ID_LIRUNBILV || 
   count == ID_LIRUNJINE)
  {
   double tMoney = 0.0;
   int rtn = sscanf_s(DbStr[count].GetString(), "%lf", tMoney);

   ASSERT(rtn == 1);

   rc = sqlite3_bind_double(stmt, count, tMoney);
  }
  else
  {
   rc = sqlite3_bind_text(stmt, count, DbStr[count].GetString(), DbStr[count].GetLength(), SQLITE_STATIC);
  }
  }
 }while(0);

 if(rc != SQLITE_OK)
 {
  CString ErrStr = sqlite3_errstr(rc);
  MessageBox(ErrStr);

  return;
 }
 }

 rc = sqlite3_step(stmt); 

 if(rc != SQLITE_DONE)
 {
 if(rc == SQLITE_ERROR)
 {
  CString DbErr;
  DbErr.Format("Sql Insert failed, %s", sqlite3_errmsg(db));

  MessageBox(DbErr);
 }
 else
 {
  MessageBox("sqlite3_step Failed!");
 } 
 }

 sqlite3_finalize(stmt);

附上數(shù)據(jù)庫創(chuàng)建的sql語法:

sqlite> .dump work
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE work (baodanhao text unique primary key , chudanriqi text,qudao text,lianxiren text,xiaoshou text,beibaorenxingming text,chufar
iqi text,baoxianpinpai text,baoxianjihua text,baoxianjine real,yongjinbilv real,jingbaofei real,huankuanfangshi text,haikuanjine real,huanku
anriqi text,shifouquane text,lirunbilv real,lirunjine real,huoliriqi text,fapiaojisong text,shifubaoxiangongsi text,beizhu text);

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

您可能感興趣的文章:
  • SQLite3中的日期時間函數(shù)使用小結(jié)
  • 為SQLite3提供一個ANSI到UTF8的互轉(zhuǎn)函數(shù)

標(biāo)簽:荊州 臨滄 泰州 海北 那曲 齊齊哈爾 通化 阜陽

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《SQLite3的綁定函數(shù)族使用與其注意事項(xiàng)詳解》,本文關(guān)鍵詞  SQLite3,的,綁定,函數(shù),族,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《SQLite3的綁定函數(shù)族使用與其注意事項(xiàng)詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于SQLite3的綁定函數(shù)族使用與其注意事項(xiàng)詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    晴隆县| 安福县| 清涧县| 桑植县| 平安县| 景宁| 那曲县| 昌都县| 定陶县| 宁安市| 贺兰县| 丰都县| 加查县| 永修县| 海门市| 双牌县| 花莲市| 会理县| 库车县| 阜平县| 扶余县| 马尔康县| 两当县| 会理县| 会宁县| 祁门县| 桐庐县| 安龙县| 广灵县| 城固县| 绥宁县| 武安市| 台中县| 雷州市| 麻栗坡县| 莒南县| 芮城县| 阆中市| 本溪市| 铜鼓县| 大邑县|