12. oktober 2006 - 11:12
#3
Hov, det fik jeg vidst aldrig lagt ud.. men her er hvordan du gør det i C:
MySQL API C
1) Installer MySQL Serveren.
2) Gå ind i Project
3) Gå ind i Settings
4) Gå ind under fanebladet Link
5) Vælg i tappen Category "Input"
6) Gå ned i "Additinoal library path"
7) Tilføj linjen "C:\mysql\lib\debug"
8) Gå op i boxen Category igen og vælg "General"
7) Gå ned i feltet "Object/Library" og tilføj "libmySQL.lib" og "ws2_32.lib "
9) Vælg fanebladet "C++".
10) Vælg Preprocessor i selectboxen Category
11) Tilføj "C:\mysql\include" i Additional Include Directories
12) Kopier filen C:\mysql\lib\debug\libmySQL.dll til det debug bibliotek hvor din .exe fil ligger.
Koden:
#define SOCKET int
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
int main()
{
/* connection */
MYSQL *handle;
/* query result */
MYSQL_RES *result;
/* række i query result */
MYSQL_ROW row;
/* antal feltveer i query result */
int nfields;
/* pointer til array med felt længder i række i query resuult */
int *l;
/* counter */
int i;
/*
* åben connection til:
* server = "localhost"
* username = "root"
* password = ""
* database = "Test"
* port = 0 (bliver opfattet som default 3306)
*/
handle= mysql_init(NULL);
if(handle == NULL)
{
printf("MySQL error: %s", mysql_error(handle));
exit(1);
}
if(!mysql_real_connect(handle, "localhost", "root", "", "Test", 0, NULL, 0))
{
printf("MySQL error: %s", mysql_error(handle));
exit(1);
}
/* udfør query */
mysql_query(handle, "SELECT * FROM t1");
result = mysql_store_result(handle);
/* print resultat af query */
nfields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result))) {
l = (int *)mysql_fetch_lengths(result);
for (i=0; i<nfields; i++) {
printf(" %.*s", l[i], row[i] ? row[i] : "NULL");
}
printf("\n");
}
/* luk query */
mysql_free_result(result);
/* luk connection */
mysql_close(handle);
return 0;
}
//>Rune
12. oktober 2006 - 11:12
#4
Og her hvordan du gør det med MFC:
MySQL API C MFC
// MySQL API MFCDlg.cpp : implementation file
//
// Forudsætninger:
/*
1) Installer MySQL Serveren.
2) Gå ind i Project
3) Gå ind i Settings
4) Gå ind under fanebladet Link
5) Vælg i tappen Category "Input"
6) Gå ned i "Additinoal library path"
7) Tilføj linjen "C:\mysql\lib\debug"
8) Gå op i boxen Category igen og vælg "General"
7) Gå ned i feltet "Object/Library" og tilføj "libmySQL.lib" og "ws2_32.lib "
9) Vælg fanebladet "C++".
10) Vælg Preprocessor i selectboxen Category
11) Tilføj "C:\mysql\include" i Additional Include Directories
12) Kopier filen C:\mysql\lib\debug\libmySQL.dll til det debug bibliotek hvor din .exe fil ligger.
Tilføj et multiline tekstfelt og
tildel det membervariablen m_Text1
*/
#include "stdafx.h"
#define SOCKET int
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
#include "MySQL API MFC.h"
#include "MySQL API MFCDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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()
/////////////////////////////////////////////////////////////////////////////
// CMySQLAPIMFCDlg dialog
CMySQLAPIMFCDlg::CMySQLAPIMFCDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMySQLAPIMFCDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMySQLAPIMFCDlg)
m_Text1 = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMySQLAPIMFCDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMySQLAPIMFCDlg)
DDX_Text(pDX, IDC_EDIT1, m_Text1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMySQLAPIMFCDlg, CDialog)
//{{AFX_MSG_MAP(CMySQLAPIMFCDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMySQLAPIMFCDlg message handlers
BOOL CMySQLAPIMFCDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CMySQLAPIMFCDlg::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 CMySQLAPIMFCDlg::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 CMySQLAPIMFCDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMySQLAPIMFCDlg::OnOK()
{
// TODO: Add extra validation here
/* connection */
MYSQL *handle;
/* query result */
MYSQL_RES *result;
/* række i query result */
MYSQL_ROW row;
/* antal feltveer i query result */
int nfields;
/* pointer til array med felt længder i række i query resuult */
int *l;
/* counter */
int i;
char *server;
char *username;
char *password;
char *database;
int port;
/* åben connection til: */
server = "localhost";
username = "root";
password = "";
database = "Test";
port = 0; //(bliver opfattet som default 3306)
handle= mysql_init(NULL);
if(handle == NULL)
{
CString strError;
strError.Format("MySQL error: %s", mysql_error(handle));
AfxMessageBox(strError, MB_ICONEXCLAMATION);
return;
}
if(!mysql_real_connect(handle, server, username, password, database, port, NULL, 0))
{
CString strError;
strError.Format("MySQL error: %s", mysql_error(handle));
AfxMessageBox(strError, MB_ICONEXCLAMATION);
return;
}
/* udfør query */
mysql_query(handle, "SELECT * FROM tablename1");
result = mysql_store_result(handle);
/* print resultat af query */
nfields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result))) {
l = (int *)mysql_fetch_lengths(result);
for (i=0; i<nfields; i++) {
m_Text1 += _T(row[i]);
m_Text1 += "\r\n";
}
}
UpdateData(false);
/* luk query */
mysql_free_result(result);
/* luk connection */
mysql_close(handle);
}
//>Rune
20. november 2006 - 14:52
#6
Lige for at bryde ind... Hvordan kan jeg bruge MySql++ API'et via Borland C++ Builder 6 hvor jeg har brug for at sætte min information ind i en MySql-db men samtidig kunne vise det i et DBGrid (via en TTable/TSource-komponent)?
Tak for hjælpen
Jesper