?? sqlite3工作平臺dlg.cpp
字號:
// Sqlite3工作平臺Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "Sqlite3工作平臺.h"
#include "Sqlite3工作平臺Dlg.h"
#include <io.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "sqlite3.h"
//FILE * fp;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
sqlite3 *M_db=NULL;
CString m_filepath="";
CString m_tablename="";
int ConditionIndex=-1;
char * TableName[32]={0};
int columnNum=0;
char Fieldsql[256]="";
char valuesql[32]="";
char setsql[256]="";
int colwidth[10]={50,60,70,60,70,60,90,120,10};
int showMaxRow=0;
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSqlite3Dlg dialog
CSqlite3Dlg::CSqlite3Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CSqlite3Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSqlite3Dlg)
m_Message = _T("");
m_MaxRow = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
CSqlite3Dlg::~CSqlite3Dlg()
{
if(M_db!=NULL)
{
sqlite3_exec(M_db,"COMMIT",0,0,0);
}
sqlite3_close(M_db);
}
void CSqlite3Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSqlite3Dlg)
DDX_Control(pDX, IDC_LIST_INFO, m_cListInfo);
DDX_Control(pDX, IDC_CHECK, m_Check);
DDX_Control(pDX, IDC_COMBOCondition, m_CheckCondition);
DDX_Control(pDX, IDC_TableList, m_TableList);
DDX_Text(pDX, IDC_MESSAGE, m_Message);
DDX_Text(pDX, IDC_MAXROW, m_MaxRow);
DDV_MaxChars(pDX, m_MaxRow, 5);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSqlite3Dlg, CDialog)
//{{AFX_MSG_MAP(CSqlite3Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTONOPENDATA, OnButtonopendata)
ON_CBN_SELCHANGE(IDC_COMBOCondition, OnSelchangeCOMBOCondition)
ON_BN_CLICKED(IDC_BUTTONClOSEDATA, OnBUTTONClOSEDATA)
ON_LBN_SELCHANGE(IDC_TableList, OnSelchangeTableList)
ON_BN_CLICKED(IDC_EXECUTE, OnExecute)
ON_EN_KILLFOCUS(IDC_CHECK, OnKillfocusCheck)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSqlite3Dlg message handlers
BOOL CSqlite3Dlg::OnInitDialog()
{
CDialog::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)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
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
m_MaxRow="100";
/* TCITEM tcItem;
tcItem.mask = TCIF_TEXT ;
tcItem.pszText = _T("Data");
m_TabSQLResult.InsertItem(0,&tcItem);
tcItem.pszText = _T("Schema");
m_TabSQLResult.InsertItem(1,&tcItem);
m_cListInfo.InsertColumn(0, "name");
m_cListInfo.SetColumnWidth(0, 80);
m_cListInfo.InsertColumn(1, "ae");
m_cListInfo.SetColumnWidth(1, 80);
m_cListInfo.InsertColumn(2, "me");
m_cListInfo.SetColumnWidth(2, 80);
for(int i = 0; i < 5; ++ i)
{
CString strText;
strText.Format("lie is %d", i);
m_cListInfo.InsertItem(
LVIF_TEXT|LVIF_STATE, i, strText,
(i%2)==0 ? LVIS_SELECTED : 0, LVIS_SELECTED,
0, 0);
// Initialize the text of the subitems.
for (int j=1;j < 3;j++)
{
strText.Format(TEXT("sub-item %d %d"), i, j);
m_cListInfo.SetItemText(i, j, strText);
}
}*/
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CSqlite3Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::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 CSqlite3Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (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
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSqlite3Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSqlite3Dlg::OnButtonopendata()
{
// TODO: Add your control notification handler code here
//CButton * CopenButton=(CButton *)GetDlgItem(IDC_BUTTONOPENDATA);
sqlite3 *db=NULL;
char filepath[512]={0};
char gettablenamesql[1024]="";
int rc=0;
char * errmsg=NULL;
char errmsg1[256]={0};
sqlite3_stmt *vm;
UpdateData(TRUE);
m_TableList.ResetContent();
char szFilter[] = "db Files (*.db)|*.db|*.db|Data Files (*.db;*.db)|*.db; *.db|All Files (*.*)|*.*||";
CFileDialog dlg( TRUE,"db","*.db",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szFilter,this);
if( dlg.DoModal() == IDOK)
{
//filepath.Format(dlg.GetPathName());
sprintf(filepath,"%s",dlg.GetPathName());
if (access(filepath,0) != 0 || strstr(filepath,".db") == NULL)
{
MessageBox("Index不存在!",NULL,MB_OK);
return;
}
}
else
{
return;
}
if(db!=NULL)
sqlite3_close(M_db);
m_filepath.Format("%s",filepath);
int rs=sqlite3_open(m_filepath,&db);
M_db=db;
sqlite3_exec(M_db,"BEGIN",0,0,0);
sprintf(gettablenamesql,"SELECT name FROM sqlite_master Where type=='table' ");
rc = sqlite3_prepare(M_db, gettablenamesql, sizeof(gettablenamesql), &vm, 0);
if(rc != SQLITE_OK)
{
sprintf(errmsg1,sqlite3_errmsg(M_db));
MessageBox(errmsg1,NULL,MB_OK);
return;
}
while ((rc=sqlite3_step(vm))==SQLITE_ROW)
{
m_TableList.AddString((CString)sqlite3_column_text(vm,0));
}
rc = sqlite3_finalize(vm);
}
void CSqlite3Dlg::OnSelchangeCOMBOCondition()
{
// TODO: Add your control notification handler code here
CButton * CExecuteSql=(CButton *)GetDlgItem(IDC_EXECUTE);
UpdateData(FALSE);
char sql[1024]="";
CComboBox * checkcondition=(CComboBox *) GetDlgItem(IDC_COMBOCondition);
CEdit * checksql=(CEdit *) GetDlgItem(IDC_CHECK);
ConditionIndex=checkcondition->GetCurSel();
switch(ConditionIndex)
{
case 0:
if(m_tablename=="")
{
sprintf(sql,"SELECT * FROM tableName WHERE ");
}
else
{
sprintf(sql,"SELECT * FROM %s WHERE ",m_tablename);
}
//m_Check="";
// m_Check=sql;
checksql->SetWindowText(sql);
break;
case 1:
if(m_tablename=="")
{
sprintf(sql,"DELETE FROM tableName WHERE ");
}
else
{
sprintf(sql,"DELETE FROM %s WHERE ",m_tablename);
}
// m_Check="";
// m_Check=sql;
checksql->SetWindowText(sql);
break;
case 2:
if(m_tablename=="")
{
sprintf(sql,"INSERT INTO tableName ([Field1,...N]) VALUES ([Value1,...N])");
}
else
{
sprintf(sql,"INSERT INTO %s ( %s ) VALUES ( %s )",m_tablename,Fieldsql,valuesql);
}
//m_Check="";
// m_Check=sql;
checksql->SetWindowText(sql);
break;
case 3:
if(m_tablename=="")
{
sprintf(sql,"UPDATE tableName SET WHERE ");
}
else
{
sprintf(sql,"UPDATE %s SET %s WHERE ",m_tablename,setsql);
}
//m_Check="";
// m_Check=sql;
checksql->SetWindowText(sql);
break;
case 4:
if(m_tablename=="")
{
sprintf(sql,"DROP TABLE tableName ");
}
else
{
sprintf(sql,"DROP TABLE %s ",m_tablename);
}
//m_Check="";
// m_Check=sql;
checksql->SetWindowText(sql);
break;
case 5:
sprintf(sql,"CREATE TABLE tableName ()");
checksql->SetWindowText(sql);
break;
case 6:
sprintf(sql,"Vacuum");
checksql->SetWindowText(sql);
break;
default:
break;
}
if(M_db!=NULL)
CExecuteSql->EnableWindow(TRUE);
}
void CSqlite3Dlg::OnBUTTONClOSEDATA()
{
// TODO: Add your control notification handler code here
//CButton * CExecuteSql=(CButton *)GetDlgItem(IDC_EXECUTE);
m_TableList.ResetContent();
if(M_db!=NULL)
{
sqlite3_exec(M_db,"COMMIT",0,0,0);
}
sqlite3_close(M_db);
M_db=NULL;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -