Sqlite3插入数据和查找数据

1.

//表单:Record(USN+有线MAC地址+无线MAC地址)
//查询
#define QUERY_SQL_SN "SELECT * FROM Record WHERE USN = '%s'"
#define QUERY_SQL_MAC "SELECT * FROM Record WHERE MAC = '%s'"
#define QUERY_SQL_WMAC "SELECT * FROM Record WHERE W_MAC = '%s'"
//插入
//#define INSERT_SQL_SN_MAC "INSERT INTO Record (USN, MAC, W_MAC, TIME) VALUES ('%s','%s','%s','%s');"
#define INSERT_SQL_SN_MAC "INSERT OR REPLACE INTO Record (USN, MAC, W_MAC, TIME) VALUES ('%s','%s','%s','%s');"
#define INSERT_SQL_SN_MAC_OLD "INSERT  INTO RepeatRecord (USN, MAC, W_MAC, TIME) VALUES ('%s','%s','%s','%s');" 
//insert or replace:如果不存在就插入,存在就更新
//insert or ignore:如果不存在就插入,存在就忽略

2.

   //NOT NULL - 非空
	//UNIQUE - 唯一
	//PRIMARY KEY - 主键
	//FOREIGN KEY - 外键
	//CHECK - 条件检查
	//DEFAULT - 默认
static const char* create_sql =	"CREATE TABLE  Record("\
								"USN  CHAR(20) PRIMARY KEY UNIQUE NOT NULL,"\
								"MAC  CHAR(20) UNIQUE,"\
								"W_MAC  CHAR(20) UNIQUE,"\
								"TIME  CHAR(20)) ;";

static const char* create_EntrySql =  "CREATE TABLE  RepeatRecord("\
	                                  "USN  CHAR(20) ,"\
	                                  "MAC  CHAR(20) ,"\
	                                  "W_MAC  CHAR(20) ,"\
	                                  "TIME  CHAR(20)) ;";
DB::DB(void):m_pDB(NULL)
{
}

DB::~DB(void)
{
	if (m_pDB)
	{
		sqlite3_close(m_pDB);
		m_pDB = NULL;
	}
}

bool DB::OpenDB(const char* name, sqlite3 **ppDB)
{
	int ret = sqlite3_open(name, &m_pDB);
	if (SQLITE_OK != ret)
	{
		AfxMessageBox(_T("打开数据库失败"));
		return false;
	}
	return true;
}

bool DB::CreateTable(const char* sql)
{
	if (!m_pDB || !sql) return false;
	char *errMsg = NULL;
	int ret = sqlite3_exec(m_pDB, sql, 0, 0, &errMsg);
	if (SQLITE_OK != ret)
	{
		CString strMsg;
		strMsg.Format("创建表失败,错误信息:%s", errMsg);
		AfxMessageBox(strMsg);
		sqlite3_free(errMsg);
		return false;
	}
	if (errMsg) sqlite3_free(errMsg);
	return true;
}

bool DB::InsertToDB(const char* dbName, char* sql, int Control)
{
	if (!dbName || !sql) return false;
	bool bCreateTbl = true;
	if (-1 == _access(dbName, 0))
		bCreateTbl = false;
	bool bSuccess = OpenDB(dbName, &m_pDB);
	if (!bSuccess) return false;
	if (!bCreateTbl)
	{
 		if(Control == 0)
		bSuccess = CreateTable(create_sql);
 		else
 		bSuccess = CreateTable(create_EntrySql);
// 		if (!bSuccess) return false;
	}
	if (!m_pDB) return false;
	char *errMsg = NULL;
	int ret = sqlite3_exec(m_pDB, sql, 0, 0, &errMsg);
	if (SQLITE_OK != ret)
	{
		CString strMsg;
		strMsg.Format("数据库插入失败,错误信息:%s", errMsg);
		AfxMessageBox(strMsg);
		sqlite3_free(errMsg);
		return false;
	}
	else
	{
		//AfxMessageBox("插入数据库成功!");
	}
	if (errMsg) sqlite3_free(errMsg);
	return true;
}

