PDA

View Full Version : Extract data from db then pass it to qtreeview



nethanjavier
23rd July 2012, 07:08
Hi,
I'm totally a newbie. I have this project in mind. I want to search data from my database and if it finds it, it will display the selected data to my QTreeView. I have already a query that can connect to my Postgresql Database. Here is the code:

import sys
from PyQt4.QtSql import *

def createConnection():
db = QSqlDatabase.addDatabase("QPSQL");
db.setHostName("localhost")
db.setPort(5432)
db.setDatabaseName("db")
db.setUserName("username")
db.setPassword("password")
ok = db.open()

query = QSqlQuery()
query.exec_("select * from itemlist;")
return True

This is the code for connecting to my database. My table for example is composed of pkey,itemcode,description and Quantity. I have a QLineEdit in my Gui. In that QLineEdit,it will search the ItemCode from my table and when it finds an item, it will automatically send that item data to my QTreeView with its details. How can this be possible. I hope you could help me. Please.

Best Regards,
Nethan

wysota
23rd July 2012, 09:39
QSqlQuery::next(), QSqlQuery::value()

nethanjavier
23rd July 2012, 10:03
Hi wysota,

What do you mean by using private messages? I'm just explaining my part because I have no background in programming. Sorry if you felt like I'm just using it for my own good. Currently I'm using PyQt and it's hard to start. Hope you understand my situation. I also try my best to find solutions but when I don't have any of it, that's the time I go have to ask.
For QSqlQuery, I already google anything that i find. Others said i should use bindvalues. But I don't see enough examples so that I could understand how to bind my QLineEdit to a column in my table db. Thank you so much wysota.

Best Regards,
Nethan

Hi wysota,

What do you mean by using private messages? I'm just explaining my part because I have no background in programming. Sorry if you felt like I'm just using it for my own good. Currently I'm using PyQt and it's hard to start. Hope you understand my situation. I also try my best to find solutions but when I don't have any of it, that's the time I go have to ask.
For QSqlQuery, I already google anything that i find. Others said i should use bindvalues. But I don't see enough examples so that I could understand how to bind my QLineEdit to a column in my table db. Thank you so much wysota.

Best Regards,
Nethan

wysota
23rd July 2012, 15:33
What do you mean by using private messages? I'm just explaining my part because I have no background in programming. Sorry if you felt like I'm just using it for my own good. Currently I'm using PyQt and it's hard to start. Hope you understand my situation. I also try my best to find solutions but when I don't have any of it, that's the time I go have to ask.
You are referring to my signature. It's not about you, some people tend to send private messages instead of asking here.


Others said i should use bindvalues.
I don't know who told you that but essentially bindValues() is used for something different.


But I don't see enough examples so that I could understand how to bind my QLineEdit to a column in my table db. Thank you so much wysota.
All you need is in the docs I linked to in my previous post.

nethanjavier
24th July 2012, 09:38
Hi,

I've read so many documents but still can't figure out how to do it. Please help me wysota. I'm really a newbie in programming. What I have so far is a UI for it and convert it to python file. Then I have a connection python file. In my main.py file, this is the code:

from UI import Ui_MainWindow
from connection import createConnection

class Form(QtGui.QWidget):
def __init__(self,parent=None):
super(Form,self).__init__(parent)
self.ui = UI()
self.ui.setupUi(self)

def mymodel(self.model):
self.proxyModel.setSourceModel(model)
self.sourceView.setModel(model)


after that...I don't know the code. I don't know how to query to a specific column in my table and if it finds the value,it passes it to the treeview. Please help me.


Best Regards,
Nethan

wysota
24th July 2012, 11:45
Honestly I have no idea what you are trying to do. I see no relation between what you posted earlier and what you posted now.