看板 C_and_CPP 關於我們 聯絡資訊
我的開發平台是 win 工具是QT Creator 版本是Qt Core 5.15.2 我程式是要把資料庫的所有id拿出來去別的地方撈資料 資料撈回來之後要處理一下然後再更新回資料庫 Infos 只是一個一堆QString欄位的struct 雖然只有一筆資料要query但我還是用prepare-addBindValue的原因是偷懶 因為這樣就不用管如果字串裡有任何有可能會需要escape的字元 void MainWorker::updateGalleryInfo(Infos info){ QString queryString{"SELECT tags FROM gallerys WHERE id = ?;"}; mQuery->prepare(queryString); mQuery->addBindValue(info.id); if (!mQuery->execBatch()){ qDebug() << __LINE__ << mQuery->lastError().text(); }else if (!mQuery->first()){ qDebug() << __LINE__ << mQuery->lastError().text(); }else{ //如果有tag的話就做一些處理,再準備更新進去,其實不重要 QStringList oldTags = mQuery->value(1).toString().split(",", Qt::SkipEmptyParts); foreach (QString tag, oldTags) { if (!info.tags.contains(tag)){ info.tags << tag; } } } QString updateString{"UPDATE gallerys SET " "titleEN = ?, " "titleJP = ?, " "nameEN = ?, " "nameJP = ?, " "category = ?, " "publishDate = ?, " "parent = ?, " "tags = ?, " "isUpdated = 'yes' WHERE id = ?;"}; mQuery->prepare(updateString); mQuery->addBindValue(info.titleEN); mQuery->addBindValue(info.titleJP); mQuery->addBindValue(info.nameEN); mQuery->addBindValue(info.nameJP); mQuery->addBindValue(info.category); mQuery->addBindValue(info.publishDate); mQuery->addBindValue(info.parent); mQuery->addBindValue(info.tags.join(",")); mQuery->addBindValue(info.id); if (!mQuery->execBatch()){ qDebug() << __LINE__ << mQuery->lastError().text(); } } 執行至此,沒有跳出錯誤訊息,但也沒有寫入,因為isUpdated仍是no https://imgur.com/kX7h4hz.jpg
而我把最後面這個update的query測試,確實是可以update進去的 但不知道為什麼我的程式卻無法更新進去 卡了好久,求救... 感謝閱讀 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.192.225.144 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1667146900.A.B53.html ※ 編輯: liu2007 (123.192.225.144 臺灣), 10/31/2022 00:22:24
lc85301: 好奇 id 是對的嗎,會不會 where 排除掉了? 11/01 00:16