bool DB::QueryDB(const char* dbName, char* sql, CString& strResult)
{
	if (!dbName || !sql) return false;
	if (-1 == _access(dbName, 0)) return false;
	bool bSuccess = OpenDB(dbName, &m_pDB);
	if (!bSuccess) return false;
	if (!m_pDB) return false;
	char *errMsg = NULL;
	char** pResult = NULL;
	int rowCount = 0, colCount = 0;
	int ret = sqlite3_get_table(m_pDB, sql, &pResult, &rowCount, &colCount, &errMsg);
	if (SQLITE_OK != ret)
	{
		CString strMsg;
		strMsg.Format("数据库查询失败,错误信息:%s", errMsg);
		AfxMessageBox(strMsg);
		sqlite3_free(errMsg);
		return false;
	}
	//"INSERT OR REPLACE INTO Record (USN, MAC, W_MAC, TIME) VALUES ('%s','%s','%s','%s');"
	int index = colCount;
	CString pData[10]={0};
	for (int i = 0; i < rowCount; ++i)
	{
		for (int j = 0; j < colCount; ++j)
		{
			//strResult += pResult[j];
			//strResult += ":";
			//strResult += pResult[index];
			//strResult += "#";
			pData[j]=pResult[index];
			++ index;
		}
		//strResult += "&&";
	   strResult.Format(INSERT_SQL_SN_MAC_OLD,pData[0],pData[1],pData[2],pData[3]);
	  // sprintf_s(strResult,sizeof(strResult),QUERY_SQL_WMAC,pData[0],pData[1],pData[2],pData[3]);

	}
	//TRACE(strResult);

	if (pResult) sqlite3_free_table(pResult);
	if (errMsg) sqlite3_free(errMsg);
	return true;
}

bool DB::UpdateDB(const char* dbName, char* sql)
{
	if (!dbName || !sql) return false;
	if (-1 == _access(dbName, 0)) return false;
	bool bSuccess = OpenDB(dbName, &m_pDB);
	if (!bSuccess) return false;
	if (!m_pDB) return false;
	char *errMsg = NULL;
	int ret = sqlite3_exec(m_pDB, sql, 0, 0, &errMsg);
	if (SQLITE_OK != ret)
	{
		CString strMsg;
		strMsg.Format("数据库修改失败,错误信息:%s", errMsg);
		AfxMessageBox(strMsg);
		sqlite3_free(errMsg);
		return false;
	}
	if (errMsg) sqlite3_free(errMsg);
	return true;
}

bool DB::DeleteDB(const char* dbName, char* sql)
{
	if (!dbName || !sql) return false;
	if (-1 == _access(dbName, 0)) return false;
	bool bSuccess = OpenDB(dbName, &m_pDB);
	if (!bSuccess) return false;
	if (!m_pDB) return false;
	char *errMsg = NULL;
	int ret = sqlite3_exec(m_pDB, sql, 0, 0, &errMsg);
	if (SQLITE_OK != ret)
	{
		CString strMsg;
		strMsg.Format("数据库删除失败,错误信息:%s", errMsg);
		AfxMessageBox(strMsg);
		sqlite3_free(errMsg);
		return false;
	}
	if (errMsg) sqlite3_free(errMsg);
	return true;
}

3.

