aboutsummaryrefslogtreecommitdiffstats
path: root/examples/sql
diff options
context:
space:
mode:
authorVenugopal Shivashankar <Venugopal.Shivashankar@qt.io>2019-09-17 15:22:21 +0200
committerVenugopal Shivashankar <Venugopal.Shivashankar@qt.io>2019-09-20 06:54:55 +0200
commitf8d7efd45cb0e25a513248e3a9ce24eda3afb1bd (patch)
tree5d6cbcfa8a3a99dca6be71ae74b5a954be157429 /examples/sql
parent833e2e1dde7c2f4de281c177e42e737b6865cdde (diff)
Example: Replace date type with text for authors' birthdate
Apparently, SQLite does not have the 'date' type. Instead, 'text' type is recommended by its docs. Moreover, the python datetime object does not translate to an appropriate type in SQLite. So str() seems the right alternative for date at the moment. Change-Id: I246c0708e60469a0fb253d33bdf8ab506eece1c9 Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'examples/sql')
-rw-r--r--examples/sql/books/createdb.py13
1 files changed, 6 insertions, 7 deletions
diff --git a/examples/sql/books/createdb.py b/examples/sql/books/createdb.py
index d03060ad5..1c27abf25 100644
--- a/examples/sql/books/createdb.py
+++ b/examples/sql/books/createdb.py
@@ -39,8 +39,7 @@
#############################################################################
from PySide2.QtSql import QSqlDatabase, QSqlError, QSqlQuery
-from datetime import datetime
-
+from datetime import date
def add_book(q, title, year, authorId, genreId, rating):
q.addBindValue(title)
@@ -59,7 +58,7 @@ def add_genre(q, name):
def add_author(q, name, birthdate):
q.addBindValue(name)
- q.addBindValue(birthdate)
+ q.addBindValue(str(birthdate))
q.exec_()
return q.lastInsertId()
@@ -68,7 +67,7 @@ BOOKS_SQL = """
genre integer, year integer, rating integer)
"""
AUTHORS_SQL = """
- create table authors(id integer primary key, name varchar, birthdate date)
+ create table authors(id integer primary key, name varchar, birthdate text)
"""
GENRES_SQL = """
create table genres(id integer primary key, name varchar)
@@ -106,9 +105,9 @@ def init_db():
check(q.exec_, GENRES_SQL)
check(q.prepare, INSERT_AUTHOR_SQL)
- 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))
+ asimovId = add_author(q, "Isaac Asimov", date(1920, 2, 1))
+ greeneId = add_author(q, "Graham Greene", date(1904, 10, 2))
+ pratchettId = add_author(q, "Terry Pratchett", date(1948, 4, 28))
check(q.prepare,INSERT_GENRE_SQL)
sfiction = add_genre(q, "Science Fiction")