2011-02-15 38 views
0

我想使用sqlite作爲數據庫的Symbian應用程序,但我不能創建一個表。這裏的代碼:C + + Qt 4 Sqlite:不能創建表

bool DatabaseManager::createExpenseTable(){ 

    if(QFile::exists(dbName)){ 
     this->showDebugMsg("Database file exist"); 
    }else{ 
     this->showDebugMsg("Database file exist DOES NOT exist"); 
    } 

    // Create table "person" 
    bool ret = false; 
    if (db.isOpen()){ 
     this->showDebugMsg("Database open"); 
     QSqlQuery query; 
     ret = query.exec("create table expense " 
        "(id int primary key, " 
        "item varchar(100)"); 
        //"price double, " 
        //"date datetime)"); 

    }else{ 
     this->showDebugMsg("Database CLOSED"); 
    } 
    if(ret){ 
     this->showDebugMsg("Table created"); 
    }else{ 
     this->showDebugMsg("Table NOT created"); 

    } 
    return ret; 
    } 

據我可以看到從調試消息「數據庫文件存在」和「數據庫打開」數據庫存在,並打開。

但我總是得到「table not created」消息。你們有沒有看到問題出在哪裏?

回答

1

等一下,我發現了這個問題。與此代碼:

this->showDebugMsg(query.lastError().text()); 

我看到「表費用已經存在」,因此,它不會創建一個新的。對不起,打擾你們所有人。

1

在查詢中缺少一個右括號(您評論過)。

+0

它已關閉。在評論部分前面看一看。順便說一下,如果它們不是密切的,代碼將不會編譯。 – Segolas

+2

引用的SQL中缺少括號! – hmn

+0

@Segolas:它沒有關閉。 hmn是對的。您已經註釋了包含關閉參數的代碼。刪除引號併合並字符串會顯示您發佈的代碼執行以下查詢:'創建表費用(id int primary key,item varchar(100)',缺少最後一個parens。即使代替由於編譯器無法猜測你傳遞給SQL驅動程序是否是有效的SQL,或者因爲它甚至不知道它是SQL,它只是傳遞你告訴它的任何字節,因此'kjrfhiherfhdfgvjhfgvjh'查詢到SQL驅動程序 –