there is no data in the connection error. When you connect to a database, you can't create one if it doesn't exist?
-
The MYSQL plug-in is installed to connect to an existing database. But there is no data in the connection error. When you connect to a database, you can't create one if it doesn't exist?
@Jason-Xu said in there is no data in the connection error. When you connect to a database, you can't create one if it doesn't exist?:
But there is no data in the connection error
Can you please explain better?
What data? What error? If you get an error please post it here... -
When there is a Test database in the database, the application can connect normally. However, if a test database is not established in advance, the application will fail to connect。
There is no Student database in the database. The program failed to connect to the database
The Student database is included in the database. The program connects to the database successfully
Can't Qt create databases using mysql????,
If not how to create a new database? -
When there is a Test database in the database, the application can connect normally. However, if a test database is not established in advance, the application will fail to connect。
There is no Student database in the database. The program failed to connect to the database
The Student database is included in the database. The program connects to the database successfully
Can't Qt create databases using mysql????,
If not how to create a new database?@Jason-Xu
Your error message "Unknown database 'student'" was issued when there was indeed no such database, and so was correct. Once it was created it then connected, which is also correct.Can't Qt create databases using mysql????,
Of course! You saw that you created the database in mysql by asking it to execute the statement
CREATE DATABASE student
, so if you want to do that from Qt useQSqlQuery::exec()
to execute that statement. -
@JonB said in there is no data in the connection error. When you connect to a database, you can't create one if it doesn't exist?:
CREATE DATABASE student
qslDabe = QSqlDatabase::addDatabase("QMYSQL"); qslDabe.setHostName("localhost"); //本地数据库 qslDabe.setPort(3306);//端口号 qslDabe.setUserName("root"); //登录用户 qslDabe.setPassword("123456");//登录密码 qslDabe.setDatabaseName("student");//使用的数据库的table if(!qslDabe.open()) { qDebug()<<"连接数据库错误"<<qslDabe.lastError()<<endl; return ; } else { qDebug()<<"连接数据库成功"<<endl; } QSqlQuery query(qslDabe); query.exec("CREATE DATABASE student");
QslDabe connection failed, query.exec cannot execute. So when you first connect to the DATABASE, if the data does not exist, how do you CREATE the data using "query.exec("CREATE DATABASE Student ")" and then reconnect.
-
@JonB said in there is no data in the connection error. When you connect to a database, you can't create one if it doesn't exist?:
CREATE DATABASE student
qslDabe = QSqlDatabase::addDatabase("QMYSQL"); qslDabe.setHostName("localhost"); //本地数据库 qslDabe.setPort(3306);//端口号 qslDabe.setUserName("root"); //登录用户 qslDabe.setPassword("123456");//登录密码 qslDabe.setDatabaseName("student");//使用的数据库的table if(!qslDabe.open()) { qDebug()<<"连接数据库错误"<<qslDabe.lastError()<<endl; return ; } else { qDebug()<<"连接数据库成功"<<endl; } QSqlQuery query(qslDabe); query.exec("CREATE DATABASE student");
QslDabe connection failed, query.exec cannot execute. So when you first connect to the DATABASE, if the data does not exist, how do you CREATE the data using "query.exec("CREATE DATABASE Student ")" and then reconnect.
@Jason-Xu
Of course the attempt to connect specifyingstudent
as the database name will fail to open if the database does not exist. Look at the mysql command you used (mysql -u root -p
) when it did not exist and you intended to create it: that command did not specifystudent
as the database to connect to and nor should you from Qt.Depending on how mysql works, do not call
setDatabaseName()
, call it with maybe""
as the database name, or perhaps you need to pass in an existing system database, such assys
ormysql
. -
@JonB said in there is no data in the connection error. When you connect to a database, you can't create one if it doesn't exist?:
CREATE DATABASE student
qslDabe = QSqlDatabase::addDatabase("QMYSQL"); qslDabe.setHostName("localhost"); //本地数据库 qslDabe.setPort(3306);//端口号 qslDabe.setUserName("root"); //登录用户 qslDabe.setPassword("123456");//登录密码 qslDabe.setDatabaseName("student");//使用的数据库的table if(!qslDabe.open()) { qDebug()<<"连接数据库错误"<<qslDabe.lastError()<<endl; return ; } else { qDebug()<<"连接数据库成功"<<endl; } QSqlQuery query(qslDabe); query.exec("CREATE DATABASE student");
QslDabe connection failed, query.exec cannot execute. So when you first connect to the DATABASE, if the data does not exist, how do you CREATE the data using "query.exec("CREATE DATABASE Student ")" and then reconnect.
-
Thank you very much for your reply! It works very well!!
-
@Jason-Xu
Out of interest, for my future reference, did you find you could specify "no database" or did you specify perhapsmysql
for the database?@JonB said in there is no data in the connection error. When you connect to a database, you can't create one if it doesn't exist?:
Out of interest, for my future reference, did you find you could specify "no database" or did you specify perhaps mysql for the database?
"SetDatabaseName()" do not specify the database name, "qsldape.open ()" will execute successfully and then create the specified database
qslDabe = QSqlDatabase::addDatabase("QMYSQL"); qslDabe.setHostName("localhost"); //本地数据库 qslDabe.setPort(3306);//端口号 qslDabe.setUserName("root"); //登录用户 qslDabe.setPassword("123456");//登录密码 qslDabe.setDatabaseName("student"); if(!qslDabe.open()) { qDebug()<<"student 连接数据库错误"<<qslDabe.lastError()<<endl; qDebug()<<"开始连接默认数据库"<<endl; qslDabe.setDatabaseName(""); if(!qslDabe.open()) { qDebug()<<"默认数据库连接错误!"<<qslDabe.lastError()<<endl; return; } else { qDebug()<<"默认数据库连接成功"<<endl; } qDebug()<<"创建student数据库"<<endl; qslDabe.exec("CREATE DATABASE IF NOT EXISTS student"); qslDabe.close(); qslDabe.setDatabaseName("student"); if(!qslDabe.open()) { qDebug()<<"student数据库连接错误!"<<qslDabe.lastError()<<endl; return; } else { qDebug()<<"student数据库连接成功"<<endl; } } else { qDebug()<<"连接数据库成功"<<endl; }