diff options
author | Venugopal Shivashankar <Venugopal.Shivashankar@qt.io> | 2019-05-22 16:02:53 +0200 |
---|---|---|
committer | Venugopal Shivashankar <Venugopal.Shivashankar@qt.io> | 2019-06-18 14:00:36 +0200 |
commit | ee226dff8334cea7c4970051fcd3ade392cd85b0 (patch) | |
tree | 0bf1ee367edd6aabaee6ee89b1ff7eb6955009bc | |
parent | c0f118e4106b5d38cd828425b6533147f111cdd4 (diff) |
Example: Move the SQL statements
Moving the SQL statements into separate variables
outside the scope of the 'init_db' function, improves
readability.
Change-Id: I716dfceef6dc343287afab17f74e8de7fae618c6
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
Reviewed-by: Christian Tismer <tismer@stackless.com>
-rw-r--r-- | examples/sql/books/bookwindow.py | 7 | ||||
-rw-r--r-- | examples/sql/books/createdb.py | 60 | ||||
-rw-r--r-- | sources/pyside2/doc/deployment.rst | 51 |
3 files changed, 61 insertions, 57 deletions
diff --git a/examples/sql/books/bookwindow.py b/examples/sql/books/bookwindow.py index 500acf2ef..c6d43b1f3 100644 --- a/examples/sql/books/bookwindow.py +++ b/examples/sql/books/bookwindow.py @@ -58,11 +58,8 @@ class BookWindow(QMainWindow, Ui_BookWindow): QMainWindow.__init__(self) self.setupUi(self) - #check for SQL errors - err = createdb.init_db() - if err.type() is not QSqlError.NoError: - showError(err) - return + #Initialize db + createdb.init_db() model = QSqlRelationalTableModel(self.bookTable) model.setEditStrategy(QSqlTableModel.OnManualSubmit) diff --git a/examples/sql/books/createdb.py b/examples/sql/books/createdb.py index d662cacd1..1ca52470f 100644 --- a/examples/sql/books/createdb.py +++ b/examples/sql/books/createdb.py @@ -63,43 +63,59 @@ def add_author(q, name, birthdate): q.exec_() return q.lastInsertId() +BOOKS_SQL = """ + create table books(id integer primary key, title varchar, author integer, + genre integer, year integer, rating integer) + """ +AUTHORS_SQL = """ + create table authors(id integer primary key, name varchar, birthdate date) + """ +GENRES_SQL = """ + create table genres(id integer primary key, name varchar) + """ +INSERT_AUTHOR_SQL = """ + insert into authors(name, birthdate) values(?, ?) + """ +INSERT_GENRE_SQL = """ + insert into genres(name) values(?) + """ +INSERT_BOOK_SQL = """ + insert into books(title, year, author, genre, rating) + values(?, ?, ?, ?, ?) + """ def init_db(): + """ + init_db() + Initializes the database. + If tables "books" and "authors" are already in the database, do nothing. + Return value: None or raises ValueError + The error value is the QtSql error instance. + """ + def check(func, *args): + if not func(*args): + raise ValueError(func.__self__.lastError()) db = QSqlDatabase.addDatabase("QSQLITE") db.setDatabaseName(":memory:") - if not db.open(): - return db.lastError() - - tables = db.tables() - for table in tables: - if table == "books" and table == "authors": - return QSqlError() + check(db.open) q = QSqlQuery() - if not q.exec_("create table books(id integer primary key, title varchar, author integer, " - "genre integer, year integer, rating integer)"): - return q.lastError() - if not q.exec_("create table authors(id integer primary key, name varchar, birthdate date)"): - return q.lastError() - if not q.exec_("create table genres(id integer primary key, name varchar)"): - return q.lastError() + check(q.exec_,BOOKS_SQL) + check(q.exec_,AUTHORS_SQL) + check(q.exec_,GENRES_SQL) + check(q.prepare,INSERT_AUTHOR_SQL) - if not q.prepare("insert into authors(name, birthdate) values(?, ?)"): - return q.lastError() asimovId = add_author(q, "Isaac Asimov", datetime(1920, 2, 1)) greeneId = add_author(q, "Graham Greene", datetime(1904, 10, 2)) pratchettId = add_author(q, "Terry Pratchett", datetime(1948, 4, 28)) - if not q.prepare("insert into genres(name) values(?)"): - return q.lastError() + check(q.prepare,INSERT_GENRE_SQL) sfiction = add_genre(q, "Science Fiction") fiction = add_genre(q, "Fiction") fantasy = add_genre(q, "Fantasy") - if not q.prepare("insert into books(title, year, author, genre, rating) " - "values(?, ?, ?, ?, ?)"): - return q.lastError() + check(q.prepare,INSERT_BOOK_SQL) add_book(q, "Foundation", 1951, asimovId, sfiction, 3) add_book(q, "Foundation and Empire", 1952, asimovId, sfiction, 4) add_book(q, "Second Foundation", 1953, asimovId, sfiction, 3) @@ -113,5 +129,3 @@ def init_db(): add_book(q, "Guards! Guards!", 1989, pratchettId, fantasy, 3) add_book(q, "Night Watch", 2002, pratchettId, fantasy, 3) add_book(q, "Going Postal", 2004, pratchettId, fantasy, 3) - - return QSqlError() diff --git a/sources/pyside2/doc/deployment.rst b/sources/pyside2/doc/deployment.rst index a125dc4d7..eb8d77b0d 100644 --- a/sources/pyside2/doc/deployment.rst +++ b/sources/pyside2/doc/deployment.rst @@ -6,23 +6,23 @@ Deploying or freezing an application is a crucial part of many Python projects. Most large projects are not based on a single Python file, so the distribution of these applications becomes more difficult. -The options for a project are: +Here are a few distribution options that you could use: 1. Sending a normal zip-file with the application's content. 2. Building a proper `Python package (wheel) <https://packaging.python.org/>`_. - 3. Freezing the application in a single binary file, or into a directory. + 3. Freezing the application into a single binary file or a directory. -For the **third** option, there are many available tools: +If you choose the **third** option, consider using one of these tools: * `fbs <https://build-system.fman.io/>`_, * `PyInstaller <https://www.pyinstaller.org/>`_, * `cx_Freeze <https://anthony-tuininga.github.io/cx_Freeze/>`_, * `py2exe <http://www.py2exe.org/>`_, * `py2app <https://py2app.readthedocs.io/en/latest/>`_, -Since |project| is a cross-platform framework, -we would like to focus on solutions that at least work on -the three major platform supported by Qt: Linux, macOS, and Windows. - -The following table summarizes the above mentioned tools support: +|project| is a cross-platform framework, +so we would like to focus on solutions that work on the three +major platforms supported by Qt: Linux, macOS, and Windows. +The following table summarizes the platform support for those packaging +tools: =========== ======= ===== ===== ======= Name License Linux macOS Windows @@ -34,34 +34,27 @@ py2exe MIT no no yes py2app MIT no yes no =========== ======= ===== ===== ======= -From the table we can see that only *fbs*, *cx_Freeze* and *PyInstaller* -meet our requirements. +According to this table, only *fbs*, *cx_Freeze*, and *PyInstaller* +meets our cross-platform requirement. -All tools are command-line based, and it could become -a hard task to include more resources to your application, such as -images, icons, and meta-information, because you will need to create -special hooks or separate scripts to handle them. -Additionally, since this only -allows you to freeze your current application, you don't have -any mechanism to update your application. +As these are command-line tools, it could be hard to include +resources to your application, such as images, icons, and +meta-information. This means, you will need special hooks +or scripts to handle them before adding to the package. +In addition to this, these tools does not offer a mechanism +to update your application packages. -To cover the update part, there is a tool built around PyInstaller -called `PyUpdater <https://www.pyupdater.org/>`_ which enables -a simple mechanism to ship applications updates. +To create update packages, use the `PyUpdater <https://www.pyupdater.org/>`_, +which is built around PyInstaller. -On top of all these features, including also a nice interface -that allows the user to install the application step by step, -or even better, provide templates to create new projects to easily -freeze-them-up is something really beneficial for both developers -and end-users. -This is where `fbs <https://build-system.fman.io>`_ enters the -game, being based on PyInstaller, but including all the nice features -we previously mentioned. +The `fbs <https://build-system.fman.io>`_ tool offers a nice UI +that allows the user to install the application step-by-step. Here you can find a set of tutorials on how to use the previously described tools. -.. note:: Deployment is possible only in Qt for Python 5.12.2 +.. note:: Deployment is supported only from Qt for Python 5.12.2 and +later. .. toctree:: :name: mastertoc |