Results 1 to 2 of 2

Thread: Database qustion

  1. #1
    Join Date
    Feb 2010
    Location
    Wokingham, United Kingdom
    Posts
    36
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Database qustion

    hi, I'm writing an interface to work with database tables, and i was wondering if it were possible to add a table without the sql statement. I want to be able to call my function, lets call it
    Qt Code:
    1. addTable(QString name, QSqlRecord rec)
    To copy to clipboard, switch view to plain text mode 
    where the QSqlFields in 'rec' contain the field names, and types.
    Qt Code:
    1. //so this...
    2. QSqlQuery query("CREATE TABLE table (id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT)",db);
    3.  
    4. // becomes
    5. QSqlField id, data;
    6. QSqlRecord record;
    7. // yada yada yada
    8. addTable("table", record);
    To copy to clipboard, switch view to plain text mode 
    thanks

  2. #2
    Join Date
    Feb 2010
    Location
    Wokingham, United Kingdom
    Posts
    36
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Database qustion

    sorted it out with some help from the kexi source code...
    heres my .h
    Qt Code:
    1. #ifndef BACKEND_H
    2. #define BACKEND_H
    3.  
    4. #include <QObject>
    5. #include <QSqlDatabase>
    6. #include <QSqlTableModel>
    7. #include <QSqlQuery>
    8. #include <QSqlRecord>
    9. #include <QDebug>
    10. #include <QStringList>
    11. #include <QList>
    12.  
    13. struct field {
    14. QString type;
    15. QString name;
    16.  
    17. bool isKey;
    18. bool isNotNull;
    19. bool isAutoIncrement;
    20.  
    21. field() : isKey(false), isNotNull(false), isAutoIncrement(false){}
    22.  
    23. };
    24.  
    25. Q_DECLARE_METATYPE(field);
    26.  
    27. class backend : public QObject
    28. {
    29. Q_OBJECT
    30. public:
    31. explicit backend(QObject *parent = 0);
    32. ~backend();
    33.  
    34. QSqlDatabase * database() { return ptr_db; }
    35. bool sqlExec(QString sql);
    36. bool addTable(QString name, const QList<field>& fields);
    37. bool dropTable(QString name);
    38. void addRecord(QString table, QSqlRecord record, int pos = -1);
    39. bool isConnected() { return connected; }
    40. bool connectDatabase(QString connection_name, QString db_name);
    41. QString connectionName() { return ptr_db->connectionName(); }
    42.  
    43.  
    44. signals:
    45.  
    46. public slots:
    47.  
    48. private:
    49. QSqlDatabase *ptr_db;
    50. bool connected;
    51. };
    52.  
    53. #endif // BACKEND_H
    To copy to clipboard, switch view to plain text mode 

    and the .cpp
    Qt Code:
    1. #include "backend.h"
    2.  
    3. backend::backend(QObject *parent) :
    4. QObject(parent)
    5. {
    6. connected = false;
    7. }
    8.  
    9. backend::~backend()
    10. {
    11. ptr_db->close();
    12. ptr_db->removeDatabase(ptr_db->connectionName());
    13. }
    14.  
    15. bool backend::connectDatabase(QString connection_name, QString db_name)
    16. {
    17. ptr_db = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE", connection_name));
    18. ptr_db->setDatabaseName(db_name);
    19. if (ptr_db->open()){
    20. connected = true;
    21. qDebug() << "database connection established...";
    22. return true;
    23. }
    24. connected = false;
    25. qDebug() << "unable to connect to database...";
    26. return false;
    27. }
    28.  
    29. bool backend::dropTable(QString name)
    30. {
    31. QSqlQuery query(*ptr_db);
    32. if (!query.exec("DROP TABLE IF EXISTS " + name)){
    33. return false;
    34. }
    35. qDebug() << "table" << name << "dropped from database...";
    36. return true;
    37. }
    38.  
    39. void backend::addRecord(QString table, QSqlRecord record, int pos)
    40. {
    41. model.setTable(table);
    42. model.database().transaction();
    43. model.insertRecord(pos, record);
    44. model.database().commit();
    45. }
    46.  
    47. bool backend::sqlExec(QString sql)
    48. {
    49. QSqlQuery query(sql, *ptr_db);
    50. if (!query.isActive()){
    51. return false;
    52. }
    53. return true;
    54. }
    55.  
    56. bool backend::addTable(QString name, const QList<field> &fields)
    57. {
    58. bool first = true;
    59.  
    60. s = "CREATE TABLE ";
    61. s.append(name.toLatin1() + " ( ");
    62.  
    63. foreach (field f , fields){
    64. if (first){
    65. first = false;
    66. } else {
    67. s.append(", ");
    68. }
    69.  
    70. s.append(f.name.toLatin1() + " " + f.type.toLatin1());
    71.  
    72. if (f.isKey){
    73. s.append(" PRIMARY KEY");
    74. }
    75.  
    76. if (f.isAutoIncrement){
    77. s.append(" AUTOINCREMENT");
    78. }
    79.  
    80. if (f.isNotNull){
    81. s.append(" NOT NULL");
    82. }
    83. }
    84.  
    85. s.append(")");
    86.  
    87. QSqlQuery q(*ptr_db);
    88.  
    89. if (q.exec(s)){
    90. return true;
    91. }
    92.  
    93. return false;
    94. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. database
    By sattu in forum Qt Programming
    Replies: 7
    Last Post: 23rd September 2010, 07:30
  2. regarding database
    By sattu in forum Qt Programming
    Replies: 1
    Last Post: 18th September 2010, 12:29
  3. Replies: 9
    Last Post: 20th May 2010, 09:55
  4. Database
    By hazardpeter in forum Newbie
    Replies: 3
    Last Post: 19th December 2009, 17:48
  5. Using database
    By debmag in forum Newbie
    Replies: 2
    Last Post: 6th February 2009, 08:15

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.