0
votes

I'm looking for an answer to the following question:
Is it possible to 'Create a Table' and to 'Input Values' with various Arrays?

My Problem is, that I have a large amount of values which I'd like to insert into the table. Because of that it's not really practical for me, to do the standard 'CreateTable' stuff like c.execute('''CREATE TABLE calc_matrix (date text, trans text, symbol text, qty real, price real)''')

I have the following Arrays which I like to insert:
1. Array: column_name [200] = [ColName1 string, ColName2 string, ...., ColName200 string]
2. Array: Result_1 [200] = [ Value1 int, Value2 double, Date date, Value3 float, ...., Value185 int, Value 186 float]
3. Array: Result_2 [200] = [ Value1 int, Value2 double, Date date, Value3 float, ...., Value185 int, Value 186 float]
4. Array: Result_3 [200] = [ Value1 int, Value2 double, Date date, Value3 float, ...., Value185 int, Value 186 float]

Finale Table Example:
[0]|ColName1|ColName2|ColName3|ColName4|...|ColName199|ColName200| ______________________________________________________________
[1]|Value1|Value2|Date|Value3|....|Value198|Value199|
[2]|Value1|Value2|Date|Value3|....|Value198|Value199|
[3]|Value1|Value2|Date|Value3|....|Value198|Value199|
[4]|Value1|Value2|Date|Value3|....|Value198|Value199|
.
.
.
[150]|Value1|Value2|Date|Value3|....|Value198|Value199|

Therefore I don't know how to accomplish that.

I've tried things like:
c.execute('''CREATE TABLE calc_matrix %s''', column_name)
Or in my loop:
c.execute('''CREATE TABLE calc_matrix ?''', column_name[i])

but nothing worked ......

1
You shouldn't. The best solution is to normalize your schema by an index column and have one row per array element. - Colonel Thirty Two

1 Answers

0
votes

Try to create the full SQL command in a string first and then pass the string into c.execute

string = "CREATE TABLE calc_matrix ( "
your for loop
    string += column_name[i] + ", "

string += ")"
c.execute(string)

Your for loop wasn't working before because your program was trying to execute many CREATE TABLE commands that all had the same table name calc_matrix. You need the entire command in one c.execute statement.

The same pattern can be used for inserting many rows into a table.

string = "INSERT INTO calc_matrix VALUES ("
your for loop
    string += result_1[i] + ", "

string += ")"
c.execute(string)