Solved

Making my own database??

Posted on 1998-06-08
4
161 Views
Last Modified: 2013-11-19
Hi!

I've been here a week or two ago trying to understand how to save text from edit box entries.  I found this to be lacking in security, to be honest, so I decided to create by Diary app as a Database.  So, first I'll briefly explain what I'm wanting to do, then I'll ask the question.

I'm working on a Diary application in VC++ 5 Pro. I need to create a database that will take entries from the program's edit box (as entry field) and save it to a database. Now, I have no idea if I can use the tables and MDB's already on my hard drive within the program, but I assume not.  I'm betting I have to create my own tables and database to include in this app.  So, since I do not have Access or any other Database creation software, I need to know, in detail, how to create the database I need - then I have to include it using AppWizard.

So, please, in detail, help me from start to finish with creating the database that will do the job I need to have a viable, working Daily Diary application. I also have VBasic on my system, as I hear it may be easier to create MDB's for DevStudio usage thru it. Can anyone please assist me?

I know this will be long and hard (I think), so I'm handing over all my points for this one.  Thanks!

Mike Wonch
0
Comment
Question by:MWonch
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
piano_boxer earned 130 total points
ID: 1316263
Here is how you can create and 'format' a Access
database at runtime.

--------------

// Insert this code in some .CPP file

typedef struct
{
    LPCTSTR     lpszName;
    short       nType;
    long        lSize;
    long        lAttributes;
    BOOL        bRequired;
    BOOL        bAllowZeroLength;
} MY_FIELD_DEF;


// Static information about the fields that you want to create in
// the database table 'TestTable'

MY_FIELD_DEF my_fields[] = {
// FIELD 1
    "id",
    dbLong,
    0, // Dont matter here because dbLong is allways 4 bytes.
    dbUpdatableField | dbAutoIncrField,
    TRUE,
    FALSE,
// FIELD 2
    "text1",
    dbText,
    16,     // Max length of text (excluding zero-terminator!)
    dbUpdatableField,
    FALSE,  // Field not required!
    TRUE,   // Allow this string-field to be  'Not having a value'.
// FIELD 3
    "text2",
    dbText,
    32,     // Max length of text (excluding zero-terminator!)
    dbUpdatableField,
    FALSE,  // Field not required!
    TRUE,   // Allow this string-field to be  'Not having a value'.
};


BOOL CSomeClass::InitDB()
{
    // First, you need to determine if the database all-ready
    // exists, and if not create the actual database file.
    // I'll call it 'test.mdb'.

    if(!_access("test.mdb",0))
        return TRUE;

    CDaoDatabase db;

    try
    {
        db.Create("test.mdb");
    }
    catch(CException* p)
    {
        // Something went wrong!
        // Report error to user and make sure that the database
        // file is deleted.
        p->ReportError();
        p->Delete();
        DeleteFile("test.mdb");
        return FALSE;
    }

    // Database file is now created and opened
    // Now, create all the fields we requere.

    CDaoTableDef    table(&db);


    // Create all fields defined in static array 'my_fields'
    try
    {
        // First create the table called 'TestTable'
        //
        table.Create("TestTable");
        TRACE("'TestTable' created.\n");

        for(int i=0;i<sizeof(my_fields)/sizeof(my_fields[0]);i++)
        {
            table.CreateField(
                my_fields[i].strName,
                my_fields[i].nType,
                my_fields[i].lSize,
                my_fields[i].lAttributes );
   
            TRACE("Field '%s' created in 'TestTable'.\n", my_fields[i].strName);
   
        }

        // Append table to database collection and
        // close the tabledef recordset
        table.Append();
        table.Close();
    }
    catch(CException* p)
    {
        // Field creation failed
        // Report error to user, close database and
        // delete invalid database-file.
        p->ReportError();
        p->Delete();

        dc.Close();
        DeleteFile("test.mdb");
        return FALSE;
    }

    db.Close();

    // Table and fields was created successfully!
    return TRUE;
}

0
 

Author Comment

by:MWonch
ID: 1316264
Excellent! Now, aside from the comments, this seems to create three fields when I only need one.  Is that so? If not, then ignore this and please accept my thanks! If yes, can I leave two out in favor of only one?

Mike Wonch
0
 
LVL 4

Expert Comment

by:piano_boxer
ID: 1316265
Yes I create 3 fields in the example.

Look at the my_fields[] static structure. This is defined to have 3 fields ( // FIELD 1, // FIELD 2, // FIELD 3 ). Just change it to something like this (only one field):


// Static information about the fields that you want to create in
// the database table 'TestTable'

MY_FIELD_DEF my_fields[] = {
    "text1",
    dbText,
    16,     // Max length of text (excluding zero-terminator!)
    dbUpdatableField,
    FALSE,  // Field not required!
    TRUE,   // Allow this string-field to be  'Not having a value'.
};


0
 

Author Comment

by:MWonch
ID: 1316266
Thanks!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
not able to insert into temp table 68 150
sameEnds challenge 3 107
scoreUp challenge 14 48
changeXy challenge 13 58
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video discusses moving either the default database or any database to a new volume.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now