On Thu, 2009-08-13 at 01:12 +0100, Simon Slavin wrote:
> On 12 Aug 2009, at 11:35pm, Igor Tandetnik wrote:
> 
> > Bill Welbourn <biostats...@yahoo.com>
> > wrote:
> >> I am new to the forum and have a question regarding querying data
> >> from a table, where the table name is random. For example, I have a
> >> database possessing upwards of 41 tables named, TAB10, TAB11,...,
> >> TAB50, and would like to query each table.
> >
> > Consider instead having a single table with an extra column containing
> > values 10, 11, ... , 50. You'll have no end of trouble with your  
> > current
> > design.
> 
> I second this advice.

I also agree it is best to reconsider your schema.

> >> Question: Within the C programming environment, is it possible to
> >> prepare a sqlite SELECT query with a random table name as a
> >> variable/parameter?
> >
> > No. Which is precisely why you should reconsider your design.
> 
> Although you can do it by executing your queries as simple text  
> commands using _execute rather than using _prepare and _step.
> 
> Simon.

You could prepare 41 queries, and in code step the correct one.

sqlite3_prepare_v2(db, "SELECT * FROM TAB10", -1, &q10, 0)
....
sqlite3_prepare_v2(db, "SELECT * FROM TAB50", -1, &q50, 0)


You could do it in a single query, but it would be really ugly:

SELECT * FROM TAB10 WHERE 'TAB10' = :TAB
UNION ALL
SELECT * FROM TAB11 WHERE 'TAB11' = :TAB
UNION ALL
....
SELECT * FROM TAB50 WHERE 'TAB50' = :TAB;


David






_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to