亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

SQLite3的綁定函數族使用與其注意事項詳解

瀏覽:260日期:2023-04-05 14:55:56

前言

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

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

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;
  }

  //日期相關
  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
  {
  //金錢相關的處理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);

為什么呢?

因為,sqlite3_bind_text綁定的text,需要在做:

rc = sqlite3_step(stmt); 

的時候統一提交,而上面的代碼使用的臨時變量,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;
  }

  //日期相關
  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
  {
  //金錢相關的處理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);

附上數據庫創建的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);

總結

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

標簽: SQLite
相關文章:
主站蜘蛛池模板: 在线免费观看国产视频 | 欧美一区二区视频三区 | 精品国产_亚洲人成在线高清 | 香蕉成视频片在线观看 | a国产 | 日本在线黄色 | 亚洲高清heyzo加勒比 | 最新毛片网 | 国产1000部成人免费视频 | 午夜激情视频在线 | 免费播放特黄特色毛片 | 国产精品极品美女自在线看免费一区二区 | 欧美一级淫片a免费播放口aaa | 小明看看在线 | 国产精品久久精品福利网站 | 国产综合视频在线观看一区 | 狠狠色丁香九九婷婷综合五月 | 黄色视频一级毛片 | 日本黄色一级网站 | 国产精品国内免费一区二区三区 | 微拍 福利 视频 国产 | 香蕉视频黄色 | 综合图色 | 国产91精品一区二区视色 | 欧美日本一道道一区二区三 | 特黄特色大片免费播放 | 欧美区在线 | 黄色大全网站 | 草逼社区| 一级网站在线观看 | 国外免费精品视频在线观看 | 香蕉视频国产精品人 | 日本特黄特色大片免费视频 | 一级毛片免费全部播放完整 | 久久精品成人免费网站 | 欧美精品人爱a欧美精品 | 日韩精品一区二区三区不卡 | 自拍第一页 | 黄色一级视频网站 | 中国孕妇疯狂xxxxbbbb | 国产在线拍揄自揄拍视频 |