Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

creating types at runtime

Posted on 2003-02-25
5
Medium Priority
?
276 Views
Last Modified: 2010-04-17
in my application i am creating databases at runtime. now for that i need the fieldnames of the database table dynamically. now i want it to be fairly object oriented. now if i create a type that contains the fields names and field types of the database, that becomes static. that is my problem. i want to be able to create types at runtime so that i can create my database without knowing at compile time the no. of fields or the type of fields in the database. please help...
0
Comment
Question by:donzden
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
TheBeaver earned 400 total points
ID: 8016739
Depends what language / database you are using.

Most versions of SQL have a "CREATE TABLE" command.

If your language supports running queries that are stored in strings, you could dynamically create the CREATE TABLE command in the string.

eg.

typedef struct def_struct
{
  char type[8];
  char name[32];
} DEF;


CreateTable(DEF *def, int fields)
{
  char str[256] = "CREATE TABLE ...";
  int f=0;

  while( f < fields )
  {
    sprintf(str, "%s ADD TABLE ... %s ... %s ... ", def[f]->type, def[f]->name);
    f++;
  }
  // Here you would execute str as an SQL statement
}

...as you can see I've left the actual syntax up to you to research. But do you get the idea of the logic/method involved?
0
 

Assisted Solution

by:samgiuoco
samgiuoco earned 400 total points
ID: 8018280
I am assuming you want help more with the runtime organization of a type than with physically creating a table.

To my knowledge you cant create types at runtime.  What you can do, however, is create a basic field type at design time with a construct to hold the information you want.  Then you can make an undimensioned array of that type and Redim during runtime to the correct number of fields.


DESIGN TIME

Public Type FieldsType
   Name as String
   Index as string
   DataType as string
end Type

Dim MyDatabase() as FieldsType


RUNTIME

Redim MyDatabase(1 to fieldcount)
 
0
 

Expert Comment

by:CleanupPing
ID: 9447519
donzden:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 

Expert Comment

by:samgiuoco
ID: 9447754
I recommend a 100/100 split between me and the beaver.  Since he didn't need to post anymore I would assume one of us solved his problem.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Progress

572 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