看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: Win10, Linux, ...) Linux Ubuntu14.04 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) Qt5.7.0 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 已先在終端機登入mysql並創建好資料庫和表格,但Qt無法連接讀取資料 餵入的資料(Input): 預期的正確結果(Expected Output): Database connection established 資料庫的資料 Closing... 錯誤結果(Wrong Output): Database connection established error: "no such table: country Unable to execute statement" Closing... 程式碼(Code):(請善用置底文網頁, 記得排版) QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE"); db.setHostName("localhost"); db.setUserName("root"); db.setPassword("4171"); db.setDatabaseName("world"); if(!db.open()) { qDebug()<<"Unable to open database"; return a.exec(); } else { qDebug()<<"Database connection established"; QSqlQuery qry; if(qry.exec("select * from country")) { while(qry.next()) { qDebug()<<qry.value(0).toString().trimmed(); } } else { qDebug()<<"error:"<<qry.lastError().text(); } qDebug()<<"Closing..."; db.close(); return a.exec(); } 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.117.167.198 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1491477265.A.CD3.html ※ 編輯: zxc123270 (140.117.167.198), 04/06/2017 19:15:09 ※ 編輯: zxc123270 (140.117.167.198), 04/06/2017 19:16:09
Sidney0503: 你開mysql然後連sqlite? 04/06 20:26
soheadsome: 為什麼是sqlite? 04/06 21:09
我是智障嗎 = = 感謝各位解答,我明天去實驗室再改看看 ※ 編輯: zxc123270 (140.117.196.119), 04/06/2017 22:15:56
uranusjr: XDDDDDDD 04/06 22:46
shadow0326: ... 04/07 13:13
ken1325: ........ 04/07 14:55
narcissusli: …… 04/08 01:20