КОД ПРОГРАММЫ (ПРИЛОЖЕНИЕ А)
// LizaDlg.cpp : implementation file
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CString g_strSqlDbPath = _T("");
CString g_strRootPath = _T("");
static size_t g_uiCurReserver = 1000;
namespace wnd_size
{ const int MAX_WIDTH = 1280;
const int MAX_HEIGHT = 1024;
const int MIN_WIDTH = 640;
const int MIN_HEIGHT = 200;};
class CAboutDlg : public CDialogEx
{public: CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected: DECLARE_MESSAGE_MAP()};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD){}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{ CDialogEx::DoDataExchange(pDX);}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CLizaDlg dialog
CLizaDlg::CLizaDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CLizaDlg::IDD, pParent)
{ m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_vecData.reserve(g_uiCurReserver);
m_strPeopleList = _T("");
for (size_t i = 0; i < MAX_PATH; i++)
{ m_szPath[i] = 0; }
m_pSql = NULL;
m_strSqlDbPath = _T("");}
CLizaDlg::~CLizaDlg(){ Cleanup(m_vecData); }
void CLizaDlg::DoDataExchange(CDataExchange* pDX)
{ CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST_PEOPLE, m_cListPeople);
DDX_Control(pDX, IDC_EDIT_SQL_DB_PATH, m_сSqlDbPath);
DDX_Text(pDX, IDC_EDIT_SQL_DB_PATH, m_strSqlDbPath);
DDX_Control(pDX, IDC_LIST_PEOPLE, m_cListPeople); }
BEGIN_MESSAGE_MAP(CLizaDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_ADD_BUTTON, &CLizaDlg::OnAdd)
ON_BN_CLICKED(IDC_EDIT_BUTTON, &CLizaDlg::OnEdit)
ON_BN_CLICKED(IDC_DELETE_BUTTON, &CLizaDlg::OnDelete)
ON_COMMAND(ID_ADD, &CLizaDlg::OnAdd)
ON_COMMAND(ID_DELETE, &CLizaDlg::OnDelete)
ON_COMMAND(ID_EDIT, &CLizaDlg::OnEdit)
ON_COMMAND(ID_SEARCH, &CLizaDlg::OnSearch)
ON_COMMAND(ID_VIEW, &CLizaDlg::OnView)
ON_WM_CONTEXTMENU()
ON_WM_GETMINMAXINFO()
ON_WM_SIZE()
ON_WM_KEYDOWN()
ON_BN_CLICKED(IDCANCEL, &CLizaDlg::OnBnClickedCancel)
ON_BN_CLICKED(IDC_SEARCH_BUTTON, &CLizaDlg::OnSearch)
ON_BN_CLICKED(IDC_VIEW_BUTTON, &CLizaDlg::OnView)
ON_BN_CLICKED(IDC_BUTTON_SELECT_SQL_DB, &CLizaDlg::OnBnClickedSelectSqlDb)
ON_BN_CLICKED(IDC_BUTTON_CONNECT, &CLizaDlg::OnBnClickedConnect)
END_MESSAGE_MAP()
// CLizaDlg message handlers
BOOL CLizaDlg::PreTranslateMessage(MSG* pMsg)
{ UINT uiMsg = pMsg->message;
WPARAM wParam = pMsg->wParam;
HWND hWnd = pMsg->hwnd;
if (uiMsg == WM_LBUTTONDBLCLK)
{ CWnd* pFocus = GetFocus();
int iFocusId = pFocus->GetDlgCtrlID();
if (iFocusId == IDC_LIST_PEOPLE)
{ OnView();
return TRUE; } }
if (uiMsg == WM_KEYDOWN)
{ if (wParam == VK_INSERT)
{ CWnd* pFocus = GetFocus();
int iFocusId = pFocus->GetDlgCtrlID();
if (iFocusId == IDC_LIST_PEOPLE)
{ OnAdd();
return TRUE; } }
else if (wParam == VK_RETURN)
{ CWnd* pFocus = GetFocus();
int iFocusId = pFocus->GetDlgCtrlID();
if (iFocusId == IDC_LIST_PEOPLE)
{ if ((GetKeyState(VK_CONTROL) & 0x8000) == 0x8000)
{ OnEdit();
return TRUE; }
else
{ OnView();
return TRUE; }}}
else if (wParam == VK_DELETE)
{ CWnd* pFocus = GetFocus();
int iFocusId = pFocus->GetDlgCtrlID();
if (iFocusId == IDC_LIST_PEOPLE)
{ OnDelete(); return TRUE;} }
else if (wParam == VK_F)
{ if ((GetKeyState(VK_CONTROL) & 0x8000) == 0x8000)
{ OnSearch();
return TRUE;} } }
return CDialogEx::PreTranslateMessage(pMsg); }
BOOL CLizaDlg::DestroyWindow()
{ CRect rcWnd;
GetWindowRect(&rcWnd);
AfxGetApp()->WriteProfileInt(_T(""), _T("x"), rcWnd.left);
AfxGetApp()->WriteProfileInt(_T(""), _T("y"), rcWnd.top);
AfxGetApp()->WriteProfileInt(_T(""), _T("width"), rcWnd.Width());
AfxGetApp()->WriteProfileInt(_T(""), _T("height"), rcWnd.Height());
AfxGetApp()->WriteProfileString(_T(""), _T("sql_db_path"), g_strSqlDbPath);
AfxGetApp()->WriteProfileString(_T(""), _T("root_path"), g_strRootPath);
return CDialogEx::DestroyWindow(); }
BOOL CLizaDlg::OnInitDialog()
{ CDialogEx::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{ BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{ pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);} }
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CRect rcWnd;
rcWnd.left = AfxGetApp()->GetProfileInt(_T(""), _T("x"), 0);
rcWnd.top = AfxGetApp()->GetProfileInt(_T(""), _T("y"), 0);
rcWnd.right=rcWnd.left+AfxGetApp()->GetProfileInt(_T(""),_T("width"),wnd_size::MIN_WIDTH);
rcWnd.bottom=rcWnd.top+AfxGetApp()-> GetProfileInt(_T(""),_T("height"),wnd_size::MIN_HEIGHT);
SetWindowPos(NULL, rcWnd.left, rcWnd.top, rcWnd.Width()+1,rcWnd.Height()+1, SWP_NOZORDER);
SetWindowPos(NULL, rcWnd.left, rcWnd.top, rcWnd.Width(), rcWnd.Height(), SWP_NOZORDER);
g_strRootPath = AfxGetApp()->GetProfileString(_T(""), _T("root_path"), _T(""));
g_strSqlDbPath = AfxGetApp()->GetProfileString(_T(""), _T("sql_db_path"), _T(""));
m_strSqlDbPath = g_strSqlDbPath;
UpdateData(FALSE);
int iWidth = (rcWnd.Width() - 20) / 15;
m_cListPeople.InsertColumn(0, _T("ID"), 0, iWidth);
m_cListPeople.InsertColumn(1, _T("Имя"), 0, iWidth);
m_cListPeople.InsertColumn(2, _T("Фамилия"), 0, iWidth);
m_cListPeople.InsertColumn(3, _T("Отчество"), 0, iWidth);
m_cListPeople.InsertColumn(4, _T("Страна"), 0, iWidth);
m_cListPeople.InsertColumn(5, _T("Область"), 0, iWidth);
m_cListPeople.InsertColumn(6, _T("Город"), 0, iWidth);
m_cListPeople.InsertColumn(7, _T("Улица"), 0, iWidth);
m_cListPeople.InsertColumn(8, _T("Номер дома"), 0, iWidth);
m_cListPeople.InsertColumn(9, _T("Номер квартиры"), 0, iWidth);
m_cListPeople.InsertColumn(10, _T("Телефон"), 0, iWidth);
m_cListPeople.InsertColumn(11, _T("Дополнительная информация"), 0, iWidth);
return TRUE; // return TRUE unless you set the focus to a control }
void CLizaDlg::OnSysCommand(UINT nID, LPARAM lParam)
{ if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{ CAboutDlg dlgAbout;
dlgAbout.DoModal();}
else {CDialogEx::OnSysCommand(nID, lParam);}}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CLizaDlg::OnPaint()
{ if (IsIconic())
{ CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);}
else {CDialogEx::OnPaint(); } }
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CLizaDlg::OnQueryDragIcon()
{ return static_cast<HCURSOR>(m_hIcon); }
void CLizaDlg::OnDestroy()
{ CDialogEx::OnDestroy();
Cleanup(m_vecData);
sqlite3_close(m_pSql); }
/*static*/
void CLizaDlg::Cleanup(vector<CPeople*>& vecData)
{ for (size_t i = 0; i < vecData.size(); i++) {delete vecData[i];}
vecData.erase(vecData.begin(), vecData.end()); }
void CLizaDlg::OnAdd()
{ CPeople* pRec = new CPeople;
SelectNextPeopleId(pRec, m_pSql);
CEditData* pDlg = new CEditData(pRec, this);
int iRet = pDlg->DoModal();
if (iRet == IDOK)
{ InsertPeople(pRec, m_pSql);
SelectPeople(m_vecData, m_pSql);
InitPeopleList();}
else {delete pRec;}
delete pDlg; }
void CLizaDlg::OnEdit()
{ UpdateData();
CString str;
int iRow = m_cListPeople.GetSelectionMark();
if (m_vecData.size() == 0)
{ str.Format(_T("Записей больше нету.\n"), iRow);
MessageBox(str, _T("Редактирование записи"), MB_ICONWARNING);
return; }
if (iRow == -1)
{ str.Format(_T("Запись не выбрана.\n"), iRow);
MessageBox(str, _T("Редактирование записи"), MB_ICONWARNING);
return; }
CPeople* pRec = m_vecData[iRow];
CPeople* pRecCopy = new CPeople(*m_vecData[iRow]);
CEditData* pDlg = new CEditData(pRecCopy, this);
pDlg->m_iMode = REC_EDIT;
int iRet = pDlg->DoModal();
if (iRet == IDOK)
{ CPeople* pTmp = pRec;
m_vecData[iRow] = pRecCopy;
delete pTmp;
UpdatePeople(pRecCopy, m_pSql);
SelectPeople(m_vecData, m_pSql);
InitPeopleList();}
else {delete pRecCopy;}
delete pDlg; }
void CLizaDlg::OnSearch()
{ CPeople* pRecSearch = new CPeople();
CEditData* pDlg = new CEditData(pRecSearch, this);
pDlg->m_iMode = REC_SEARCH;
int iRet = pDlg->DoModal();
if (iRet == IDOK)
{ if (pDlg->m_bIsSelectAllRec)
{ SelectPeople(m_vecData, m_pSql);
InitPeopleList(); }
else {SelectPeople(m_vecData, m_pSql, pRecSearch);
InitPeopleList();}}
delete pRecSearch;
delete pDlg;}
void CLizaDlg::OnDelete()
{ UpdateData();
CString str;
int iRow = m_cListPeople.GetSelectionMark();
if (m_vecData.size() == 0)
{ str.Format(_T("Записей больше нету.\n"), iRow);
MessageBox(str, _T("Удаление записи"), MB_ICONWARNING);
return; }
if (iRow == -1)
{ str.Format(_T("Запись не выбрана.\n"), iRow);
MessageBox(str, _T("Удаление записи"), MB_ICONWARNING);
return; }
CPeople* pRec = m_vecData[iRow];
CEditData* pDlg = new CEditData(pRec, this);
pDlg->m_iMode = REC_DELETE;
int iRet = pDlg->DoModal();
if (iRet == IDOK)
{ CString strImgPath, strBuff;
strImgPath.Format(_T("%s\\img\\%s.jpg"), g_strRootPath, pRec->GetId(strBuff));
DeleteFile(strImgPath);
DeletePeople(pRec, m_pSql);
SelectPeople(m_vecData, m_pSql);
InitPeopleList(); }
delete pDlg;}
void CLizaDlg::OnView()
{ UpdateData();
CString str;
int iRow = m_cListPeople.GetSelectionMark();
if (iRow == -1)
{ str.Format(_T("Запись не выбрана.\n"), iRow);
MessageBox(str, _T("Редактирование записи"), MB_ICONWARNING);
return; }
CPeople* pRec = m_vecData[iRow];
CEditData* pDlg = new CEditData(pRec, this);
pDlg->m_iMode = REC_VIEW;
int iRet = pDlg->DoModal();
delete pDlg; }
void CLizaDlg::OnContextMenu(CWnd* pWnd, CPoint point)
{ CMenu menu;
menu.LoadMenu(IDR_MENU);
CMenu* pMenu = menu.GetSubMenu(0);
CRect rcCtrl;
CWnd* pCtrl = GetDlgItem(IDC_LIST_PEOPLE);
::GetWindowRect(pCtrl->GetSafeHwnd(), &rcCtrl);
if (rcCtrl.PtInRect(point))
{pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);}}
void CLizaDlg::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{ lpMMI->ptMaxTrackSize.x = wnd_size::MAX_WIDTH;
lpMMI->ptMaxTrackSize.y = wnd_size::MAX_HEIGHT;
lpMMI->ptMinTrackSize.x = wnd_size::MIN_WIDTH;
lpMMI->ptMinTrackSize.y = wnd_size::MIN_HEIGHT;
CDialogEx::OnGetMinMaxInfo(lpMMI);}
void CLizaDlg::OnSize(UINT nType, int cx, int cy)
{ CDialogEx::OnSize(nType, cx, cy);
MoveCtrl(nType, cx, cy);}
void CLizaDlg::MoveCtrl(UINT nType, int cx, int cy)
{ int iX = 0;
int iY = 0;
int iWidth = 0;
int iHeight = 0;
CWnd* pCtrl = NULL;
HWND hCtrl = NULL;
CRect rcCtrl;
int iFooterHeight = 50;
int iMargin = 0;
// список
pCtrl = GetDlgItem(IDC_LIST_PEOPLE);
hCtrl = pCtrl->GetSafeHwnd();
::GetWindowRect(hCtrl, rcCtrl);
ScreenToClient(&rcCtrl);
iX = 10;
iY = 10;
iWidth = cx - 10 * 2;
iHeight = cy - 75 - iFooterHeight;
::SetWindowPos(hCtrl, NULL, iX, iY, iWidth, iHeight, SWP_NOMOVE | SWP_NOZORDER);
// просмотреть
pCtrl = GetDlgItem(IDC_VIEW_BUTTON);
hCtrl = pCtrl->GetSafeHwnd();
::GetWindowRect(hCtrl, rcCtrl);
ScreenToClient(&rcCtrl);
iX = 10;
iY = cy - rcCtrl.Height() - 10;
iWidth = rcCtrl.Width();
iHeight = rcCtrl.Height();
::SetWindowPos(hCtrl, NULL, iX, iY, iWidth, iHeight, SWP_NOSIZE | SWP_NOZORDER);
iMargin = iX + rcCtrl.Width() + 5;
// поиск
pCtrl = GetDlgItem(IDC_SEARCH_BUTTON);
hCtrl = pCtrl->GetSafeHwnd();
::GetWindowRect(hCtrl, rcCtrl);
ScreenToClient(&rcCtrl);
iX = iMargin;
iY = cy - rcCtrl.Height() - 10;
iWidth = rcCtrl.Width();
iHeight = rcCtrl.Height();
::SetWindowPos(hCtrl, NULL, iX, iY, iWidth, iHeight, SWP_NOSIZE | SWP_NOZORDER);
iMargin += (rcCtrl.Width() + 5);
// добавить
pCtrl = GetDlgItem(IDC_ADD_BUTTON);
hCtrl = pCtrl->GetSafeHwnd();
::GetWindowRect(hCtrl, rcCtrl);
ScreenToClient(&rcCtrl);
iX = iMargin;
iY = cy - rcCtrl.Height() - 10;
iWidth = rcCtrl.Width();
iHeight = rcCtrl.Height();
::SetWindowPos(hCtrl, NULL, iX, iY, iWidth, iHeight, SWP_NOSIZE | SWP_NOZORDER);
iMargin += (rcCtrl.Width() + 5);
// редактировать
pCtrl = GetDlgItem(IDC_EDIT_BUTTON);
hCtrl = pCtrl->GetSafeHwnd();
::GetWindowRect(hCtrl, rcCtrl);
ScreenToClient(&rcCtrl);
iX = iMargin;
iY = cy - rcCtrl.Height() - 10;
iWidth = rcCtrl.Width();
iHeight = rcCtrl.Height();
::SetWindowPos(hCtrl, NULL, iX, iY, iWidth, iHeight, SWP_NOSIZE | SWP_NOZORDER);
iMargin += (rcCtrl.Width() + 5);
// удалить
pCtrl = GetDlgItem(IDC_DELETE_BUTTON);
hCtrl = pCtrl->GetSafeHwnd();
::GetWindowRect(hCtrl, rcCtrl);
ScreenToClient(&rcCtrl);
iX = iMargin;
iY = cy - rcCtrl.Height() - 10;
iWidth = rcCtrl.Width();
iHeight = rcCtrl.Height();
::SetWindowPos(hCtrl, NULL, iX, iY, iWidth, iHeight, SWP_NOSIZE | SWP_NOZORDER);
iMargin += (rcCtrl.Width() + 5);
// выйти
pCtrl = GetDlgItem(IDCANCEL);
hCtrl = pCtrl->GetSafeHwnd();
::GetWindowRect(hCtrl, rcCtrl);
ScreenToClient(&rcCtrl);
iX = cx - 10 - rcCtrl.Width();
iY = cy - rcCtrl.Height() - 10;
iWidth = rcCtrl.Width();
iHeight = rcCtrl.Height();
::SetWindowPos(hCtrl, NULL, iX, iY, iWidth, iHeight, SWP_NOSIZE | SWP_NOZORDER);
iMargin = rcCtrl.Width() + 5;
RedrawWindow(); }
bool CLizaDlg::CreateConnect()
{ bool bRet = true;
int iRet = sqlite3_open16(g_strSqlDbPath, &m_pSql);
if (iRet) bRet = false;
return bRet; }
/*static*/
void CLizaDlg::SelectPeople(vector<CPeople*>& vecData, sqlite3* pSql, CPeople* pRecSearch /*= NULL*/)
{ Cleanup(vecData);
CPeople* pRec = NULL;
size_t i = 0;
size_t nSize = MAX_PATH * 2;
sqlite3_stmt* pStmt;
CString strSql, strDummy;
if (!pRecSearch)
{ strSql = _T("select * from People");}
else
{ map<CString, CString> mapParam;
if (pRecSearch->m_mapMandatoryParam[_T("FirstName")])
{ mapParam[_T("FirstName")] = pRecSearch->GetFirstName(strDummy);}
if (pRecSearch->m_mapMandatoryParam[_T("SecondName")])
{mapParam[_T("SecondName")] = pRecSearch->GetSecondName(strDummy);}
if (pRecSearch->m_mapMandatoryParam[_T("MiddleName")])
{ mapParam[_T("MiddleName")] = pRecSearch->GetMiddleName(strDummy);}
if (pRecSearch->m_mapMandatoryParam[_T("Country")])
{ mapParam[_T("Country")] = pRecSearch->GetCountry(strDummy);}
if (pRecSearch->m_mapMandatoryParam[_T("Locality")])
{ mapParam[_T("Locality")] = pRecSearch->GetLocality(strDummy);}
if (pRecSearch->m_mapMandatoryParam[_T("City")])
{ mapParam[_T("City")] = pRecSearch->GetCity(strDummy); }
if (pRecSearch->m_mapMandatoryParam[_T("Street")])
{ mapParam[_T("Street")] = pRecSearch->GetStreet(strDummy);}
if (pRecSearch->m_mapMandatoryParam[_T("HomeNum")])
{ mapParam[_T("HomeNum")] = pRecSearch->GetHomeNum(strDummy);}
if (pRecSearch->m_mapMandatoryParam[_T("FlatNum")])
{ mapParam[_T("FlatNum")] = pRecSearch->GetFlatNum(strDummy);}
if (pRecSearch->m_mapMandatoryParam[_T("Telephone")])
{ mapParam[_T("Telephone")] = pRecSearch->GetTelephone(strDummy);}
if (pRecSearch->m_mapMandatoryParam[_T("AddInfo")])
{ mapParam[_T("AddInfo")] = pRecSearch->GetAddInfo(strDummy);}
map<CString, CString>::iterator it;
it = mapParam.begin();
if (mapParam.size() == 0)
{ return;}
else if (mapParam.size() == 1)
{strSql.Format(_T("select*from People where %s = '%s';"), it->first, it->second);}
else if (mapParam.size() > 1)
{strSql.Format(_T("select * from People where"));
CString strSeperator = pRecSearch->m_bIsMandatoryInRec ? _T("and") : _T("or"); CString strParam;
for (it = mapParam.begin(); it != mapParam.end(); it++)
{if (it == mapParam.begin())
{strParam.Format(_T(" %s = '%s' "), it->first, it->second);
strSql += strParam; }
else{strParam.Format(_T(" %s %s = '%s' "),strSeperator,it->first,it->second); strSql += strParam; }}
strSql += _T(";"); }}
if (sqlite3_prepare16_v2(pSql, strSql, -1, &pStmt, 0) == SQLITE_OK)
{ int iRow = 0;
int iRet = 0;
int iCols = sqlite3_column_count(pStmt);
CString strBuff;
TCHAR szBuff[MAX_PATH] = {0};
while (true)
{ iRet = sqlite3_step(pStmt);
if (iRet == SQLITE_ROW)
{ pRec = new CPeople();
for (int iCol = 0; iCol < iCols; iCol++)
{ TCHAR* sz = (TCHAR*) sqlite3_column_text16(pStmt, iCol);
if (sz) _tcscpy(szBuff, sz);
if (iCol == 0) pRec->SetId(szBuff);
else if (iCol == 1) pRec->SetFirstName(szBuff);
else if (iCol == 2) pRec->SetSecondName(szBuff);
else if (iCol == 3) pRec->SetMiddleName(szBuff);
else if (iCol == 4) pRec->SetCountry(szBuff);
else if (iCol == 5) pRec->SetLocality(szBuff);
else if (iCol == 6) pRec->SetCity(szBuff);
else if (iCol == 7) pRec->SetStreet(szBuff);
else if (iCol == 8) pRec->SetHomeNum(szBuff);
else if (iCol == 9) pRec->SetFlatNum(szBuff);
else if (iCol == 10) pRec->SetTelephone(szBuff);
else if (iCol == 11) pRec->SetAddInfo(szBuff);}
vecData.push_back(pRec); }
else break;
iRow++; }
iRet = sqlite3_finalize(pStmt);}}
/*static*/
void CLizaDlg::SelectNextPeopleId(CPeople* pRec, sqlite3* pSql)
{ if (!pRec) return;
size_t i = 0;
size_t nSize = MAX_PATH * 2;
sqlite3_stmt* pStmt;
TCHAR cFirst = '1';
CString strSql = _T("select max(Id) + 1 from People;");
if (sqlite3_prepare16_v2(pSql, strSql, -1, &pStmt, 0) == SQLITE_OK)
{ int iRow = 0;
int iRet = 0;
int iCol = 0;
int iCols = sqlite3_column_count(pStmt);
CString strBuff;
TCHAR szBuff[MAX_PATH] = {0};
iRet = sqlite3_step(pStmt);
if (iRet == SQLITE_ROW)
{ TCHAR* sz = (TCHAR*) sqlite3_column_text16(pStmt, iCol);
if (!sz) sz = &cFirst;
_tcscpy(szBuff, sz);
if (iCol == 0) pRec->SetId(szBuff); }
iRet = sqlite3_finalize(pStmt); }}
/*static*/
void CLizaDlg::InsertPeople(CPeople* pRec, sqlite3* pSql)
{ size_t i = 0;
size_t nSize = MAX_PATH * 2;
sqlite3_stmt* pStmt;
CString strRec, strDummy;
strRec.Format(_T("insert into People values ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');"),
pRec->GetId(strDummy),
pRec->GetFirstName(strDummy),
pRec->GetSecondName(strDummy),
pRec->GetMiddleName(strDummy),
pRec->GetCountry(strDummy),
pRec->GetLocality(strDummy),
pRec->GetCity(strDummy),
pRec->GetStreet(strDummy),
pRec->GetHomeNum(strDummy),
pRec->GetFlatNum(strDummy),
pRec->GetTelephone(strDummy),
pRec->GetAddInfo(strDummy));
if (sqlite3_prepare16_v2(pSql, strRec, -1, &pStmt, 0) == SQLITE_OK)
{ int iRet = sqlite3_step(pStmt);
iRet = sqlite3_finalize(pStmt); }}
/*static*/
void CLizaDlg::UpdatePeople(CPeople* pRec, sqlite3* pSql)
{ size_t i = 0;
size_t nSize = MAX_PATH * 2;
sqlite3_stmt* pStmt;
CString strSql, strDummy;
strSql.Format(_T("update People set FirstName = '%s', SecondName = '%s', MiddleName = '%s', Country = '%s', Locality = '%s', City = '%s', Street = '%s', HomeNum = '%s', FlatNum = '%s', Telephone = '%s', AddInfo = '%s' where Id = '%s';"),
pRec->GetFirstName(strDummy),
pRec->GetSecondName(strDummy),
pRec->GetMiddleName(strDummy),
pRec->GetCountry(strDummy),
pRec->GetLocality(strDummy),
pRec->GetCity(strDummy),
pRec->GetStreet(strDummy),
pRec->GetHomeNum(strDummy),
pRec->GetFlatNum(strDummy),
pRec->GetTelephone(strDummy),
pRec->GetAddInfo(strDummy),
pRec->GetId(strDummy));
if (sqlite3_prepare16_v2(pSql, strSql, -1, &pStmt, 0) == SQLITE_OK)
{ int iRet = sqlite3_step(pStmt);
iRet = sqlite3_finalize(pStmt); }}
/*static*/
void CLizaDlg::DeletePeople(CPeople* pRec, sqlite3* pSql)
{ size_t i = 0;
size_t nSize = MAX_PATH * 2;
sqlite3_stmt* pStmt;
CString strSql, strDummy;
strSql.Format(_T("delete from People where Id = '%s';"),pRec->GetId(strDummy));
if (sqlite3_prepare16_v2(pSql, strSql, -1, &pStmt, 0) == SQLITE_OK)
{ int iRet = sqlite3_step(pStmt);
iRet = sqlite3_finalize(pStmt); }}
void CLizaDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{ CDialogEx::OnKeyDown(nChar, nRepCnt, nFlags);}
void CLizaDlg::OnBnClickedCancel()
{ CDialogEx::OnCancel();}
void CLizaDlg::OnBnClickedSelectSqlDb()
{ UpdateData();
m_strSqlDbPath = SelectSqlDbFile(this);
UpdateData(FALSE);}
/*static*/
CString CLizaDlg::SelectSqlDbFile(CLizaDlg* pDlg)
{ CString strRet;
CString strFilters = _T("База данных (*.SQLITE)|*.sqlite||");
CFileDialog fileDlg(TRUE, _T("sqlite"), _T("*.sqlite"),
OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, strFilters, pDlg);
CString strSqlDbPath;
if (fileDlg.DoModal() == IDOK)
{ strSqlDbPath = fileDlg.GetPathName();
strSqlDbPath.MakeLower();
g_strSqlDbPath = strSqlDbPath;
strRet = strSqlDbPath;
int iPos = strSqlDbPath.ReverseFind(_T('\\'));
if (iPos != -1)
{ strSqlDbPath = strSqlDbPath.Mid(0, iPos + 1);
g_strRootPath = strSqlDbPath; } }
return strRet;}
void CLizaDlg::OnBnClickedConnect()
{ UpdateData();
DWORD dwRet = GetFileAttributes(m_strSqlDbPath);
if (m_strSqlDbPath.IsEmpty() || dwRet != FILE_ATTRIBUTE_ARCHIVE)
{ MessageBox(_T("Соединение с базой даных не установлено - выбран неверный путь.\n"), _T("Соединение с базой данных"), MB_ICONWARNING);
return; }
bool bRet = CreateConnect();
if (bRet)
{ CreateDirectory(g_strRootPath + _T("\\img\\"), NULL);
SelectPeople(m_vecData, m_pSql);
InitPeopleList(); }
else {MessageBox(_T("Соединение с базой данных не установлено!\n"), _T("Соединение с базой данных"), MB_ICONWARNING); }}
/*static */
void CLizaDlg::AddField(CListCtrl& cListCtrl, int iRow, int iCol, const TCHAR* szBuff)
{ LVITEM lv;
lv.iItem = iRow;
lv.iSubItem = iCol;
lv.pszText = (LPTSTR) szBuff;
lv.mask = LVIF_TEXT;
if (iCol == 0)
{cListCtrl.InsertItem(&lv); }
else { cListCtrl.SetItem(&lv); }}
void CLizaDlg::InitPeopleList()
{ CString strRow;
CString strBuff;
CPeople* pRec = NULL;
m_cListPeople.DeleteAllItems();
m_cListPeople.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
for (size_t i = 0; i < m_vecData.size(); i++)
{ pRec = m_vecData[i];
AddField(m_cListPeople, i, 0, pRec->GetId(strBuff));
AddField(m_cListPeople, i, 1, pRec->GetFirstName(strBuff));
AddField(m_cListPeople, i, 2, pRec->GetSecondName(strBuff));
AddField(m_cListPeople, i, 3, pRec->GetMiddleName(strBuff));
AddField(m_cListPeople, i, 4, pRec->GetCountry(strBuff));
AddField(m_cListPeople, i, 5, pRec->GetLocality(strBuff));
AddField(m_cListPeople, i, 6, pRec->GetCity(strBuff));
AddField(m_cListPeople, i, 7, pRec->GetStreet(strBuff));
AddField(m_cListPeople, i, 8, pRec->GetHomeNum(strBuff));
AddField(m_cListPeople, i, 9, pRec->GetFlatNum(strBuff));
AddField(m_cListPeople, i, 10, pRec->GetTelephone(strBuff));
AddField(m_cListPeople, i, 11, pRec->GetAddInfo(strBuff)); } }
Не нашли, что искали? Воспользуйтесь поиском по сайту:
©2015 - 2024 stydopedia.ru Все материалы защищены законодательством РФ.
|