import os, sys
from PyQt4 import QtCore, QtGui, QtSql
def makeDB():
import sqlite3
db = sqlite3.connect("db.db")
db.execute("create table if not exists table1 (value text, data text)")
query = "insert into table1 (value, data) values (?, ?)"
valueSet = (("day","today"),("time","noon"),("food","cheese"))
for values in valueSet:
db.execute(query, values)
db.commit()
def __init__(self, model, parent = None):
super(TestApp, self).__init__(parent)
self.model = model
table.setModel(self.model)
table.setSortingEnabled(True)
table.setDropIndicatorShown(True)
table.setAcceptDrops(True)
table.setDragEnabled(True)
table.
setSelectionMode(QtGui.
QTableView.
SingleSelection) table.
setSelectionBehavior(QtGui.
QTableView.
SelectRows) '''table.horizontalHeader().setMovable(True)
table.horizontalHeader().setDragEnabled(True)
table.horizontalHeader().setDragDropMode(QtGui.QAbstractItemView.InternalMove)
table.verticalHeader().setMovable(True)
table.verticalHeader().setDragEnabled(True)
table.verticalHeader().setDragDropMode(QtGui.QAbstractItemView.InternalMove)
'''
layout.addWidget(table)
def __init__(self, parent = None):
super(myModel, self).__init__(parent)
self.setTable("table1")
self.select()
if __name__ == "__main__":
if not os.path.exists("db.db"):
makeDB()
myDb.setDatabaseName("db.db")
model = myModel()
dl = TestApp(model)
dl.exec_()
import os, sys
from PyQt4 import QtCore, QtGui, QtSql
def makeDB():
import sqlite3
db = sqlite3.connect("db.db")
db.execute("create table if not exists table1 (value text, data text)")
query = "insert into table1 (value, data) values (?, ?)"
valueSet = (("day","today"),("time","noon"),("food","cheese"))
for values in valueSet:
db.execute(query, values)
db.commit()
class TestApp(QtGui.QDialog):
def __init__(self, model, parent = None):
super(TestApp, self).__init__(parent)
self.model = model
table = QtGui.QTableView()
table.setModel(self.model)
table.setSortingEnabled(True)
table.setDropIndicatorShown(True)
table.setAcceptDrops(True)
table.setDragEnabled(True)
table.setSelectionMode(QtGui.QTableView.SingleSelection)
table.setSelectionBehavior(QtGui.QTableView.SelectRows)
table.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
'''table.horizontalHeader().setMovable(True)
table.horizontalHeader().setDragEnabled(True)
table.horizontalHeader().setDragDropMode(QtGui.QAbstractItemView.InternalMove)
table.verticalHeader().setMovable(True)
table.verticalHeader().setDragEnabled(True)
table.verticalHeader().setDragDropMode(QtGui.QAbstractItemView.InternalMove)
'''
layout = QtGui.QVBoxLayout(self)
layout.addWidget(table)
class myModel(QtSql.QSqlTableModel):
def __init__(self, parent = None):
super(myModel, self).__init__(parent)
self.setEditStrategy(QtSql.QSqlTableModel.OnFieldChange)
self.setTable("table1")
self.select()
if __name__ == "__main__":
if not os.path.exists("db.db"):
makeDB()
myDb = QtSql.QSqlDatabase.addDatabase("QSQLITE")
myDb.setDatabaseName("db.db")
model = myModel()
app = QtGui.QApplication(sys.argv)
dl = TestApp(model)
dl.exec_()
To copy to clipboard, switch view to plain text mode
Bookmarks