CppSQLite3Statement用法和得到数据库表的某一字段的最大值或一张表数据的条数

BOOL InformationTable::PHtoGMDSqlite(const CString &strPHSqlPath,const CString &strGMDsqlPath)
{
	
	string strPHSelect ="select * from caseitem";
	string strGMDInsert = L"insert into _InformationTable(UID,AppId,State,Item,Contents,App) values(?,?,?,?,?,?)";
	try
	{
		CppSQLite3Query QueryContact = m_pPHSqlite->execQuery(strPHSelect);
		CppSQLite3Statement stmtSearchWord = m_pGMDSqlite->compileStatement(strGMDInsert);

		m_pGMDSqlite->execDML("begin transaction");
		CString strID;

		CString strName;
		for (int Flag = 0; !QueryContact.eof(); QueryContact.nextRow(),++Flag)
		{
			//结构体每个元素赋值
			InformationTableFieldName.strApp = QueryContact.getStringField("display_name");
			CString strTableName=L"_InformationTable";
			InformationTableFieldName.nUID=GetNextID(strTableName,"UID");
			InformationTableFieldName.nAppID = atoi(strID);
			InformationTableFieldName.strState =QueryContact.getStringField("deleted");
			InformationTableFieldName.strItem=L"IMEI";
			InformationTableFieldName.strContents = QueryContact.getStringField("item");
			
			//进行绑定
			stmtSearchWord.bind(1,InformationTableFieldName.nUID);
			stmtSearchWord.bind(2,InformationTableFieldName.nAppID);
			stmtSearchWord.bind(3,InformationTableFieldName.strState);
			stmtSearchWord.bind(4,InformationTableFieldName.strItem)
			stmtSearchWord.bind(5,InformationTableFieldName.strContents);
			stmtSearchWord.bind(6,InformationTableFieldName.strApp);
			stmtSearchWord.execDML();
			stmtSearchWord.reset();
		}
		m_pGMDSqlite->execDML("commit transaction");//提交
		QueryContact.finalize();//释放

	}
	catch (CppSQLite3Exception* e)
	{
		InsertDebugLog("CSearchWordTable::PHtoGMDSqlite数据库打开失败");
		return FALSE;
	}
	catch (CException* e)
	{
		InsertDebugLog("CSearchWordTable::PHtoGMDSqlite内存错误");
		return FALSE;
	}
	return TRUE;
}

//得到数据库中一张表某个字段的最大值或者这张表数据的条数(如果这张表中某一个 字段是0或1顺序排下来则这张表的数据条数为返回的值即strkeyName传入该字段)

strTable 表名  strKeyName 字段名

int CbaseSql::GetNextID(const CString & strTableName, const CString & strKeyName)
{
	try
	{
		int nID = -1;
		string strSQL = L"select max(" + strKeyName + L") as Id from " +  strTableName;
		CppSQLite3Query query = m_pGMDSqlite->execQuery(strSQL);

		if(query.fieldIsNull("Id"))
			return nID;

		nID = query.getIntField("Id") + 1;
		query.finalize();

		return nID;
	}
	catch (CppSQLite3Exception & e)
	{
		InsertDebugLog("CbaseSql::GetNextID");
		return -1;
	}
	catch (CException* e)
	{
		InsertDebugLog("CbaseSql::GetNextID");
		return -1;
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值