can not connect to mysql by QSqlDatabase

Hi all
I tried to connect to MySQL database by QSqlDatabase but it failed. Script still running but result not as expected.
I don't know what wrong I did.

q0sql = QSqlDatabase.addDatabase("QMYSQL ")
#q0sql.driverName = "QMYSQ"
q0sql.setHostName("myIP")
q0sql.setDatabaseName("myDB")
q0sql.setUserName("myuser")
q0sql.setPassword("mypassword")
q0sql.setPort(3306)
print(q0sql.port) #result:-1
print(q0sql.isValid())# result False
ok = q0sql.open()

if ok:print("error")
if ok:print("connected")
else: print("disconnect")
q0sql.close()

thank for your helping.
daipv

Comments

  • Hi dai,

    I'm sorry, but I don't have experience with QSqlDatabase myself. First thing is that you should be sure you understand and know how to use the Qt API (i.e. using PyQt or C++). If you know that it's basically working, you can start porting that to KLayout's Qt binding.

    Matthias

  • thanks Matthias

  • the method proposed in stackflow looks working

    import pya
    #q0sql = pya.QSqlDatabase.addDatabase("QMYSQL ")
    q0sql = pya.QSqlDatabase.addDatabase('QODBC')
    
    #USERDSN is the name of system data sources defined in ODBC Data Source Administrator
    q0sql.setDatabaseName('USERDSN') 
    print(q0sql.port) #result:-1
    print(q0sql.isValid())# result True
    ok = q0sql.open()
    
    if ok:print("error") # result error
    if ok:print("connected") # result connected
    else: print("disconnect")
    q0sql.close()
    
  • @tiboy Very good! Thanks for sharing this and this feedback :)

    Kind regards,

    Matthias

Sign In or Register to comment.