Please use [code] tags around code.
But when immediately after this i'm executing:
bool ok = query.exec("create table user20(Id integer,Name varchar(100))");
QSqlQuery query;
bool ok = query.exec("create table user20(Id integer,Name varchar(100))");
To copy to clipboard, switch view to plain text mode
In your earlier code you are executing this after redefining the default connection to point at your Sqlite database. The SQL will fail if the table already exists, and QSqlQuery::lastError() will tell you everything there is to know about why.
More importantly, your original code connects a MSSQL database as the default connection and then replaces that default connection with the Sqlite database. Your MSSQL database will not be accessible after this and you will have received a runtime warning message to this effect. You need to look at the connection name parameter of QSqlDatabase::addDatabase().
This:
bool ok;
a.setDatabaseName("test1.db");
ok = a.open();
qDebug() << "A open" << ok;
b.setDatabaseName("test2.db");
ok = b.open();
qDebug() << "B open" << ok;
qDebug() << "But look" << a.isOpen() << b.isOpen();
bool ok;
QSqlDatabase a = QSqlDatabase::addDatabase("QSQLITE");
a.setDatabaseName("test1.db");
ok = a.open();
qDebug() << "A open" << ok;
QSqlDatabase b = QSqlDatabase::addDatabase("QSQLITE");
b.setDatabaseName("test2.db");
ok = b.open();
qDebug() << "B open" << ok;
qDebug() << "But look" << a.isOpen() << b.isOpen();
To copy to clipboard, switch view to plain text mode
produces this:
A open true
QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
B open true
But look false true
A open true
QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
B open true
But look false true
To copy to clipboard, switch view to plain text mode
Bookmarks