Sqlite3数据块以BLOB字段存储二进制数据块

int CppSQLite3DB::execDMLBlob(const char* szSQL,const char* blob1,const char* blob2,const char* blob3,const char* blob4,int length)
{
	checkDB();

	char* szError=0;
	const char *error;

	sqlite3_stmt      *stmt = 0;
	int nRet = sqlite3_prepare(mpDB, szSQL,strlen(szSQL), &stmt, &error);
	if (nRet != SQLITE_OK)
	{
		//获取SQLITE错误
	}

	//SQL语句中的下面四个参数绑定到数据块中
	int	index;
	index = sqlite3_bind_parameter_index(stmt, ":aaa");
	nRet = sqlite3_bind_blob(stmt, index, blob1, length, NULL);

	index = sqlite3_bind_parameter_index(stmt, ":bbb");
	nRet = sqlite3_bind_blob(stmt, index, blob2, length, NULL);

	index = sqlite3_bind_parameter_index(stmt, ":ccc");
	nRet = sqlite3_bind_blob(stmt, index, blob3, length, NULL);

	index = sqlite3_bind_parameter_index(stmt, ":ddd");
	nRet = sqlite3_bind_blob(stmt, index, blob4, length, NULL);

	if (nRet != SQLITE_OK)
	{
		//获取SQLITE错误,打印出来
	}
	//执行SQL语句
	nRet = sqlite3_step(stmt);
	if (nRet != SQLITE_DONE)
	{
		//获取SQLITE错误,打印出来
	}

	sqlite3_finalize(stmt); 

	return SQLITE_OK;
}

 

 

	MTK::CMTKSqliteEX	sqliteDBInsert;//自己封装后的sqlite3类
	sqliteDBInsert.open(filePathDB);

	char szCommand[MAX_BUF_LEN] = {0};
	memset(szCommand, 0, MAX_BUF_LEN);
	sprintf(szCommand, "insert into T_RIM_RTM_SAMPLE_DATA values (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12,?13,?14,?15,?16,?17,?18,?19,?20)");
	sqliteDBInsert.prepare_v2(szCommand);


	//当游标移动到文件尾时说明解析完毕
	while(m_curPosition != m_totalByte)
	{
		//解析一次m_curPosition往后移动一次
		//解析内存数据,每次解析1000条数据
		AnalyzeBinData(curFilePahtName);


		//开启事务
		sqliteDBInsert.execDML("begin transaction;");

		//解析一次存一次
		vector::iterator iter;
		for(iter=m_vecCurShowData.begin();iter!=m_vecCurShowData.end();iter++)
		{
			DataAcquisiton	tempdataAcq;
			tempdataAcq = *iter;

			sqliteDBInsert.bind(1, (__int64)tempdataAcq.head);
			sqliteDBInsert.bind(2, (int)tempdataAcq.moniStation);
			sqliteDBInsert.bind(3, (int)tempdataAcq.devParamSetAndAnt);
			sqliteDBInsert.bind(4, (int)tempdataAcq.moniTimeYear);
			sqliteDBInsert.bind(5, (int)tempdataAcq.moniTimeMouth);
			sqliteDBInsert.bind(6, (int)tempdataAcq.moniTimeDay);
			sqliteDBInsert.bind(7, (int)tempdataAcq.starMoniTimeHour);
			sqliteDBInsert.bind(8, (int)tempdataAcq.starMoniTimeMinu);
			sqliteDBInsert.bind(9, (int)tempdataAcq.starMoniTimeSeco);
			sqliteDBInsert.bind(10,(int)tempdataAcq.starMoniTimeMilli);
			sqliteDBInsert.bind(11,tempdataAcq.longitude);
			sqliteDBInsert.bind(12,tempdataAcq.latitude);
			sqliteDBInsert.bind(13,(DOUBLE)tempdataAcq.altitude);
			sqliteDBInsert.bind(14,(__int64)tempdataAcq.startFrequency);
			sqliteDBInsert.bind(15,(int)tempdataAcq.step);
			sqliteDBInsert.bind(16,(int)tempdataAcq.frePointNum);

			int totallength = tempdataAcq.frePointNum * 2;
			sqliteDBInsert.bind(18,(const UCHAR*)tempdataAcq.pField,totallength);	//bin存储的为场强值
			sqliteDBInsert.bind(17,(const UCHAR*)tempdataAcq.pdata,totallength);
			sqliteDBInsert.bind(19,(const UCHAR*)tempdataAcq.pGate,totallength);
			sqliteDBInsert.bind(20,(const UCHAR*)tempdataAcq.pOcc,totallength);

			if (SQLITE_DONE != (sqliteDBInsert.step())) break;

			if (SQLITE_OK != (sqliteDBInsert.reset())) break;

		}

		//提交事务
		sqliteDBInsert.execDML("commit transaction;");

		//释放内存
		vector::iterator iterFree;
		for(iterFree=m_vecCurShowData.begin();iterFree!=m_vecCurShowData.end();iterFree++)
		{
			DataAcquisiton	tempdataAcqFre;
			tempdataAcqFre = *iterFree;

			delete [] tempdataAcqFre.pdata;
			tempdataAcqFre.pdata = NULL;

			delete [] tempdataAcqFre.pField;
			tempdataAcqFre.pField = NULL;

			delete [] tempdataAcqFre.pGate;
			tempdataAcqFre.pGate = NULL;

			delete [] tempdataAcqFre.pOcc;
			tempdataAcqFre.pOcc = NULL;
		}

		if (m_curPosition<=m_totalByte)
		{
			float posTemp = (float)m_curPosition/m_totalByte;
			int pos = (int)(posTemp*100);

			//CString str;
			//str.Format("%d%%",pos);
			//m_listCtrl.SetItemText(index,2,str);

			::PostMessage(m_pParent->m_hWnd, UPDATAUI, (WPARAM)m_index, (LPARAM)pos);

			//m_progressCtr.SetPos(pos);
		}
	}

	//结束
	sqliteDBInsert.finalPrepare();

	sqliteDBInsert.close();

 

 

 

 

 

你可能感兴趣的:(C/C++)