1

If I have a list that contains like 50 elements, how can I create 50 independent tables for my data base?

My code is like that:

for ticker in B3tickers():

    cur.execute( 'create table ? '\
'(data datetime primary key, '\
'open REAL, '\
'high REAL, '\
'low REAL, '\
'close REAL, '\
'adjclose REAL)')

B3tickers is a list with all Brazilian companies symbol.

3
  • Which SQL database are you running? Commented Sep 19, 2020 at 21:03
  • SQlite 3, but thanks I've already done what I was supposed to Commented Sep 20, 2020 at 1:59
  • As an aside, 50 identically structured tables in a relational database may not be optimal design. Commented Sep 20, 2020 at 2:14

2 Answers 2

1

You would want to use a single sql script itself not run an execute aka:

IF  NOT EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[Customer]') AND type in (N'U'))

BEGIN
CREATE TABLE [dbo].[Customer](
   column1 datatype,
   column2 datatype,
   column3 datatype,
);
END

Sign up to request clarification or add additional context in comments.

Comments

0

After some guys helped me on another question I did solve the problem: Here is the code:

for i in range(0,len(B3tickers())):
    name = B3tickers()[i]
    cur.execute( 'CREATE TABLE {name} '\
'(data datetime primary key, '\
'Open REAL, '\
'high REAL, '\
'low REAL, '\
'close REAL, '\
'adjclose REAL)'.format(name=name))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.