📄 mysql.c
字号:
static int panel4;
#include <ansi_c.h>
#define FALSE 0
#define TRUE 1
#include "cvi_db.h"
#include <formatio.h>
#include <utility.h>
static int panel3;
static int readHandle;
static int readhandle;
static int handle;
static char readFirstName[20];
static char readSecondName[20];
static double readHeight;
static double height;
static char lastString[15];
static char firstString[15];
static int response;
static int databaseExistsFlag;
static int hStmt;
static int panel2;
static long heightStatus;
static double height;
static char dsnName[25];
static char path[280];
char *connectString;
static int startedTyping = 0;
static int statementHandle;
static int resCode;
static long firstNameStat;
char firstName[20];
static long lastNameStat;
char lastName[20];
char record[200];
static int mapHandle;
static int dbHandle;
#include <cvirte.h> /* Needed if linking in external compiler; harmless otherwise */
#include <userint.h>
#include "mySQL.h"
static int panelHandle;
void ShowError(void)
{
MessagePopup("Database Error",DBErrorMessage());
}
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0) /* Needed if linking in external compiler; harmless otherwise */
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "mySQL.uir", PANEL)) < 0)
return -1;
DisplayPanel (panelHandle);
panel4 = LoadPanel (0, "mySQL.uir", PANEL_4);
DisplayPanel (panel4);
RunUserInterface ();
return 0;
}
int CVICALLBACK quit (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
QuitUserInterface (0);
break;
}
return 0;
}
int CVICALLBACK connect (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
dbHandle = DBConnect ("DSN=mySQL");
if (dbHandle > 0)
MessagePopup ( "Database Example", " Connection to Access Database successful ");
hStmt = DBActivateSQL (dbHandle, "Select * from mynewTestTab");
if (hStmt > 0 )
{
response = ConfirmPopup ("Database Example",
"Table mynewTestTab already exists. \n Would you like to delete it ?");
DBDeactivateSQL (hStmt);
if (response == 1)
{
resCode = DBImmediateSQL (dbHandle, "DROP TABLE mynewTestTab");
databaseExistsFlag = FALSE;
}
else
{
MessagePopup ("Database Example", "Table not Dropped");
databaseExistsFlag = TRUE;
return 0;
}
}
else
{
databaseExistsFlag = FALSE ;
}
panel2 = LoadPanel (0, "mySQL.uir", PANEL_2);
DisplayPanel (panel2);
DiscardPanel (panelHandle);
break;
}
return 0;
}
int CVICALLBACK commit (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
mapHandle = DBBeginMap (dbHandle);
if (mapHandle < 0 ) MessagePopup ( " Database Example", " Error Mapping Database Fields ");
resCode = DBMapColumnToChar (mapHandle, "FirstName", 15, firstName,&firstNameStat, "");
if (resCode != DB_SUCCESS) MessagePopup ( " Database Example", " Error Mapping First Name");
resCode = DBMapColumnToChar (mapHandle, "LastName", 15, lastName,&lastNameStat, "");
if (resCode != DB_SUCCESS) MessagePopup ( " Database Example", " Error Mapping Last Name");
resCode = DBMapColumnToDouble (mapHandle, "Height", &height, &heightStatus);
if (resCode != DB_SUCCESS) MessagePopup ( " Database Example", " Error Mapping Height");
if (databaseExistsFlag == FALSE ) // Check to see whether the database exists
{
resCode = DBCreateTableFromMap (mapHandle, "myNewTestTab");
if (resCode < 0 )
{
MessagePopup ( " Database Example", " Error Creating Database ");
}
else
{
databaseExistsFlag = TRUE; // if no errors then set flag to TRUE
}
}
else
{
databaseExistsFlag = TRUE; // If database exists then set Flag to TRUE
}
statementHandle = DBActivateMap (mapHandle, "mynewTestTab");
if (statementHandle < 0 ) MessagePopup ( " Database Example", " Error Activating Database ");
GetCtrlVal (panel2, PANEL_2_STRING, firstName);
GetCtrlVal (panel2, PANEL_2_STRING_2, lastName);
GetCtrlVal (panel2, PANEL_2_NUMERIC, &height);
resCode = DBCreateRecord (statementHandle);
if (resCode != DB_SUCCESS) MessagePopup ( " Database Example", " Error Creating Record");
resCode = DBPutRecord (statementHandle);
if (resCode != DB_SUCCESS) MessagePopup ( " Database Example", " Error Putting Record");
MessagePopup ("Database Example", "Record has been committed");
SetCtrlVal (panel2, PANEL_2_STRING, "");
SetCtrlVal (panel2, PANEL_2_STRING_2, "");
SetCtrlVal (panel2, PANEL_2_NUMERIC, 0.00);
SetActiveCtrl (panel2, PANEL_2_STRING);
break;
}
return 0;
}
int CVICALLBACK done (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
DBDisconnect (dbHandle);
QuitUserInterface (0);
break;
}
return 0;
}
int CVICALLBACK readRecords (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
Cls();
readHandle=DBActivateSQL (dbHandle, "SELECT * FROM mynewTestTab ");
DBBindColChar (readHandle, 1, 15, readFirstName, &firstNameStat, "");
DBBindColChar (readHandle, 2, 15, readSecondName, &lastNameStat, "");
DBBindColDouble (readHandle, 3, &readHeight, &heightStatus);
panel3 = LoadPanel (0, "mySQL.uir", PANEL_3);
DisplayPanel (panel3);
while (DBFetchNext (readHandle)!=DB_EOF)
{
Scan (readFirstName,"%s>%s[w15y]",readFirstName);
Scan (readSecondName,"%s>%s[w15y]",readSecondName);
Scan (&readHeight,"%f>%f[w15]",&readHeight);
Fmt(record,"%s<%s%s%f[w15]",readFirstName,readSecondName,readHeight);
InsertTextBoxLine (panel3, PANEL_3_TEXTBOX, -1, record);
}
DBDisconnect (dbHandle);
break;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -