转自:http://www.cnblogs.com/NoSoul/archive/2012/09/16/2687336.html
SQL:
create database ckkc; create table users ( name char(35) not null primary key, psw char(50) not null ) create table op ( id int not null primary key auto_increment, flag int not null, pname char(15) not null, num int not null ) create table st ( pname char(15) not null primary key, num int not null ) insert into users values('root','rootpass');
main.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> #include <sql.h> #include <sqlext.h> #include <Sqltypes.h> #define MAXBUFLEN 255 #define Clr() system("cls") #define Pau() system("pause") char Str_1[MAXBUFLEN]; char Name[MAXBUFLEN]; SQLHENV henv; SQLHDBC hdbc; SQLHSTMT hstmt; SQLRETURN ret; SQLCHAR ConnectIn[MAXBUFLEN]="DRIVER={MySQL ODBC 5.1 Driver};SERVER=127.0.0.1;UID=root;PWD=rootpass;DATABASE=ckkc;CharSet=gbk;"; SQLCHAR ConnectOut[MAXBUFLEN]; SQLCHAR sql[MAXBUFLEN]; SQLCHAR Rs[MAXBUFLEN]; SQLINTEGER cbRS=SQL_NTS; void Menu() { printf("\n\n\n"); printf("\t----------------------------------------\n"); printf("\t\t欢迎登入仓库库存管理系统\n"); printf("\t----------------------------------------\n"); printf("请输入用户名:"); scanf("%s",Name); printf("请输入密码:"); scanf("%s",Str_1); return; } void Add() { int num; printf("请输入入库物品名称:"); scanf("%s",Str_1); printf("请输入入库数量:"); scanf("%d",&num); ret=SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt); wsprintf((LPSTR)sql,"INSERT INTO op(flag,pname,num) VALUES('0','%s','%d')",Str_1,num); ret=SQLPrepare(hstmt,sql,SQL_NTS); ret=SQLExecute(hstmt); if(!SQL_SUCCEEDED(ret)) { printf("2.操作数据库失败!\n"); return ; } Rs[0]='\0'; ret=SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt); wsprintf((LPSTR)sql,"SELECT * FROM st WHERE pname='%s'",Str_1); ret=SQLPrepare(hstmt,sql,SQL_NTS); ret=SQLExecute(hstmt); if(SQLFetch(hstmt)!=SQL_NO_DATA_FOUND) { SQLGetData(hstmt,1,SQL_C_CHAR,Rs,MAXBUFLEN,&cbRS); } if(Rs[0]=='\0') { ret=SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt); wsprintf((LPSTR)sql,"INSERT INTO st VALUES('%s','%d')",Str_1,num); ret=SQLPrepare(hstmt,sql,SQL_NTS); ret=SQLExecute(hstmt); if(!SQL_SUCCEEDED(ret)) { printf("3.操作数据库失败!\n"); return ; } } else { ret=SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt); wsprintf((LPSTR)sql,"UPDATE st SET num=num+%d WHERE pname='%s'",num,Str_1); ret=SQLPrepare(hstmt,sql,SQL_NTS); ret=SQLExecute(hstmt); if(!SQL_SUCCEEDED(ret)) { printf("4.操作数据库失败!\n"); return ; } } return; } void Mus() { int num,now; printf("请输入出库物品名称:"); scanf("%s",Str_1); printf("请输入出库数量:"); scanf("%d",&num); Rs[0]='\0'; ret=SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt); wsprintf((LPSTR)sql,"SELECT * FROM st WHERE pname='%s'",Str_1); ret=SQLPrepare(hstmt,sql,SQL_NTS); ret=SQLExecute(hstmt); if(SQLFetch(hstmt)!=SQL_NO_DATA_FOUND) { SQLGetData(hstmt,2,SQL_C_CHAR,Rs,MAXBUFLEN,&cbRS); } if(Rs[0]=='\0') { printf("该货物不存在!\n"); return; } else { now=atoi((const char *)Rs); if(now>num) { ret=SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt); wsprintf((LPSTR)sql,"UPDATE st SET num=num-%d WHERE pname='%s'",num,Str_1); ret=SQLPrepare(hstmt,sql,SQL_NTS); ret=SQLExecute(hstmt); if(!SQL_SUCCEEDED(ret)) { printf("5.操作数据库失败!\n"); return ; } ret=SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt); wsprintf((LPSTR)sql,"INSERT INTO op(flag,pname,num) VALUES('1','%s','%d')",Str_1,num); ret=SQLPrepare(hstmt,sql,SQL_NTS); ret=SQLExecute(hstmt); if(!SQL_SUCCEEDED(ret)) { printf("6.操作数据库失败!\n"); return ; } } else if(now==num) { ret=SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt); wsprintf((LPSTR)sql,"DELETE FROM st WHERE pname='%s'",Str_1); ret=SQLPrepare(hstmt,sql,SQL_NTS); ret=SQLExecute(hstmt); if(!SQL_SUCCEEDED(ret)) { printf("7.操作数据库失败!\n"); return ; } ret=SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt); wsprintf((LPSTR)sql,"INSERT INTO op(flag,pname,num) VALUES('1','%s','%d')",Str_1,num); ret=SQLPrepare(hstmt,sql,SQL_NTS); ret=SQLExecute(hstmt); if(!SQL_SUCCEEDED(ret)) { printf("8.操作数据库失败!\n"); return ; } } else { printf("库存不足!\n"); } } return; } void Query() { SQLINTEGER num; SQLINTEGER cbnum; printf("货物名称\t数量\n"); ret=SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt); wsprintf((LPSTR)sql,"SELECT * FROM st",SQL_NTS); ret=SQLPrepare(hstmt,sql,SQL_NTS); ret=SQLExecute(hstmt); if(SQLFetch(hstmt)==SQL_NO_DATA_FOUND) { printf("记录为空!\n"); } else { do { SQLGetData(hstmt,1,SQL_C_CHAR,Rs,MAXBUFLEN,&cbRS); SQLGetData(hstmt,2,SQL_C_LONG,&num,0,&cbnum); printf("%s\t%ld\n",Rs,num); } while(SQLFetch(hstmt)!=SQL_NO_DATA_FOUND); } return; } void Show() { SQLINTEGER num,flag; SQLINTEGER cbnum,cbflag; printf("入/出库\t货物名称\t数量\n"); ret=SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt); wsprintf((LPSTR)sql,"SELECT * FROM op",SQL_NTS); ret=SQLPrepare(hstmt,sql,SQL_NTS); ret=SQLExecute(hstmt); if(SQLFetch(hstmt)==SQL_NO_DATA_FOUND) { printf("记录为空!\n"); } else { do { SQLGetData(hstmt,2,SQL_C_LONG,&flag,0,&cbflag); SQLGetData(hstmt,3,SQL_C_CHAR,Rs,MAXBUFLEN,&cbRS); SQLGetData(hstmt,4,SQL_C_LONG,&num,0,&cbnum); if(flag==0) { printf("入"); } else { printf("出"); } printf("\t%s\t%ld\n",Rs,num); } while(SQLFetch(hstmt)!=SQL_NO_DATA_FOUND); } return; } int main() { ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&henv); ret=SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(void *)SQL_OV_ODBC3,0); ret=SQLAllocHandle(SQL_HANDLE_DBC,henv,&hdbc); ret=SQLDriverConnect(hdbc,NULL,ConnectIn,SQL_NTS,ConnectOut,MAXBUFLEN,(SQLSMALLINT *)0,SQL_DRIVER_NOPROMPT); if(!SQL_SUCCEEDED(ret)) { printf("1.操作数据库失败!\n"); return -1; } while(1) { Menu(); ret=SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt); wsprintf((LPSTR)sql,"SELECT psw FROM users WHERE name ='%s'",Name); ret=SQLPrepare(hstmt,sql,SQL_NTS); ret=SQLExecute(hstmt); if(SQLFetch(hstmt)!=SQL_NO_DATA_FOUND) { SQLGetData(hstmt,1,SQL_C_CHAR,Rs,MAXBUFLEN,&cbRS); } if(strcmp((const char *)Rs,Str_1)==0) { Clr(); while(1) { printf("HI %s,欢迎您!\n",Name); printf("\t1.入库操作\n"); printf("\t2.出库操作\n"); printf("\t3.库存查询\n"); printf("\t4.交易查询\n"); printf("\t0.退出系统\n"); printf("请选择操作:"); char ch; scanf("%*c%c",&ch); switch(ch) { case '1': Add(); break; case '2': Mus(); break; case '3': Query(); break; case '4': Show(); break; case '0': return 0; default: printf("输入错误,请输入0~4\n"); } Pau(); Clr(); } } else { printf("登录失败!\n"); Pau(); Clr(); } } return 0; }