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

随心笔谈12个月前发布 admin
82 0


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

© 版权声明

相关文章