//批量插入数据 //odbc风格,预处理语句,其中?相当于占位符 query.prepare("insert into student(name,age,score) values(?,?,?)"); //将字段内容添加到指定list中 QVariantList namelist; namelist <<"zhao"<<"qian"<<"sun"<<"li"; QVariantList agelist; agelist <<10<<20<<30<<40; QVariantList scorelist; scorelist <<60<<70<<80<<90; //给字段按顺序绑定相应的值 query.addBindValue(namelist); query.addBindValue(agelist); query.addBindValue(scorelist); //执行预处理命令 if(!query.execBatch()) { qDebug() << query.lastError(); }
//oracle风格:占位符+自定义名字 query.prepare("insert into student(name, age, score) values(:name, :age, :score)"); QVariantList Namelist; Namelist<<"zhou"<<"wu"<<"zheng"<<"wang"; QVariantList Agelist; Agelist<<11<<21<<31<<41; QVariantList Scorelist; Scorelist<<61<<71<<81<<91; //为前面留下的:标志绑定 query.bindValue(":name",Namelist); query.bindValue(":age",Agelist); query.bindValue(":score",Scorelist); //执行预处理命令 query.execBatch();
跟着qt5的视频学了两种写法,但是那两种写法不能写入MySQL数据库
经过测试,这两种写法能实现批量添加。
标签:QT6,name,批量,写法,age,score,添加,数据库 From: https://www.cnblogs.com/wenjis-nine-life-cat/p/18340841