Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7619533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:42:38+00:00 2026-05-31T03:42:38+00:00

I am creating a PyQt application that stores its data in a MySQL database.

  • 0

I am creating a PyQt application that stores its data in a MySQL database. I want my application to be able to auto-create the database on first run. However, I couldn’t find any function to connect to the MySQL Server without specifying a database name.

I know that MySQL Server provides the functionality to connect to it without specifying a database name, as per the MySQL documentation:

For mysql, the first nonoption argument is taken as the name of the default database. If there is no such option, mysql does not select a default database.

How do I do this from my PyQt application?

Additionally, some online resources have suggested connecting to the MySQL server by specifying the names of one of the default databases created by MySQL. How do I create a new database in such a case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:42:40+00:00Added an answer on May 31, 2026 at 3:42 am

    Looking at Density 21.5‘s answer, I realized that executing these commands is also possible within PyQt. You are not bound to the same database with which you open a QSqlDatabase connection; preempting the need to close & reopen the connection.

    Hence, the simplest way to solve the problem would be:

    from PyQt4.QtSql import *
    
    db = QSqlDatabase.addDatabase("QMYSQL")
    db.setHostName("localhost")
    db.setPort(3306)
    db.setDatabaseName("mysql")
    db.setUserName("root")
    db.setPassword("")
    db.open()
    
    query = QSqlQuery()
    query.exec_("""CREATE DATABASE IF NOT EXISTS newdb;""")
    query.exec_("""USE newdb;""")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Creating My Windows Form Application and using ADO.Net as Data Access layer and SQL
I am creating a small PyQt application and got stuck up in MouseOver effect.
Creating a relatively simple data entry form, and just want to separate certain sections
Creating a client application, want the whole DB to be embed in the software
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
Creating a simple RPG game, first time using XNA. Trying to get my character
When creating applications (Java, run on a normal computer). How important is program size
I'm writing a PyQt (Python bindings for the all-powerful Qt library) application and a
Creating a calculator-like dialog, I noticed that quickly clicking on a button in IE
Creating an installer for possible remote systems so that if they do not have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.