?
Solved

creating types at runtime

Posted on 2003-02-25
5
Medium Priority
?
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Starting up a Project
Suggested Courses
Course of the Month8 days, 2 hours left to enroll

765 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