Program that checks SQL Create Tables and Insert.

This program should read the SQL Statement Create and Insert Statements. Once statements are validated what the user keys in from the insert should be written in a newly created file (.txt). I would like some assistance cause this is a pretty tough assignment. Does anyone know how to do this assignment ?

1) OPEN file.

Syntax: Open "mydatabase.db"

Your program should be able to detect whether the file exists or not. If not, prompt "This is a new file". Otherwise, prompt "Database is opened" and display the structure of the database.

2) CREATE to create a table.

For example,

CREATE TABLE faculty( firstname 25,lastname 15,id 10 );

will create a table with three fields. All the fields for this assignments are character fields. These table name, field information should be stored at the beginning of the file. It should be stored in a way that you can easily read the information when you open the file. The table will be automatically saved.

3) INSERT

Another command you need to implement in this assignment is INSERT, which add data to the table. For example,

INSERT INTO faculty (firstname, lastname, id) VALUES ('John', 'Smith', '393948394');

All the data are automatically saved to the database file.


Here's the base code:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <cassert>
using namespace std;



int checksemicolon(string query, int& error)
{
int qlength;
qlength = query.length();

if(query[qlength-1] != ';')
{
cout << "Error: missing \";\" at the end of the query." << endl;
return error++;
}
else
return error;
}

int checkcreate(string query, int& error, int position1)
{
string word1;
word1 = query.substr(0, position1);

if(word1 != "CREATE")
{
cout << "Error: missing correct CREATE keyword at the beginning of the query." << endl;
return error++;
}
else
return error;
}

int checkfield1(string query, int& error, int position1)
{
int position2;
position2 = query.find(" ", position1+1);
string word2;
word2 = query.substr(position1+1, position2-(position1+1));

if(word2 == "FROM")
{
cout << "Error: missing fieldname in the query after SELECT keyword." << endl;
return error++;
}
else
return error;
}

int checkfrom(string query, int& error, int positionF)
{
if(positionF == -1)
{
cout << "Error: missing correct FROM keyword in the query. " << endl;
return error++;
}
else
return error;
}

int checktable(string query, int& error, int positionF)
{
int i;
int count = 0;

for(i=positionF; i<query.length(); i++)
{
if(query[i] == ' ')
count++;
}

if(count > 1)
{
cout << "Error: too many table names between FROM keyword and the end of the query. Only one table name authorized." << endl;
return error++;
}
else
return error;
}

int checkcomafields(string query, int& error, int position1, int positionF)
{
string fields;
fields = query.substr(position1, (positionF-position1));

int i;
for(i=position1+1; i<positionF-1; i++)
{
if(query[i] == ' ')
{
if(query[i-1] != ',')
{
cout << "Error: missing coma between field names." << endl;
error++;
}
}
}

return error;
}

int _tmain(int argc, _TCHAR* argv[])
{
string query;
cout << "Enter your query: " << endl << endl;
getline(cin , query);

int position1;
position1 = query.find(" ");

int positionF;
positionF = query.find("FROM");

int error = 0;

checksemicolon(query, error);
checkcreate(query, error, position1);
checkfrom(query, error, positionF);
checkfield1(query, error, position1);
checktable(query, error, positionF);
checkcomafields(query, error, position1, positionF);

if (error > 1)
{
cout << endl << endl;
cout << "!!! Your query do not follow the rules of syntax. Please check the previous errors message and correct the query. " << endl;
cout << endl << endl;
}
else
{
cout << endl << endl;
cout << "Your query is correct and will be executed." << endl;
cout << endl << endl;
}

system("pause");
return 0;
}
Last edited on
Topic archived. No new replies allowed.