How to create a new DataBase in SOL Server
-
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?
-
@zhmh
To create a new database you need to Google for something likeodbc 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 issuesCREATE DATABASE
statements across ODBC, you need to do similar. -
@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. -
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; }
-
@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.
-
-
@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);
-
@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. -
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.