bulk insert mysql from list of tuples

Solutions on MaxInterview for bulk insert mysql from list of tuples by the best coders in the world

showing results for - "bulk insert mysql from list of tuples"
Claudio
24 Jun 2018
1#function to transform your list into a string
2def stringify(v): 
3    return "('%s', '%s', %s, %s)" % (v[0], v[1], v[2], v[3])
4
5#transform all to string
6v = map(stringify, row)
7
8#glue them together
9batchData = ", ".join(e for e in v)
10
11#complete the SQL
12sql = "INSERT INTO `table_name`(`column`, `column_1`, `column_2`, `column_3`) \
13VALUES %s" % batchData
14
15#execute it
16cursor.execute(sql)
17db.commit()
18