Results 1 to 3 of 3

Thread: Create sqlite table is not work

  1. #1
    Join Date
    Apr 2015
    Posts
    4
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Exclamation Create sqlite table is not work

    I have a method that make a connection to SQLITE and work with database by conditions (insert data or create table in my database.)

    When I call it in my code, at first open database correctly but can not create tables.

    Qt Code:
    1. the SQLite said No such table
    To copy to clipboard, switch view to plain text mode 

    this is my method :

    Qt Code:
    1. void SQL(string cmnd)
    2. {
    3.  
    4. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//not dbConnection
    5. QString path = "DBAbsent";
    6. db.setDatabaseName(path);
    7. QSqlQuery q(db);
    8.  
    9. if(!db.open())
    10. {
    11.  
    12. printf("Error to open database");
    13. }
    14. else
    15. {
    16.  
    17. if (cmnd=="createTable")
    18. {
    19. q.prepare("create table IF NOT EXISTS Personel (P_id int primary key, "
    20. "CardID varchar(50), "
    21. "name varchar(50));");
    22.  
    23. if(!q.exec())
    24. {
    25. qDebug()<<strerror(errno);
    26. qDebug()<<q.lastError(); // show this error : QSqlError("", "Unable to fetch row", "No query")
    27.  
    28. }
    29.  
    30. q.prepare("create table IF NOT EXISTS Absent "
    31. "(P_id int , "
    32. "DateTime varchar(50), "
    33. "key1 int,key2 int,key3 int,key4 int);");
    34. if(!q.exec())
    35. qDebug()<<strerror(errno);
    36.  
    37. db.close();
    38. return;
    39.  
    40. }
    41. else if (cmnd=="Register")
    42. {
    43. string card=ReadFromCard();
    44. printf("\nCard Accepted : %s\nEnter Your name: ",card.c_str());
    45. string name;
    46. int id;
    47. //string dt=QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
    48. cin>>name;
    49. printf("\n Name Accepted.\nEnter Your Personel ID: ");
    50. cin>>id;
    51. q.prepare( "SELECT count(P_id) FROM Personel WHERE P_id =?;" );
    52. q.bindValue(0,id);
    53.  
    54. if( !q.exec() )
    55. {
    56. printf("\nThe error occured from P_ID database");
    57. qDebug()<<strerror(errno);
    58. //ShowMainMenu();
    59. //return;
    60. }
    61.  
    62. // Note: if you don't return in case of an error, put this into the else{} part
    63. while( q.next() )
    64. {
    65. if (q.value(0).toInt()>0)
    66. {
    67. printf("\nThis personel ID was repeated\n");
    68. break;
    69. }
    70. else
    71. {
    72. q.prepare("SELECT CardID FROM Personel WHERE CardID =?;");
    73. q.bindValue(0,card.c_str());
    74. q.exec();
    75.  
    76. while(q.next())
    77. {
    78. printf("\nThis card is not valid : it was repeated\n");
    79. break;
    80. }
    81. }
    82. }
    83.  
    84. q.prepare("insert into Personel value(?,?,?);");
    85. q.bindValue(0,id);
    86. q.bindValue(1,card.c_str());
    87. q.bindValue(2,name.c_str());
    88. q.exec();
    89. printf("\nInsert Personel complete\n");
    90. db.close();
    91. return;
    92.  
    93. }
    94. else
    95. {
    96. string card;
    97. card=cmnd;
    98. QString dt=QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
    99.  
    100. q.prepare("SELECT P_id FROM Personel WHERE CardID =?;");
    101. q.bindValue(0,card.c_str());
    102.  
    103. if( !q.exec() )
    104. {
    105. printf("\nThe error occured from database418\n");
    106. ShowMainMenu();
    107.  
    108. }
    109. string result=("\n CARD NOT VALID =>repeated %s\n");
    110. // Note: if you don't return in case of an error, put this into the else{} part
    111. while( q.next() )
    112. {
    113. int P_id = q.value(0).toInt();
    114. q.prepare("insert into Absent(P_id,DateTime,key1,key2,key3,key4) value(?,?,1,0,0,0);");
    115. q.bindValue(0,P_id);
    116. q.bindValue(1,dt);
    117. q.exec();
    118. result=("\n********WELL COME **********%s\n");
    119. }
    120. printf("%s",result);
    121. db.close();
    122. return;
    123. }
    124.  
    125. }
    126. }
    To copy to clipboard, switch view to plain text mode 
    And this is my output :

    Qt Code:
    1. 0 - Press 0 to Back
    2. 1 - Press 1 to Register the card
    3. 1
    4. //the below lines when I want to select something from both tables , occurred
    5. No such file or directory
    6. QSqlError("", "Unable to fetch row", "No query")
    7.  
    8. No such file or directory
    9. //the below line is when the if(cmnd="Register") {..} block is running , occurred.
    10. QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    To copy to clipboard, switch view to plain text mode 
    c++ sqlite
    Last edited by galaxy66; 1st March 2016 at 10:06.

  2. #2
    Join Date
    May 2015
    Posts
    9
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Create sqlite table is not work

    Hi:

    Try this
    Qt Code:
    1. q.prepare("create table IF NOT EXISTS 'Personel' (P_id int primary key, "
    2. "'CardID' varchar(50), "
    3. "'name' varchar(50))");
    To copy to clipboard, switch view to plain text mode 
    Best regards.

  3. #3
    Join Date
    Apr 2015
    Posts
    4
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Create sqlite table is not work

    It solved. Just do these :
    1 - As @Tony said: QSqlQuery should defined after opening database.
    2 - q.prepare just uses for SQL query contains bindvalue(), like select() or insert() .

Similar Threads

  1. Replies: 4
    Last Post: 30th August 2015, 14:07
  2. Replies: 13
    Last Post: 6th December 2010, 05:41
  3. Replies: 2
    Last Post: 4th August 2010, 14:06
  4. Replies: 5
    Last Post: 3rd April 2010, 05:07
  5. Replies: 4
    Last Post: 10th March 2007, 19:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.