• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    SQLite3的绑定函数族使用与其注意事项详解

    发布者: 涵韵 | 发布时间: 2025-6-18 14:44| 查看数: 124| 评论数: 0|帖子模式

    前言
    本文给大家展示的代码实际上就是如何利用Sqlite3的参数化机制做数据插入,也可以update操作,就看你怎么玩了,这里只列出代码,然后说一些注意事项。
    下面的代码,有一个问题,插入后的东西一定是:
    1. INSERT INTO "work" VALUES('铪','铪铪铪铪铪',NULL,NULL,NULL,NULL,'铪铪铪铪铪',NULL,NULL,110.0,1.0,108.9,NULL,NULL,'铪铪铪铪铪',NULL,NULL,NULL,'铪铪铪铪铪',NULL,NULL,NULL);
    复制代码
    看看有问题的代码:
    1. sqlite3_stmt *stmt;
    2. CString sql = "insert into work values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    3. int rc = sqlite3_prepare_v2(db, sql.GetString(), -1, &stmt, NULL);

    4. if(rc != SQLITE_OK)
    5. {
    6. MessageBox("sqlite3_prepare_v2 Failed!");
    7. return;
    8. }

    9. count = 0;
    10. p_wnd = PrevWnd;

    11. while(count++ < ID_TOTALCOUNT)
    12. {
    13. CString DbStr;

    14. p_wnd = CWnd::GetNextDlgTabItem(p_wnd, FALSE);
    15. if(p_wnd == NULL)
    16. {
    17.   return;
    18. }

    19. p_wnd->GetWindowText(DbStr);

    20. do
    21. {
    22.   if(!DbStr.GetLength())
    23.   {
    24.   rc = sqlite3_bind_null(stmt, count);
    25.   break;
    26.   }

    27.   //日期相关
    28.   if( count == ID_CHUDANRIQI ||
    29.   count == ID_CHUFARIQI ||
    30.   count == ID_HUANKUANRIQI ||
    31.   count == ID_HUOLIRIQI)
    32.   {
    33.   CDateTimeCtrl *TimeCtl = (CDateTimeCtrl *)p_wnd;  
    34.   CString time = DateTimeToString(*TimeCtl);

    35.   rc = sqlite3_bind_text(stmt, count, time.GetString(), time.GetLength(), SQLITE_STATIC);
    36.   break;
    37.   }
    38.   else
    39.   {
    40.   //金钱相关的处理real类型
    41.   if( count == ID_BAOXIANJINE ||
    42.    count == ID_YONGJINBILV ||
    43.    count == ID_JINGBAOFEI ||
    44.    count == ID_HUANKUANJINE ||
    45.    count == ID_LIRUNBILV ||
    46.    count == ID_LIRUNJINE)
    47.   {
    48.    double tMoney = 0.0;
    49.    int rtn = sscanf_s(DbStr.GetString(), "%lf", &tMoney);

    50.    ASSERT(rtn == 1);

    51.    rc = sqlite3_bind_double(stmt, count, tMoney);
    52.   }
    53.   else
    54.   {
    55.    char *str = (char *)DbStr.GetString();
    56.    int c = strlen(str);
    57.    int c1 = DbStr.GetLength();

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

    62. if(rc != SQLITE_OK)
    63. {
    64.   CString ErrStr = sqlite3_errstr(rc);
    65.   MessageBox(ErrStr);

    66.   return;
    67. }
    68. }

    69. rc = sqlite3_step(stmt);

    70. if(rc != SQLITE_DONE)
    71. {
    72. if(rc == SQLITE_ERROR)
    73. {
    74.   CString DbErr;
    75.   DbErr.Format("Sql Insert failed, %s", sqlite3_errmsg(db));

    76.   MessageBox(DbErr);
    77. }
    78. else
    79. {
    80.   MessageBox("sqlite3_step Failed!");
    81. }
    82. }

    83. sqlite3_finalize(stmt);
    复制代码
    为什么呢?

    因为,sqlite3_bind_text绑定的text,需要在做:
    1. rc = sqlite3_step(stmt);
    复制代码
    的时候统一提交,而上面的代码使用的临时变量,
    1. rc = sqlite3_step(stmt);
    复制代码
    的时候,早就不存在了。因此乱码也是正常的。
    修改如下:
    1. sqlite3_stmt *stmt;
    2. CString sql = "insert into work values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    3. int rc = sqlite3_prepare_v2(db, sql.GetString(), -1, &stmt, NULL);

    4. if(rc != SQLITE_OK)
    5. {
    6. MessageBox("sqlite3_prepare_v2 Failed!");
    7. return;
    8. }

    9. count = 0;
    10. p_wnd = PrevWnd;

    11. CString DbStr[ID_TOTALCOUNT + 1];

    12. while(count++ < ID_TOTALCOUNT)
    13. {
    14. DbStr[count].Empty();

    15. p_wnd = CWnd::GetNextDlgTabItem(p_wnd, FALSE);
    16. if(p_wnd == NULL)
    17. {
    18.   return;
    19. }

    20. p_wnd->GetWindowText(DbStr[count]);

    21. do
    22. {
    23.   if(!DbStr[count].GetLength())
    24.   {
    25.   rc = sqlite3_bind_null(stmt, count);
    26.   break;
    27.   }

    28.   //日期相关
    29.   if( count == ID_CHUDANRIQI ||
    30.   count == ID_CHUFARIQI ||
    31.   count == ID_HUANKUANRIQI ||
    32.   count == ID_HUOLIRIQI)
    33.   {
    34.   CDateTimeCtrl *TimeCtl = (CDateTimeCtrl *)p_wnd;  
    35.   CString time = DateTimeToString(*TimeCtl);

    36.   DbStr[count] = time;

    37.   rc = sqlite3_bind_text(stmt, count, time.GetString(), time.GetLength(), SQLITE_STATIC);
    38.   }
    39.   else
    40.   {
    41.   //金钱相关的处理real类型
    42.   if( count == ID_BAOXIANJINE ||
    43.    count == ID_YONGJINBILV ||
    44.    count == ID_JINGBAOFEI ||
    45.    count == ID_HUANKUANJINE ||
    46.    count == ID_LIRUNBILV ||
    47.    count == ID_LIRUNJINE)
    48.   {
    49.    double tMoney = 0.0;
    50.    int rtn = sscanf_s(DbStr[count].GetString(), "%lf", &tMoney);

    51.    ASSERT(rtn == 1);

    52.    rc = sqlite3_bind_double(stmt, count, tMoney);
    53.   }
    54.   else
    55.   {
    56.    rc = sqlite3_bind_text(stmt, count, DbStr[count].GetString(), DbStr[count].GetLength(), SQLITE_STATIC);
    57.   }
    58.   }
    59. }while(0);

    60. if(rc != SQLITE_OK)
    61. {
    62.   CString ErrStr = sqlite3_errstr(rc);
    63.   MessageBox(ErrStr);

    64.   return;
    65. }
    66. }

    67. rc = sqlite3_step(stmt);

    68. if(rc != SQLITE_DONE)
    69. {
    70. if(rc == SQLITE_ERROR)
    71. {
    72.   CString DbErr;
    73.   DbErr.Format("Sql Insert failed, %s", sqlite3_errmsg(db));

    74.   MessageBox(DbErr);
    75. }
    76. else
    77. {
    78.   MessageBox("sqlite3_step Failed!");
    79. }
    80. }

    81. sqlite3_finalize(stmt);
    复制代码
    附上数据库创建的sql语法:
    1. sqlite> .dump work
    2. PRAGMA foreign_keys=OFF;
    3. BEGIN TRANSACTION;
    4. CREATE TABLE work (baodanhao text unique primary key , chudanriqi text,qudao text,lianxiren text,xiaoshou text,beibaorenxingming text,chufar
    5. iqi text,baoxianpinpai text,baoxianjihua text,baoxianjine real,yongjinbilv real,jingbaofei real,huankuanfangshi text,haikuanjine real,huanku
    6. anriqi text,shifouquane text,lirunbilv real,lirunjine real,huoliriqi text,fapiaojisong text,shifubaoxiangongsi text,beizhu text);
    复制代码
    总结
    以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

    来源:https://www.jb51.net/article/110054.htm
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    最新评论

    浏览过的版块

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表