2016-03-01 26 views
1

我有一個方法,使對於SQLite的連接,並通過條件與數據庫工作(插入數據或在我的數據庫中創建表。)創建SQLite表是不行的

當我把它在我的代碼,在第一正確打開數據庫,但不能創建表。這意味着查詢不會顯示錯誤,但不會執行。

這是我的方法:

void SQL(string cmnd) 
{ 

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//not dbConnection 
    QString path = "DBAbsent"; 
    db.setDatabaseName(path); 
    QSqlQuery q(db); 

    if(!db.open()) 
    { 

     printf("Error to open database"); 
    } 
    else 
    { 

     if (cmnd=="createTable") 
     { 
      q.prepare("create table IF NOT EXISTS Personel (P_id int primary key, " 
           "CardID varchar(50), " 
           "name varchar(50));"); 

      if(!q.exec()) 
     { 
      qDebug()<<strerror(errno); 
      qDebug()<<q.lastError(); // show this error : QSqlError("", "Unable to fetch row", "No query") 

     } 

      q.prepare("create table IF NOT EXISTS Absent " 
           "(P_id int , " 
           "DateTime varchar(50), " 
           "key1 int,key2 int,key3 int,key4 int);"); 
      if(!q.exec()) 
       qDebug()<<strerror(errno); 

      db.close(); 
      return; 

     } 
     else if (cmnd=="Register") 
     { 
      string card=ReadFromCard(); 
      printf("\nCard Accepted : %s\nEnter Your name: ",card.c_str()); 
      string name; 
      int id; 
      //string dt=QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"); 
      cin>>name; 
      printf("\n Name Accepted.\nEnter Your Personel ID: "); 
      cin>>id; 
      q.prepare("SELECT count(P_id) FROM Personel WHERE P_id =?;"); 
      q.bindValue(0,id); 

      if(!q.exec()) 
      { 
       printf("\nThe error occured from P_ID database"); 
       qDebug()<<strerror(errno); 
       //ShowMainMenu(); 
       //return; 
      } 

      // Note: if you don't return in case of an error, put this into the else{} part 
      while(q.next()) 
      { 
       if (q.value(0).toInt()>0) 
       { 
        printf("\nThis personel ID was repeated\n"); 
        break; 
       } 
       else 
       { 
        q.prepare("SELECT CardID FROM Personel WHERE CardID =?;"); 
        q.bindValue(0,card.c_str()); 
        q.exec(); 

        while(q.next()) 
        { 
         printf("\nThis card is not valid : it was repeated\n"); 
         break; 
        } 
       } 
      } 

      q.prepare("insert into Personel value(?,?,?);"); 
      q.bindValue(0,id); 
      q.bindValue(1,card.c_str()); 
      q.bindValue(2,name.c_str()); 
      q.exec(); 
      printf("\nInsert Personel complete\n"); 
      db.close(); 
      return; 

     } 
     else 
     { 
      string card; 
      card=cmnd; 
      QString dt=QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"); 

      q.prepare("SELECT P_id FROM Personel WHERE CardID =?;"); 
      q.bindValue(0,card.c_str()); 

      if(!q.exec()) 
      { 
       printf("\nThe error occured from database418\n"); 
       ShowMainMenu(); 

      } 
      string result=("\n CARD NOT VALID =>repeated  %s\n"); 
      // Note: if you don't return in case of an error, put this into the else{} part 
      while(q.next()) 
      { 
       int P_id = q.value(0).toInt(); 
       q.prepare("insert into Absent(P_id,DateTime,key1,key2,key3,key4) value(?,?,1,0,0,0);"); 
       q.bindValue(0,P_id); 
       q.bindValue(1,dt); 
       q.exec(); 
       result=("\n********WELL COME **********%s\n"); 
      } 
      printf("%s",result); 
      db.close(); 
      return; 
     } 

    } 
} 

這是我的輸出:

0 - Press 0 to Back 
    1 - Press 1 to Register the card 
    1 
    //the below lines when I want to select something from both tables , occurred 
    No such file or directory 
    QSqlError("", "Unable to fetch row", "No query") 

    No such file or directory 
    //the below line is when the if(cmnd="Register") {..} block is running , occurred. 
    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed. 

回答

0

解決了。只要做到這些:
1 - 由於@Tony說:QSqlQuery應該在打開數據庫後定義。
2 - q.prepare只是用於SQL查詢包含bindvalue(),select()insert()

0

你並不需要關閉並每次SQL()函數運行開放數據庫連接。更好的方法是打開一次數據庫,當你的應用程序啓動並在最後關閉它。然後,在SQL(..)中,您只需創建如下查詢:

QSqlQuery q; 

並使用它。

QSqlError("", "Unable to fetch row", "No query")的原因是您在db打開之前創建了QSqlQuery(db);。你也需要檢查q.prepare()是否有錯誤。

+0

謝謝,但我改變了QSqlQuery q(db)的位置;打開數據庫後,但沒有改變。 – MHM

+0

q.prepare()返回什麼錯誤? –