2012-05-12 85 views
2
query->prepare("INSERT INTO businessweek (id, source, date, headline, body) VALUES (NULL, ?, ?, ?, ?)"); 
query->bindValue(1, "source"); 
query->bindValue(2, "date"); 
query->bindValue(3, "headline"); 
query->bindValue(4, "body"); 

if (query->exec()) { 
    tt << "Query success"; 
} else { 
    qDebug() << db.lastError(); 
    return; 
} 

我正在使用QMYSQL。且誤差Qt sql查詢失敗

QSqlError(-1, "", "") 

query->exec("INSERT INTO businessweek (id, source, date, headline, body) VALUES (NULL, 'google', 'may 0, 23', 'head', 'fjsdflksjdlkfdsjlfkjd');") 

工作正常。

回答

3

很難猜出問題。但有一件事我可以想象,而且你的代碼肯定是錯誤的,bindValue的字段編號從0開始(請參閱here)。

因此,我建議嘗試

query->prepare("INSERT INTO businessweek (id, source, date, headline, body) VALUES (NULL, ?, ?, ?, ?)"); 
query->bindValue(0, "source"); 
query->bindValue(1, "date"); 
query->bindValue(2, "headline"); 
query->bindValue(3, "body"); 

if (query->exec()) { 
    tt << "Query success"; 
} else { 
    qDebug() << db.lastError(); 
    return; 
} 
+0

謝謝。它以0開頭:) – Dewsworld