[Zope] SQLs from Python strings

Lee lee.reilly@ntlworld.com
Sat, 06 Jan 2001 16:39:51 +0000


On first impressions this may look like a homework assignment question
but I can assure you it isn't! I'm having difficulty trying to explain
this (hence the strange subject line ;-) but I hope someone can offer me
some advice...

I have four attributes (A, B, C, D) and each is an integer say, between
0 and 9.

I need a method to create a table with the following columns:

Name
Age
[if A=3] then A1, A2, A3
[if B=1] then B1

i.e. if A=3 and B=1 and C & D =0 then 6 columns will be created.

I already have the code (well, the logic) to do this but what is the
best way to do it. ZSQL methods can't handle such logic, right?
I was thinking maybe passing the parameters into a python method to
build a "CREATE TABLE..." string... but can this be used to actually
execute the query?

I don't know if I have explained this well enough.

Here's the logic (no particular language and A,B,C,D = P,T,A,B)

int p = PRACTICALS;
int t = TUTORIALS;
int a = ASSIGNMENTS;
int b = BONUS;

string classAndCode = CLASSCODE;

String createDB = "CREATE TABLE " + classAndCode + " ( "

while (p != 0)
  {
  String createDB = create DB + "p" + p + "integer, " // e.g. p0, p1,
p2, etc.
  p = p-1;
  }

while (t != 0)
  {
  String createDB = create DB + "t" + t + "varchar, "
  t = t-1;
  }

... etc

Okay, say I have a python method, which returns an String (an SQL CREATE
TABLE statement). How could I execute this?

Thanks very much in advance.

Lee

PS: Alex, I managed to get the COUNT var to work. The solution was
simply a good night's sleep and a large mug of coffee in the morning.
Cheers :)

--
Lee Reilly
mailto:lee.reilly@ntlworld.com
http://www.footkick.co.uk/lee