0

Qt 6.2.0, Ubuntu 21.10. Usually MariaDb stores database under /var/lib/mysql/ directory.

I can connect to my own database using this working code:

_db = QSqlDatabase::addDatabase("QMYSQL", "machines");
_db.setHostName("localhost");
_db.setDatabaseName("mydb");
_db.setUserName("user");
_db.setPassword("password");

qDebug() << _db.open(); // true

I copied the database to another location:

$ sudo cp -r /var/lib/mysql/mydb /home/mark/mydir/

Changing the database name to:

_db.setDatabaseName("/home/mark/mydir/mydb");

leads to fail when open() is called. The error is:

QMYSQL: Unable to connect", "Unknown database 'home/mark/mydir/mydb'

What is the correct syntax do specify a different path?

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
Mark
  • 4,338
  • 7
  • 58
  • 120
  • there is no path to be entered the databse name is mydb – nbk Mar 05 '22 at 10:50
  • @nbk only if you want to use the default location – Mark Mar 05 '22 at 11:06
  • @Mark: No - If you want the database files lying on another directory then you have to specify it in the mysql config, not when you're doing a connection to a db (not matter if it is Qt or anything other) – chehrlic Mar 05 '22 at 11:46
  • in the database name is not possible to9 define the location, yyou can only change the my.cnf to redirect the location. try it pleas – nbk Mar 05 '22 at 13:21

1 Answers1

1

mysql does not locate database using path, you have to specify the database name instead

Yanis600
  • 71
  • 9