Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to create a new DataBase in SOL Server
Forum Updated to NodeBB v4.3 + New Features

How to create a new DataBase in SOL Server

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • zhmhZ Offline
    zhmhZ Offline
    zhmh
    wrote on last edited by zhmh
    #1

    With this code I can connect to database that exist in SQL server and create table in it

    db = QSqlDatabase::addDatabase("QODBC");
      bool test=db.isValid();//true
      test=db.isDriverAvailable("QODBC");//true
      db.setHostName("localhost");
     db.setDatabaseName("DRIVER={SQL Server};SERVER=localhost;DATABASE=attendance97");
      test=db.isValid();//true
      if(!db.open())
          qDebug()<<"not connected";
      else
          qDebug()<<"connected";
    
          QSqlQuery qry;
        int res=qry.exec("create table ZMFar "
                    "(id integer primary key, "
                     "firstname varchar(20), "
                      "lastname varchar(30), "
                    "age integer)");
    
       qDebug() << "exec :" << qry.lastError();
    

    but I want to create DataBase whit variable name first then create table in it, for example

    if (m==98)
     greate database and set name --> attendance98
    

    How can I do it?

    JonBJ 1 Reply Last reply
    0
    • zhmhZ zhmh

      With this code I can connect to database that exist in SQL server and create table in it

      db = QSqlDatabase::addDatabase("QODBC");
        bool test=db.isValid();//true
        test=db.isDriverAvailable("QODBC");//true
        db.setHostName("localhost");
       db.setDatabaseName("DRIVER={SQL Server};SERVER=localhost;DATABASE=attendance97");
        test=db.isValid();//true
        if(!db.open())
            qDebug()<<"not connected";
        else
            qDebug()<<"connected";
      
            QSqlQuery qry;
          int res=qry.exec("create table ZMFar "
                      "(id integer primary key, "
                       "firstname varchar(20), "
                        "lastname varchar(30), "
                      "age integer)");
      
         qDebug() << "exec :" << qry.lastError();
      

      but I want to create DataBase whit variable name first then create table in it, for example

      if (m==98)
       greate database and set name --> attendance98
      

      How can I do it?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @zhmh
      To create a new database you need to Google for something like odbc create database. Have a look at e.g. https://stackoverflow.com/a/41829339/489865. Qt does not have some call to do that for you. You see it issues CREATE DATABASE statements across ODBC, you need to do similar.

      zhmhZ 1 Reply Last reply
      2
      • JonBJ JonB

        @zhmh
        To create a new database you need to Google for something like odbc create database. Have a look at e.g. https://stackoverflow.com/a/41829339/489865. Qt does not have some call to do that for you. You see it issues CREATE DATABASE statements across ODBC, you need to do similar.

        zhmhZ Offline
        zhmhZ Offline
        zhmh
        wrote on last edited by zhmh
        #3

        @JonB how about sqlite?can create new database from QT code?

        JonBJ 1 Reply Last reply
        0
        • zhmhZ zhmh

          @JonB how about sqlite?can create new database from QT code?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @zhmh
          No, there is no special, individual function call to create a database from Qt, whatever the underlying data provider. But doing it from code is not difficult. I already gave you a link with the few lines of code required for ODBC, there is no reason why you should not use it.

          zhmhZ 1 Reply Last reply
          1
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            Hi
            Just as a note.
            With Sqlite. it will actually create the database if it does not exists.

             QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
             db.setDatabaseName("c:\\test.db"); // make sure PATH is valid! c:\ cannot be used on win 7+
                if (!db.open()) {
                    QMessageBox::critical(0, qApp->tr("Cannot open database"), "Click Cancel to exit.",
                                          QMessageBox::Cancel);
                    return false;
                }
            
            1 Reply Last reply
            3
            • JonBJ JonB

              @zhmh
              No, there is no special, individual function call to create a database from Qt, whatever the underlying data provider. But doing it from code is not difficult. I already gave you a link with the few lines of code required for ODBC, there is no reason why you should not use it.

              zhmhZ Offline
              zhmhZ Offline
              zhmh
              wrote on last edited by
              #6

              @JonB This is because I'm looking for a simple way. I do not know syntax of code is on the link and how much time it takes to change the code and use it.. You also answered my previous question, but you did not know about it and just copied a link, so please do not confuse the questioner if you do not know the answer.

              jsulmJ JonBJ 2 Replies Last reply
              0
              • zhmhZ zhmh

                @JonB This is because I'm looking for a simple way. I do not know syntax of code is on the link and how much time it takes to change the code and use it.. You also answered my previous question, but you did not know about it and just copied a link, so please do not confuse the questioner if you do not know the answer.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @zhmh There is not always a simple way. For SQLite it is simple as @mrjj wrote. But for other databases there is no functionality built in Qt, so you will have to learn how to do it for your database and that is what @JonB tried to tell you.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                zhmhZ 1 Reply Last reply
                2
                • jsulmJ jsulm

                  @zhmh There is not always a simple way. For SQLite it is simple as @mrjj wrote. But for other databases there is no functionality built in Qt, so you will have to learn how to do it for your database and that is what @JonB tried to tell you.

                  zhmhZ Offline
                  zhmhZ Offline
                  zhmh
                  wrote on last edited by
                  #8

                  @jsulm I understand there is no functionality built in Qt,but how can I use this code ?

                  SQLWCHAR     strConnect[256] = L"Driver={SQL Server};Server=.\\MACHINE;Trusted_Connection=yes;";
                  SQLWCHAR     strConnectOut[1024] = { 0 };
                  SQLSMALLINT nNumOut = 0;
                  SQLRETURN nResult = SQLDriverConnect(handleDBC, NULL, (SQLWCHAR*)strConnect, SQL_NTS, (SQLWCHAR*)strConnectOut, sizeof(strConnectOut), &nNumOut, SQL_DRIVER_NOPROMPT);
                  if (!SQL_SUCCEEDED(nResult))
                     // some error handling
                  
                  SQLHSTMT    handleStatement = SQL_NULL_HSTMT;
                  nResult = SQLAllocHandle(SQL_HANDLE_STMT, handleDBC, (SQLHANDLE*)&handleStatement);
                  if (!SQL_SUCCEEDED(nResult))
                     // some error handling
                  
                  // Create a new database and use that
                  nResult = SQLExecDirect(handleStatement, L"CREATE DATABASE Foobar", SQL_NTS);
                  nResult = SQLExecDirect(handleStatement, L"USE Foobar", SQL_NTS);
                  
                  // create table Wallet in database Foobar
                  nResult = SQLExecDirect(handleStatement, L"CREATE TABLE Wallet (WalletID int NOT NULL,  Name nvarchar(5) NOT NULL)", SQL_NTS);
                  
                  jsulmJ 1 Reply Last reply
                  0
                  • zhmhZ zhmh

                    @jsulm I understand there is no functionality built in Qt,but how can I use this code ?

                    SQLWCHAR     strConnect[256] = L"Driver={SQL Server};Server=.\\MACHINE;Trusted_Connection=yes;";
                    SQLWCHAR     strConnectOut[1024] = { 0 };
                    SQLSMALLINT nNumOut = 0;
                    SQLRETURN nResult = SQLDriverConnect(handleDBC, NULL, (SQLWCHAR*)strConnect, SQL_NTS, (SQLWCHAR*)strConnectOut, sizeof(strConnectOut), &nNumOut, SQL_DRIVER_NOPROMPT);
                    if (!SQL_SUCCEEDED(nResult))
                       // some error handling
                    
                    SQLHSTMT    handleStatement = SQL_NULL_HSTMT;
                    nResult = SQLAllocHandle(SQL_HANDLE_STMT, handleDBC, (SQLHANDLE*)&handleStatement);
                    if (!SQL_SUCCEEDED(nResult))
                       // some error handling
                    
                    // Create a new database and use that
                    nResult = SQLExecDirect(handleStatement, L"CREATE DATABASE Foobar", SQL_NTS);
                    nResult = SQLExecDirect(handleStatement, L"USE Foobar", SQL_NTS);
                    
                    // create table Wallet in database Foobar
                    nResult = SQLExecDirect(handleStatement, L"CREATE TABLE Wallet (WalletID int NOT NULL,  Name nvarchar(5) NOT NULL)", SQL_NTS);
                    
                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @zhmh I'm not sure about this code. My guess is it is using Microsoft SQL Server client library. If you want to use it to create the database you will need to read its documentation.
                    An alternative would be to use the client application (call it via QProcess) to create the database, but this requires the client tools to be installed.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • zhmhZ zhmh

                      @JonB This is because I'm looking for a simple way. I do not know syntax of code is on the link and how much time it takes to change the code and use it.. You also answered my previous question, but you did not know about it and just copied a link, so please do not confuse the questioner if you do not know the answer.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @zhmh

                      but you did not know about it and just copied a link, so please do not confuse the questioner if you do not know the answer.

                      That's very kind of you. I know that as I said Qt has no special call for creating a database, and I know that the link I gave you showed & explained the principle of how you can create a new database across ODBC, even if you do not know how to adapt it.

                      I will obey your admonition and not suggest anything further for you.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved