from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtSql import *
import sys
def __init__(self):
super(MyDlg, self).__init__()
filename = "C:/temp/testfile.db"
db.setDatabaseName(filename)
if db.isOpen():
print "Database is already open by another process!"
db.open()
self.model = ListModel(db)
view.setModel(self.model)
layout.addWidget(view)
layout.addWidget(add_button)
self.setLayout(layout)
add_button.clicked.connect(self.addRow)
def addRow(self):
text, o
= QInputDialog.
getText(self,
"Enter a row to add",
"Text") self.
model.
insertRow(self.
model.
rowCount()+1,
QModelIndex(), text
)
def __init__(self, db):
super(ListModel, self).__init__()
self.db = db
count_query.prepare("SELECT COUNT(1) from names")
count_query.exec_()
count_query.next()
return count_query.value(0)
def data(self, index, role=Qt.DisplayRole):
if not index.isValid():
return None
if role == Qt.DisplayRole:
data_query.prepare("SELECT name FROM names WHERE rowid=?")
data_query.addBindValue(index.row() + 1)
data_query.exec_()
data_query.next()
return data_query.value(0)
def insertRow
(self, row, parent
=QModelIndex(), text
="Default"): insert_query.prepare("INSERT INTO names VALUES (?)")
insert_query.addBindValue(text)
if insert_query.exec_():
self.db.transaction()
self.db.commit()
self.dataChanged.emit(self.index(row, 0), self.index(row, 0))
return True
else:
print insert_query.lastError().databaseText()
return False
def main():
dlg = MyDlg()
dlg.show()
app.exec_()
main()
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtSql import *
import sys
class MyDlg(QDialog):
def __init__(self):
super(MyDlg, self).__init__()
db = QSqlDatabase.addDatabase("QSQLITE")
filename = "C:/temp/testfile.db"
db.setDatabaseName(filename)
if db.isOpen():
print "Database is already open by another process!"
db.open()
self.model = ListModel(db)
view = QListView()
view.setModel(self.model)
add_button = QPushButton("Insert row")
layout = QVBoxLayout()
layout.addWidget(view)
layout.addWidget(add_button)
self.setLayout(layout)
add_button.clicked.connect(self.addRow)
def addRow(self):
text, o = QInputDialog.getText(self, "Enter a row to add", "Text")
self.model.insertRow(self.model.rowCount()+1, QModelIndex(), text)
class ListModel(QAbstractListModel):
def __init__(self, db):
super(ListModel, self).__init__()
self.db = db
def rowCount(self, parent=QModelIndex()):
count_query = QSqlQuery()
count_query.prepare("SELECT COUNT(1) from names")
count_query.exec_()
count_query.next()
return count_query.value(0)
def data(self, index, role=Qt.DisplayRole):
if not index.isValid():
return None
if role == Qt.DisplayRole:
data_query = QSqlQuery()
data_query.prepare("SELECT name FROM names WHERE rowid=?")
data_query.addBindValue(index.row() + 1)
data_query.exec_()
data_query.next()
return data_query.value(0)
def insertRow(self, row, parent=QModelIndex(), text="Default"):
insert_query = QSqlQuery()
insert_query.prepare("INSERT INTO names VALUES (?)")
insert_query.addBindValue(text)
if insert_query.exec_():
self.db.transaction()
self.db.commit()
self.dataChanged.emit(self.index(row, 0), self.index(row, 0))
return True
else:
print insert_query.lastError().databaseText()
return False
def main():
app = QApplication(sys.argv)
dlg = MyDlg()
dlg.show()
app.exec_()
main()
To copy to clipboard, switch view to plain text mode
Bookmarks