Kod w wersji konsolowej wygląda tak:
#include "sqlite3.h"
#include "libsqlitewrapped.h"
using namespace std;
int main()
{
char i;
int k=1;
Database db("tutorial.db");
if (!db.Connected())
{
printf("Database not connected - exiting\n");
exit(-1);
}
Query q(db);
char cos[1000];
string cos2="player";
sprintf(cos, "select name from %s where num=%i", cos2,k);
string name = q.get_string(cos);
long num = q.get_count("select num from player where name='Grymse'");
printf("The name of player#1 is '%s'\n", name.c_str());
printf("The number of 'Grymse' is %ld\n", num);
scanf("%i",&i);
}
Wszystko działa jak należy. Postanowiłem zatem przejść do Winapi. Na podstawie tego, co udało mi się przeczytać, skleiłem coś takiego:
#include <windows.h>
#include "sqlite3.h"
#include "libsqlitewrapped.h"
using namespace std;
int button[100];
char a[200];
HWND wnd_crt[10];
static TCHAR lpszAppName[] = TEXT( "API Windows" );
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
int xPos;
char ss[201];
case WM_CREATE:
{
wnd_crt[0]=CreateWindow("STATIC"," dowolny tekst ", WS_CHILD | WS_VISIBLE | WS_BORDER, 10, 10, 200, 20, hWnd, (HMENU)100, GetModuleHandle(NULL), NULL);
wnd_crt[1]=CreateWindow("STATIC"," dowolny tekst ", WS_CHILD | WS_VISIBLE, 10, 40, 200, 20, hWnd, (HMENU)101, GetModuleHandle(NULL), NULL);
wnd_crt[3]=CreateWindow("EDIT","", WS_CHILD | WS_VISIBLE |WS_BORDER, 10, 70, 200, 20, hWnd, (HMENU)103, GetModuleHandle(NULL), NULL);
wnd_crt[7]=CreateWindow("BUTTON","z bazy ", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 10, 100, 200, 30, hWnd, (HMENU)107, GetModuleHandle(NULL), NULL);
wnd_crt[9]=CreateWindow("BUTTON","message ", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 10, 200, 200, 30, hWnd, (HMENU)108, GetModuleHandle(NULL), NULL);
wnd_crt[8]=CreateWindow("EDIT","", WS_CHILD | WS_VISIBLE | ES_READONLY | WS_BORDER, 10, 130, 200, 20, hWnd, (HMENU)104, GetModuleHandle(NULL), NULL);
}
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case 107:
{
int k=1;
int j=0;
Database db("tutorial.db");
if (!db.Connected())
{
j=-1;
}
Query q(db);
char cos[1000];
char cos3[1000];
string cos2="player";
sprintf(cos, "select name from %s where num=%i", cos2,k);
j=400;
string name = q.get_string(cos);
sprintf(cos3,"w: %s w2: %i ",name,j);
wnd_crt[8]=CreateWindow("EDIT",cos3, WS_CHILD | WS_VISIBLE | ES_READONLY |WS_BORDER, 10, 130, 200, 20, hWnd, (HMENU)104, GetModuleHandle(NULL), NULL);
}
break;
case 108:
{
GetWindowText(wnd_crt[3],ss,200);
MessageBox(NULL, ss,"Info", MB_OK);
}
break;
}
}
break;
case WM_DESTROY: PostQuitMessage(0); break;
case WM_KEYDOWN: break;
default: return (DefWindowProc(hWnd, uMsg, wParam, lParam));
}
return(0L);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS wndclass;
HWND hWnd;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = MainWndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = NULL;
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = lpszAppName;
if(RegisterClass(&wndclass) == 0)
return FALSE;
hWnd = CreateWindow(lpszAppName, lpszAppName,
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if(hWnd == NULL)
return FALSE;
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
Jest tam trochę śmieci, ale z tym zrobi się porządek. Problemem jest "case 107:". Przerzuciłem instrukcje łączenia z bazą - nie wiem, czy to dobre miejsce - może lepiej przed "switch (uMsg)" - choć wtedy łączenie się z bazą będzie następować przy każdym zdarzeniu, ale globalnie nie działa (błędy).
string name = q.get_string(cos);
sprintf(cos3,"w: %s w2: %i ",name,j);
Powyższy fragment nie do końca działa. W oknie programu pojawia się "w: (null) w2: -858993460". Trochę to dziwne, gdyż parę linii wcześniej mamy "j=400". Wynik selecta także jest dziwny - baza w końcu istnieje, nie była modyfikowana, znajduje się we właściwym katalogu. Dla tej samej bazy, wcześniejszy program w wersji konsolowej, działa bez zarzutu.
Posiedziałem przy tym jeszcze:
string cos2="player";
sprintf(cos3,"--%s",cos2);
W programie, w polu tekstowym, po kliknięciu na przycisk (wcześniej pole jest puste), mamy "--(null)". :|