Irriteret over database system.
Jeg er ved at lave et skelet til noget database halløj jeg skal bruge, men af en eller anden sær grund, må jeg ikke få lov at læse fra databasen 2 gange .. Jeg må skrive, ændre og slette alt det jeg vil, men kun læse 1 gang:#include <iostream>
#include <string.h>
#include <sstream>
#include <windows.h>
#include <conio.h>
#include <sql.h>
#include <sqlext.h>
#include <time.h>
using namespace std;
// Variables for DATABASE SYS:
int sl;
int outconlen; // Length of returned connection string
char outconstr[1024]; // Returned connection string
char sqlstr[200]; // Insert
char *constr = "Driver={Microsoft Access Driver (*.mdb)};Dbq=database.mdb;Uid=Admin;Pwd=";
char s[5000];
SQLHENV Environment; //ODBC enviroment
SQLHDBC DataBaseConnect; //ODBC Connection
SQLHSTMT stmt; //ODBC Statement
SQLRETURN stat; //ODBC Return status
class Database
{
public:
Database()
{
stat = SQLAllocEnv(&Environment);
if((stat!=SQL_SUCCESS)&&(stat!=SQL_SUCCESS_WITH_INFO))
{
printf("Error in AllocEnv\n");
}
stat = SQLAllocConnect(Environment,&DataBaseConnect);
if((stat!=SQL_SUCCESS)&&(stat!=SQL_SUCCESS_WITH_INFO))
{
printf("Error in AllocConnect\n");
}
stat = SQLDriverConnect(DataBaseConnect,NULL,
(SQLCHAR *)constr,(SQLSMALLINT)strlen(constr),
(SQLCHAR *)outconstr, (SQLSMALLINT)sizeof(outconstr),
(SQLSMALLINT *)&outconlen,SQL_DRIVER_COMPLETE);
if((stat!=SQL_SUCCESS)&&(stat!=SQL_SUCCESS_WITH_INFO))
{
printf("Error in Connect\n");
}
stat = SQLAllocStmt(DataBaseConnect,&stmt);
if((stat!=SQL_SUCCESS)&&(stat!=SQL_SUCCESS_WITH_INFO))
{
printf("Error in AllocStmt\n");
}
}
~Database()
{
SQLFreeStmt(stmt,SQL_DROP);
SQLDisconnect(DataBaseConnect);
SQLFreeConnect(DataBaseConnect);
SQLFreeEnv(Environment);
}
void Write(char *Table, char *Row, char *Value)
{
sprintf(sqlstr, "INSERT INTO %s (%s) VALUES('%s')",Table,Row,Value);
stat = SQLExecDirect(stmt,(SQLCHAR *)sqlstr,strlen(sqlstr));
if((stat!=SQL_SUCCESS)&&(stat!=SQL_SUCCESS_WITH_INFO))
{
printf("Error in Write \n");
}
stat = SQLBindCol(stmt,1,SQL_C_CHAR,s,sizeof(s),(SQLINTEGER *)&sl);
if((stat!=SQL_SUCCESS)&&(stat!=SQL_SUCCESS_WITH_INFO))
{
printf("Error in BindCol\n");
}
for(;;)
{
stat = SQLFetch(stmt);
if((stat!=SQL_SUCCESS)&&(stat!=SQL_SUCCESS_WITH_INFO)) break;
s[sl] = '\0';
}
}
void Update(char *Table, char *Row, char *Input,int ID)
{
sprintf(sqlstr, "UPDATE %s SET %s = '%s' WHERE ID = %d",Table,Row,Input,ID);
stat = SQLExecDirect(stmt,(SQLCHAR *)sqlstr,strlen(sqlstr));
if((stat!=SQL_SUCCESS)&&(stat!=SQL_SUCCESS_WITH_INFO))
{
printf("Error in Update \n");
}
stat = SQLBindCol(stmt,1,SQL_C_CHAR,s,sizeof(s),(SQLINTEGER *)&sl);
if((stat!=SQL_SUCCESS)&&(stat!=SQL_SUCCESS_WITH_INFO))
{
printf("Error in BindCol\n");
}
for(;;)
{
stat = SQLFetch(stmt);
if((stat!=SQL_SUCCESS)&&(stat!=SQL_SUCCESS_WITH_INFO)) break;
s[sl] = '\0';
}
}
void Del(char *Table, int ID)
{
sprintf(sqlstr, "DELETE FROM %s WHERE ID = %d",Table,ID);
stat = SQLExecDirect(stmt,(SQLCHAR *)sqlstr,strlen(sqlstr));
if((stat!=SQL_SUCCESS)&&(stat!=SQL_SUCCESS_WITH_INFO))
{
printf("Error in Delete \n");
}
stat = SQLBindCol(stmt,1,SQL_C_CHAR,s,sizeof(s),(SQLINTEGER *)&sl);
if((stat!=SQL_SUCCESS)&&(stat!=SQL_SUCCESS_WITH_INFO))
{
printf("Error in BindCol\n");
}
for(;;)
{
stat = SQLFetch(stmt);
if((stat!=SQL_SUCCESS)&&(stat!=SQL_SUCCESS_WITH_INFO)) break;
s[sl] = '\0';
}
}
void ReadFromID(char *Row, char *Table, int ID)
{
sprintf(sqlstr,"SELECT %s FROM %s WHERE ID = %d",Row,Table,ID);
stat = SQLExecDirect(stmt,(SQLCHAR *)sqlstr,strlen(sqlstr));
if((stat!=SQL_SUCCESS)&&(stat!=SQL_SUCCESS_WITH_INFO))
{
printf("Error in Read \n");
}
stat = SQLBindCol(stmt,1,SQL_C_CHAR,s,sizeof(s),(SQLINTEGER *)&sl);
if((stat!=SQL_SUCCESS)&&(stat!=SQL_SUCCESS_WITH_INFO))
{
printf("Error in BindCol\n");
}
for(;;)
{
stat = SQLFetch(stmt);
if((stat!=SQL_SUCCESS)&&(stat!=SQL_SUCCESS_WITH_INFO)) break;
s[sl] = '\0';
}
}
};
int main()
{
Database DB;
//Table, Row, Value
DB.Write("Personer","Navn","DETTE ER TEST");
//Table,Row,Input,ID, Exampel: "Personer", "Navn", "Hej", 56
DB.Update("Personer", "Navn", "Hej", 40);
//Table, ID, Exampel: Personer 23
DB.Del("Personer",44);
//Row, Table, ID, Exampel: Navn, Personer, 29
DB.ReadFromID("Navn", "Personer", 25);
cout << "1: " <<s<< "\n";
//Row, Table, ID, Exampel: Navn, Personer, 29
DB.ReadFromID("Navn", "Personer", 35);
cout << "2: " <<s<< "\n";
system("pause");
return 0;
}
- Det undre mig meget, og kan ikke gennemskue hvad fejlen er ..