from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtSql import *
def __init__(self, db_file, mode):
super().__init__()
self.db.setDatabaseName(db_file)
self.setTable("VOCABULARY")
self.select()
if mode == "create":
self.create_db()
elif mode == "load":
self.load_db()
def create_db(self):
sql_create_vocab = '''CREATE TABLE VOCABULARY
([generated_id] INTEGER PRIMARY KEY,
[word] TEXT NOT NULL,
[translation] TEXT NOT NULL,
[pos] TEXT NOT NULL,
[example_sentence] TEXT NOT NULL,
[example_translation] TEXT NOT NULL,
[description] TEXT NOT NULL,
[related_words] TEXT NOT NULL,
[related_image] BLOB NOT NULL)'''
query.prepare(sql_create_vocab)
query.exec()
self.setQuery(query)
return True
def load_db(self):
if self.db.open():
log.debug('MODEL: connect to SQL Server successfully')
# return True
else:
log.error('MODEL: connection failed')
# return False
log.info('MODEL: Processing Query')
qry.prepare('SELECT * FROM VOCABULARY')
qry.exec()
self.setQuery(qry)
def save_data(self):
print("BEFORE " + self.record(0).value("related_words"))
try:
self.setData(self.index(0, 7), "XXXXX")
self.submitAll()
print("SUCCESS")
except:
print("FAIL")
print("AFTER " + self.record(0).value("related_words"))
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtSql import *
class model(QSqlTableModel):
def __init__(self, db_file, mode):
super().__init__()
self.db = QSqlDatabase.addDatabase('QSQLITE')
self.db.setDatabaseName(db_file)
self.setEditStrategy(QSqlRelationalTableModel.OnFieldChange)
self.setTable("VOCABULARY")
self.select()
if mode == "create":
self.create_db()
elif mode == "load":
self.load_db()
def create_db(self):
sql_create_vocab = '''CREATE TABLE VOCABULARY
([generated_id] INTEGER PRIMARY KEY,
[word] TEXT NOT NULL,
[translation] TEXT NOT NULL,
[pos] TEXT NOT NULL,
[example_sentence] TEXT NOT NULL,
[example_translation] TEXT NOT NULL,
[description] TEXT NOT NULL,
[related_words] TEXT NOT NULL,
[related_image] BLOB NOT NULL)'''
query = QSqlQuery(self.db)
query.prepare(sql_create_vocab)
query.exec()
self.setQuery(query)
return True
def load_db(self):
if self.db.open():
log.debug('MODEL: connect to SQL Server successfully')
# return True
else:
log.error('MODEL: connection failed')
# return False
qry = QSqlQuery(self.db)
log.info('MODEL: Processing Query')
qry.prepare('SELECT * FROM VOCABULARY')
qry.exec()
self.setQuery(qry)
def save_data(self):
print("BEFORE " + self.record(0).value("related_words"))
try:
self.setData(self.index(0, 7), "XXXXX")
self.submitAll()
print("SUCCESS")
except:
print("FAIL")
print("AFTER " + self.record(0).value("related_words"))
To copy to clipboard, switch view to plain text mode
Bookmarks