前言
本文给大家展示的代码实际上就是如何利用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);
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“SQLite3的绑定函数族使用与其注意事项详解”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年09月21日
2024年09月21日
- [ABC]安娜-胆麦发烧女声[6N纯银镀膜][2016[低速原抓WAV+CUE]
- 任天堂今晚举行直面会!第三方及独立游戏展示
- 《哆啦A梦的铜锣烧店物语》发售!开罗公式+哆啦A梦
- 任天堂公布《塞尔达传说》系列时间线:野炊与王泪独立在外
- 五条人.2012-一些风景2CD【刀马旦】【WAV+CUE】
- 陈奕迅.2013-Easons.Life演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 许美静.1995-遗憾(新马版)【上华】【WAV+CUE】
- 《叶倩文 歌声情缘》[WAV+CUE][410MB]
- 《张国荣 首首动听经典不容错过 追忆的风 2CD》[WAV+CUE][870MB]
- 《腾格尔 容中尔甲 亚东 高原三星 男人篇 3CD》[WAV/分轨][1GB]
- 命运圣契公测实测可用兑换码大全 命运圣契最新兑换码分享
- 黑神话悟空上品疾蝠精魄获取方法一览|上品疾蝠精魄收集攻略
- 《七龙珠电光炸裂!ZERO》GT角色预告片曝光,15位新角色登场
- NewViennaOctetViennaWindSoloists-TheDeccaRecordings(2024)18CD[24-48][FLAC]-7
- 雨果唱片-三角琴《俄罗斯民间音乐系列-梁祝(中国名曲)》wav