C语言创建数据库
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include "cvi_db.h"
#include
#include
#include "dbc.h"
static int hstmt;
static int hmap;
static int hstmt;
static int hdbc;
//数据指针
static int i = 1;
static int panelHandle;
//设置文本框和数字框的输入模式为Indicator
void setindicator (void);
//设置文本框和数字框的输入模式为hot
void sethot (void);
//清除输入框内的所有内容
void setclear (void);
//判断主键设置,主键为学号ID
int setmainkey (void);
//设置按钮的状态
void setmode (int cmdfirst, int cmdprev, int cmdnext, int cmdlast, int cmdadd, int cmddel, int cmdmodify, int cmdfind);
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "dbc.uir", PANEL)) < 0)
return -1;
//连接数据库
hdbc = DBConnect ("DSN=client");
DisplayPanel (panelHandle);
//将记录定位到第一条
first (panelHandle, PANEL_CMD_FIRST, EVENT_COMMIT, NULL, 0, 0);
//设置输入框的模式为Indicator
setindicator ();
RunUserInterface ();
DiscardPanel (panelHandle);
//断开数据库连接,释放系统资源
DBDisconnect (hdbc);
return 0;
}
int CVICALLBACK panelCB (int panel, int event, void *callbackData,
int eventData1, int eventData2)
{
switch (event)
{
case EVENT_CLOSE:
QuitUserInterface (0);
break;
}
return 0;
}
//第一条记录
int CVICALLBACK first (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
long agestatus;
int agevalue;
long sexstatus;
char sexvalue[10];
long namestatus;
char namevalue[20];
long idstatus;
int idvalue;
switch (event)
{
case EVENT_COMMIT:
hmap = DBBeginMap (hdbc);
DBMapColumnToInt (hmap, "ID", &idvalue, &idstatus);
DBMapColumnToChar (hmap, "NAME",20, namevalue, &namestatus, "");
DBMapColumnToChar (hmap, "SEX", 10, sexvalue, &sexstatus, "");
DBMapColumnToInt (hmap, "AGE", &agevalue, &agestatus);
hstmt = DBActivateMap (hmap, "table2");
while (DBFetchNext (hstmt) != DB_EOF)
{
if (i)
break;
}
SetCtrlVal (panelHandle, PANEL_NUMERIC_ID, idvalue);
SetCtrlVal (panelHandle, PANEL_STRING_NAME, namevalue);
SetCtrlVal (panelHandle, PANEL_STRING_SEX, sexvalue);
SetCtrlVal (panelHandle, PANEL_NUMERIC_AGE, agevalue);
DBDeactivateMap (hmap);
//设置数据指针为1
i = 1;
break;
}
return 0;
}
//上一条记录
int CVICALLBACK prev (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
long agestatus;
int agevalue;
long sexstatus;
char sexvalue[3];
long namestatus;
char namevalue[9];
long idstatus;
int idvalue;
int j = 0;
switch (event)
{
case EVENT_COMMIT:
hmap = DBBeginMap (hdbc);
DBMapColumnToInt (hmap, "ID", &idvalue, &idstatus);
DBMapColumnToChar (hmap, "NAME", 9, namevalue, &names
tatus, "");
DBMapColumnToChar (hmap, "SEX", 3, sexvalue, &sexstatus, "");
DBMapColumnToInt (hmap, "AGE", &agevalue, &agestatus);
hstmt = DBActivateMap (hmap, "table2");
i --;
if (i <= 0)
{
i = 1;
}
while (DBFetchNext (hstmt) != DB_EOF)
{
j ++;
if ((i - j) == 0)
break;
}
SetCtrlVal (panelHandle, PANEL_NUMERIC_ID, idvalue);
SetCtrlVal (panelHandle, PANEL_STRING_NAME, namevalue);
SetCtrlVal (panelHandle, PANEL_STRING_SEX, sexvalue);
SetCtrlVal (panelHandle, PANEL_NUMERIC_AGE, agevalue);
DBDeactivateMap (hmap);
break;
}
return 0;
}
//下一条记录
int CVICALLBACK next (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int numberofrecords;
int hstmt2;
long agestatus;
int agevalue;
long sexstatus;
char sexvalue[3];
long namestatus;
char namevalue[9];
long idstatus;
int idvalue;
int j = 0;
switch (event)
{
case EVENT_COMMIT:
hmap = DBBeginMap (hdbc);
DBMapColumnToInt (hmap, "ID", &idvalue, &idstatus);
DBMapColumnToChar (hmap, "NAME", 9, namevalue, &namestatus, "");
DBMapColumnToChar (hmap, "SEX", 3, sexvalue, &sexstatus, "");
DBMapColumnToInt (hmap, "AGE", &agevalue, &agestatus);
hstmt2 = DBActivateSQL (hdbc, "SELECT * FROM table2");
numberofrecords = DBNumberOfRecords (hstmt2);
DBDeactivateSQL (hstmt2);
hstmt = DBActivateMap (hmap, "table2");
i ++;
while (DBFetchNext (hstmt) != DB_EOF)
{
j ++;
if ((i - j) == 0)
break;
}
if (i > numberofrecords)
{
i = numberofrecords;
}
SetCtrlVal (panelHandle, PANEL_NUMERIC_ID, idvalue);
SetCtrlVal (panelHandle, PANEL_STRING_NAME, namevalue);
SetCtrlVal (panelHandle, PANEL_STRING_SEX, sexvalue);
SetCtrlVal (panelHandle, PANEL_NUMERIC_AGE, agevalue);
DBDeactivateMap (hmap);
break;
}
return 0;
}
//最后一条记录
int CVICALLBACK last (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int numberofrecords;
int hstmt2;
long agestatus;
int agevalue;
long sexstatus;
char sexvalue[3];
long namestatus;
char namevalue[9];
long idstatus;
int idvalue;
int j = 0;
switch (event)
{
case EVENT_COMMIT:
hstmt2 = DBActivateSQL (hdbc, "SELECT * FROM table2");
numberofrecords = DBNumberOfRecords (hstmt2);
DBDeactivateSQL (hstmt2);
hmap = DBBeginMap (hdbc);
DBMapColumnToInt (hmap, "ID", &idvalue, &idstatus);
DBMapColumnToChar (hmap, "NAME", 9, namevalue, &namestatus, "");
DBMapColumnToChar (hmap, "SEX", 3, sexvalue, &sexstatus, "");
DBMapColumnToInt (hmap, "AGE", &agevalue, &agestatus);
hstmt = DBActivateMap (hmap, "table2");
while (DBFetchNext (hstmt) != DB_EOF)
{
}
SetCtrlVal (panelHandle, PANEL_NUMERIC_ID, idvalue);
SetCtrlVal (panelHandle, PANEL_STRING_NAME, namevalue);
SetCtrlVal (panelHandle, PANEL_STRING_SEX, sexvalue);
SetCtrlVal (panelHandle, PANEL_NUMERIC_AGE, agevalue);
DBDeactivateMap (hmap);
//定位数据指针在最后一条数据
i = numberofrecords;
break;
}
return 0;
}
//添加按钮
int CVICALLBACK add (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int comparisonresult;
char labeltext[10];
int agevalue;
char *sexvalue;
char *namevalue;
int idvalue;
int hstmt1;
static int k = 1;
switch (event)
{
case EVENT_COMMIT:
if (k)
{
SetCtrlAttribute (panelHandle, PANEL_CMD_ADD, ATTR_LABEL_TEXT, "保存");
k = 0;
sethot ();
setmode (1, 1, 1, 1, 0, 1, 1, 1);
setclear ();
}
else
{
SetCtrlAttribute (panelHandle, PANEL_CMD_ADD, ATTR_LABEL_TEXT, "添加");
k = 1;
setindicator ();
setmode (0, 0, 0, 0, 0, 0, 0, 0);
}
GetCtrlAttribute (panelHandle, PANEL_CMD_ADD, ATTR_LABEL_TEXT, labeltext);
comparisonresult = strcmp (labeltext, "添加");
if (comparisonresult == 0)
{
if (setmainkey() == 0)
{
namevalue = malloc (9);
sexvalue = malloc (3);
GetCtrlVal (panelHandle, PANEL_NUMERIC_ID, &idvalue);
GetCtrlVal (panelHandle, PANEL_STRING_NAME, namevalue);
GetCtrlVal (panelHandle, PANEL_STRING_SEX, sexvalue);
GetCtrlVal (panelHandle, PANEL_NUMERIC_AGE, &agevalue);
hstmt1 = DBPrepareSQL (hdbc, "INSERT INTO table2 VALUES(?, ?, ?, ?)");
DBCreateParamInt (hstmt1, "ID", DB_PARAM_INPUT, idvalue);
DBCreateParamChar (hstmt1, "NAME", DB_PARAM_INPUT, namevalue, 9);
DBCreateParamChar (hstmt1, "SEX", DB_PARAM_INPUT, sexvalue, 3);
DBCreateParamInt (hstmt1, "AGE", DB_PARAM_INPUT, agevalue);
DBExecutePreparedSQL (hstmt1);
DBClosePreparedSQL (hstmt1);
free(namevalue);
free(sexvalue);
}
}
break;
}
return 0;
}
//删除按钮
int CVICALLBACK del (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int responsevalue;
int hstmt1;
int idvalue;
switch (event)
{
case EVENT_COMMIT:
responsevalue = ConfirmPopup ("确认删除", "真的要删除该记录吗?");
if (responsevalue)
{
GetCtrlVal (panelHandle, PANEL_NUMERIC_ID, &idvalue);
hstmt1 = DBPrepareSQL (hdbc, "DELETE * FROM table2 where ID=?");
DBCreateParamInt (hstmt1, "ID", DB_PARAM_INPUT, idvalue);
DBExecutePreparedSQL (hstmt1);
DBClosePreparedSQL (hstmt1);
next (panelHandle, PANEL_CMD_NEXT, EVENT_COMMIT, NULL, 0, 0);
}
break;
}
return 0;
}
//修改按钮
int CVICALLBACK modify (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int comparisonresult;
char labeltext[10];
long agestatus;
int agevalue;
long sexstatus;
char sexvalue[3];
long namestatus;
char namevalue[9];
long idstatus;
int idvalue;
static int idvalue1 = 0;
static int k = 1;
switch (event)
{
case EVENT_COMMIT:
if (k)
{
SetCtrlAttribute (panelHandle, PANEL_CMD_MODIFY, ATTR_LABEL_TEXT, "保存");
k = 0;
sethot ();
setmode (1, 1, 1, 1, 1, 1, 0, 1);
GetCtrlVal (panelHandle, PANEL_NUMERIC_ID, &idvalue1);
}
else
{
SetCtrlAttribute (panelHandle, PANEL_CMD_MODIFY, ATTR_LABEL_TEXT, "修改");
k = 1;
setindicator ();
setmode (0, 0, 0, 0, 0, 0, 0, 0);
}
GetCtrlAttribute (panelHandle, PANEL_CMD_MODIFY, ATTR_LABEL_TEXT, labeltext);
comparisonresult = strcmp (labeltext, "修改");
if (comparisonresult == 0)
{
if (setmainkey () == 0)
{
hmap = DBBeginMap (hdbc);
DBMapColumnToInt (hmap, "ID", &idvalue, &idstatus);
DBMapColumnToChar (hmap, "NAME", 9, namevalue, &namestatus, "");
DBMapColumnToChar (hmap, "SEX", 3, sexvalue, &sexstatus, "");
DBMapColumnToInt (hmap, "AGE", &agevalue, &agestatus);
hstmt = DBActivateMap (hmap, "table2");
while (DBFetchNext (hstmt) != DB_EOF)
{
if (idvalue1 == idvalue)
break;
}
GetCtrlVal (panelHandle, PANEL_NUMERIC_ID, &idvalue);
GetCtrlVal (panelHandle, PANEL_STRING_NAME, namevalue);
GetCtrlVal (panelHandle, PANEL_STRING_SEX, sexvalue);
GetCtrlVal (panelHandle, PANEL_NUMERIC_AGE, &agevalue);
DBPutRecord (hstmt);
DBDeactivateMap (hmap);
}
}
break;
}
return 0;
}
//查找按钮
int CVICALLBACK find (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
char responsebuffer[9];
long agestatus;
int agevalue;
long sexstatus;
char sexvalue[3];
long namestatus;
char namevalue[9];
long idstatus;
int idvalue;
int findflag = 0;
switch (event)
{
case EVENT_COMMIT:
PromptPopup ("查找", "请输入您所要查找的同学姓名", responsebuffer, 8);
hmap = DBBeginMap (hdbc);
DBMapColumnToInt (hmap, "ID", &idvalue, &idstatus);
DBMapColumnToChar (hmap, "NAME", 9, namevalue, &namestatus, "");
DBMapColumnToChar (hmap, "SEX", 3, sexvalue, &sexstatus, "");
DBMapColumnToInt (hmap, "AGE", &agevalue, &agestatus);
hstmt = DBActivateMap (hmap, "table2");
while (DBFetchNext (hstmt) != DB_EOF)
{
if ((strcmp (responsebuffer, namevalue)) == 0)
{
findflag = 1;
break;
}
}
if (findflag)
{
SetCtrlVal (panelHandle, PANEL_NUMERIC_ID, idvalue);
SetCtrlVal (panelHandle, PANEL_STRING_NAME, namevalue);
SetCtrlVal (panelHandle, PANEL_STRING_SEX, sexvalue);
SetCtrlVal (panelHandle, PANEL_NUMERIC_AGE, agevalue);
}
else
{
MessagePopup ("查找", "您所要求的数据没有查到!");
}
DBDeactivateMap (hmap);
break;
}
return 0;
}
void setindicator (void)
{
SetCtrlAttribute (panelHandle, PANEL_NUMERIC_ID, ATTR_CTRL_MODE, VAL_INDICATOR);
SetCtrlAttribute (panelHandle, PANEL_STRING_NAME, ATTR_CTRL_MODE, VAL_INDICATOR);
SetCtrlAttribute (panel
Handle, PANEL_STRING_SEX, ATTR_CTRL_MODE, VAL_INDICATOR);
SetCtrlAttribute (panelHandle, PANEL_NUMERIC_AGE, ATTR_CTRL_MODE, VAL_INDICATOR);
}
void sethot (void)
{
SetCtrlAttribute (panelHandle, PANEL_NUMERIC_ID, ATTR_CTRL_MODE, VAL_HOT);
SetCtrlAttribute (panelHandle, PANEL_STRING_NAME, ATTR_CTRL_MODE, VAL_HOT);
SetCtrlAttribute (panelHandle, PANEL_STRING_SEX, ATTR_CTRL_MODE, VAL_HOT);
SetCtrlAttribute (panelHandle, PANEL_NUMERIC_AGE, ATTR_CTRL_MODE, VAL_HOT);
}
void setmode (int cmdfirst, int cmdprev, int cmdnext, int cmdlast, int cmdadd, int cmddel, int cmdmodify, int cmdfind)
{
SetCtrlAttribute (panelHandle, PANEL_CMD_FIRST, ATTR_DIMMED, cmdfirst);
SetCtrlAttribute (panelHandle, PANEL_CMD_PREV, ATTR_DIMMED, cmdprev);
SetCtrlAttribute (panelHandle, PANEL_CMD_NEXT, ATTR_DIMMED, cmdnext);
SetCtrlAttribute (panelHandle, PANEL_CMD_LAST, ATTR_DIMMED, cmdlast);
SetCtrlAttribute (panelHandle, PANEL_CMD_ADD, ATTR_DIMMED, cmdadd);
SetCtrlAttribute (panelHandle, PANEL_CMD_DEL, ATTR_DIMMED, cmddel);
SetCtrlAttribute (panelHandle, PANEL_CMD_MODIFY, ATTR_DIMMED, cmdmodify);
SetCtrlAttribute (panelHandle, PANEL_CMD_FIND, ATTR_DIMMED, cmdfind);
}
void setclear (void)
{
SetCtrlVal (panelHandle, PANEL_NUMERIC_ID, 0);
SetCtrlVal (panelHandle, PANEL_STRING_NAME, "");
SetCtrlVal (panelHandle, PANEL_STRING_SEX, "");
SetCtrlVal (panelHandle, PANEL_NUMERIC_AGE, 0);
}
int setmainkey (void)
{
long idstatus;
int idvalue;
int idvalue1;
int mainkeyflag = 0;
GetCtrlVal (panelHandle, PANEL_NUMERIC_ID, &idvalue1);
hmap = DBBeginMap (hdbc);
DBMapColumnToInt (hmap, "ID", &idvalue, &idstatus);
hstmt = DBActivateMap (hmap, "table2");
while ((DBFetchNext (hstmt)) != DB_EOF)
{
if (idvalue1 == idvalue)
{
mainkeyflag ++;
}
}
if (mainkeyflag > 1)
{
MessagePopup ("提示", "主键不能重复!请修改ID号,使其唯一!");
next (panelHandle, PANEL_CMD_NEXT, EVENT_COMMIT, NULL, 0, 0);
return -1;
}
else
{
return 0;
}
}