class Test(): cursor = connection.cursor() data_to_insert = [] sql = "INSERT INTO test_t (id, name ) VALUES" """ (%s, %s) """ d = ('1', "apple") data_to_insert.append(d) d = ('2', "orange") data_to_insert.append(d) # 查看执行的sql语句 sql = cursor.mogrify(sql, data_to_insert) print(sql) # 执行批量插入 cursor.executemany(sql, data_to_insert) #创建表 cursor.execute(""" CREATE TABLE TEST_BLOCT ( ID int auto_increment COMMENT 'id', NAME VARCHAR(40) NOT NULL COMMENT '姓名', PRIMARY KEY (ID) ) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COMMENT='测试表'; """) # 创建索引 cursor.execute(""" CREATE INDEX TEST_BLOCT_INDEX on TEST_BLOCT (NAME ) ; """) # 插入数据 sql = "INSERT INTO TEST_BLOCT(ID,NAME ) VALUES (%s, %s)" params = [("1", "hello")] cursor.executemany(sql, params)
标签:insert,BLOCT,python,动态创建,cursor,sql,TEST,data From: https://www.cnblogs.com/yclh/p/17715422.html