user2470026
user2470026

Reputation: 157

how to add column name present in the list object into table

how to do like this

cursor.execute("create table" + config.table + config.cols)

all the config names are in different files and cols is a list object like this

cols = [
  "name varchar(50)",
  "address varchar(50)",
  "etc "

i have a error like this

TypeError: cannot concatenate 'str' and 'list' objects

how to concatenate str with the list objects... Please help!!

Upvotes: 0

Views: 284

Answers (1)

AMADANON Inc.
AMADANON Inc.

Reputation: 5919

Usually when you want to combine a list (or array) with a string, you need to use join(). Assuming config.table is a string, and config.cols is an array/list, you might want something like:

cursor.execute("create table " + config.table + " (" + ", ".join(config.cols) + ")")

This combines all the individual cols, by putting commas between them (but not before the first or after the last). I also added a () around the field definition, and a space between the word table, and the table name.

If it still doesn't work, try printing the string out, instead of executing it, to make sure it is valid SQL.

Upvotes: 1

Related Questions