Results 1 to 3 of 3

Thread: Create database

  1. #1
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Create database

    Newbie having trouble creating a database here.
    Following code compiles ok, but no database is created and I can't find any sqlite file.

    Qt Code:
    1. #include "makeDB.h"
    2. #include <iostream>
    3. #include <QString>
    4. #include <QtSql>
    5.  
    6. void makeDB(QString dbName) {
    7. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    8. db.setHostName("localHost");
    9. db.setDatabaseName(dbName);
    10. db.setUserName("logger");
    11. db.setPassword("logger");
    12. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QString>
    3. #include <QtSql>
    4. #include "mainwindow.h"
    5. #include "makeDB.h"
    6. #include "createTable.h"
    7. #include "ui_mainwindow.h"
    8.  
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QApplication a(argc, argv);
    13. MainWindow w;
    14. w.show();
    15.  
    16. makeDB("log1");
    17.  
    18. createTable();
    19.  
    20. return a.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong here? Thanks!
    Waynew

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Create database

    Quote Originally Posted by waynew View Post
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setHostName("localHost");
    3. db.setDatabaseName(dbName);
    4. db.setUserName("logger");
    5. db.setPassword("logger");
    To copy to clipboard, switch view to plain text mode 
    SQLite don't support user and password. So simply write:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName("c:/path/to/you/databases/or/whatever/dbname.db");
    To copy to clipboard, switch view to plain text mode 

    Instead of dbname.db you can choose whatever you want.

  3. The following user says thank you to Lykurg for this useful post:

    waynew (8th October 2009)

  4. #3
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: Create database

    Thanks for the help!

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 08:30
  2. cannot share the database connection!!!!
    By cbarmpar in forum Qt Programming
    Replies: 13
    Last Post: 23rd September 2008, 14:42
  3. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 16:56
  4. Database Master-Detail Entry Form
    By Phan Sin Tian in forum Newbie
    Replies: 4
    Last Post: 3rd February 2008, 14:31
  5. Filling combobox from database
    By Philip_Anselmo in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2006, 17:53

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.