BOOL CXVRProductCheckDlg::PreTranslateMessage(MSG* pMsg)
{
	//if (WM_KEYFIRST <= pMsg-> message && pMsg-> message <= WM_KEYLAST) 
	if(pMsg->message==WM_KEYDOWN)//键盘按下
	{
	 if(GetFocus()->GetDlgCtrlID() == IDC_SN)//绑定SN编辑框ID
	 {
		
         if(pMsg-> wParam==VK_RETURN ) //回车事件
		{
			UpdateData(TRUE);
			DB db;
			int x=0,y=0,z=0;
		    char szSql[1024] = {0};
			char szSqlUsn[1024]={0};
			char szSqlMac[1024]={0};
			char szSqlWmac[1024]={0};
			CString strResultUsn;
			CString strResultMac;
			CString strResultWmac;
			CString strSnMac;
			CString strWiredMac = m_dev.wiredmac;
			CString strWifiMac  =m_dev.wifimac;
			CString strPrint;//SN+有线MAC+无线MAC
			m_snmac.GetWindowText(strSnMac);
			CTime time;
			time = CTime::GetCurrentTime();
			CString strNowTime = time.Format("%Y-%m-%d;%H:%M:%S");//获取当前时间

			if((strWiredMac.IsEmpty()!=0)||(strWifiMac.IsEmpty()!=0)||(strSnMac.IsEmpty()!=0))
			//if(strSnMac.IsEmpty()!=0)
			{
			  MessageBox("USN,有线MAC和无线MAC地址不能为空!","提示",MB_ICONSTOP|MB_OK);
		    }
			else
			{
			    sprintf_s(szSqlUsn,sizeof(szSqlUsn),QUERY_SQL_SN,strSnMac);
				sprintf_s(szSqlMac,sizeof(szSqlMac),QUERY_SQL_MAC,strWiredMac);
				sprintf_s(szSqlWmac,sizeof(szSqlWmac),QUERY_SQL_WMAC,strWifiMac);
				sprintf_s(szSql, sizeof(szSql),INSERT_SQL_SN_MAC,strSnMac,strWiredMac,strWifiMac,strNowTime);
			  //分别查询USN,MAC,W_MAC,有一个重复都算重复
			  db.QueryDB("my_sql.db",szSqlUsn,strResultUsn);
			  if(strResultUsn.IsEmpty()==0)
			  {   x=1;
			      //MessageBox(strResultUsn,"提示",MB_ICONSTOP|MB_OK);
				 // db.InsertToDB("Oldsql.db", (LPSTR)(LPCTSTR)strResultUsn,1);
			  }
			  db.QueryDB("my_sql.db",szSqlMac,strResultMac);
			  if(strResultMac.IsEmpty()==0)
			  {    y=1;
				  //db.InsertToDB("Oldsql.db", (LPSTR)(LPCTSTR)strResultMac,1);
			  }
			  db.QueryDB("my_sql.db",szSqlWmac,strResultWmac);
			  if(strResultWmac.IsEmpty()==0)
			  {   z=1;
				 // db.InsertToDB("Oldsql.db", (LPSTR)(LPCTSTR)strResultWmac,1);
			  }
			   point dlg;
			  if((x+y+z)>0)
			  {
				  dlg.m_usn =x;
				  dlg.m_mac =y;
				  dlg.m_wmac=z;
				  dlg.DoModal();//提示框,输入123继续打印否则取消

			  }
			  else
			  {
				  db.InsertToDB("my_sql.db", szSql,0);
				  strPrint.Format("%s,%s,%s",strSnMac,strWiredMac,strWifiMac);
				 // MessageBox(strPrint,"提示",MB_ICONSTOP|MB_OK);
				  char szUSn[100] = {0};
				  memcpy(szUSn, strPrint, strlen(strPrint)+1);
				  szUSn[100] = '\0';
				  if( NULL == "MAC.fr3") return FALSE;
				  HINSTANCE hDllFile = NULL;
				  hDllFile = LoadLibrary("JuanReport.dll");
				  DLL_API_BarPrint BarPrint=NULL;
				  BarPrint =  (DLL_API_BarPrint)GetProcAddress(hDllFile, "PrintReport");
				  BarPrint(szUSn, NULL, "MAC.fr3");
				  FreeLibrary(hDllFile);
				  BarPrint = NULL;
				  hDllFile = NULL;
			  }
			  if(dlg.m_Print==1)
			  {
				  if(x==1) db.InsertToDB("Oldsql.db", (LPSTR)(LPCTSTR)strResultUsn,1);
				  if(y==1) db.InsertToDB("Oldsql.db", (LPSTR)(LPCTSTR)strResultMac,1);
                  if(z==1) db.InsertToDB("Oldsql.db", (LPSTR)(LPCTSTR)strResultWmac,1);
				  
				  db.InsertToDB("my_sql.db", szSql,0);
				  strPrint.Format("%s,%s,%s",strSnMac,strWiredMac,strWifiMac);
				  // MessageBox(strPrint,"提示",MB_ICONSTOP|MB_OK);
				  char szUSn[100] = {0};
				  memcpy(szUSn, strPrint, strlen(strPrint)+1);
				  szUSn[100] = '\0';
				  if( NULL == "MAC.fr3") return FALSE;
				  HINSTANCE hDllFile = NULL;
				  hDllFile = LoadLibrary("JuanReport.dll");
				  DLL_API_BarPrint BarPrint=NULL;
				  BarPrint =  (DLL_API_BarPrint)GetProcAddress(hDllFile, "PrintReport");
				  BarPrint(szUSn, NULL, "MAC.fr3");
				  FreeLibrary(hDllFile);
				  BarPrint = NULL;
				  hDllFile = NULL;
			  }
			}
			
			return TRUE;//除了截获的消息,其他返回应该全部为假
		} 
	  } 
	}
	return FALSE;
	//return CDialogEx::PreTranslateMessage(pMsg);
}	

 

你可能感兴趣的:(数据库,Sqlite3,MFC)