本文整理汇总了Python中PyQt4.QtSql.QSqlDatabase.setPort方法的典型用法代码示例。如果您正苦于以下问题:Python QSqlDatabase.setPort方法的具体用法?Python QSqlDatabase.setPort怎么用?Python QSqlDatabase.setPort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt4.QtSql.QSqlDatabase
的用法示例。
在下文中一共展示了QSqlDatabase.setPort方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: makeValueRelationDict
# 需要导入模块: from PyQt4.QtSql import QSqlDatabase [as 别名]
# 或者: from PyQt4.QtSql.QSqlDatabase import setPort [as 别名]
def makeValueRelationDict(self, valueDict):
ret = dict()
codes = valueDict['FilterExpression'].replace('code in (', '').replace(')','').split(',')
keyColumn = valueDict['Key']
valueColumn = valueDict['Value']
table = valueDict['Layer'][:-17]#removing the date-time characters
uri = QgsDataSourceURI(self.currLayer.dataProvider().dataSourceUri())
if uri.host() == '':
db = QSqlDatabase('QSQLITE')
db.setDatabaseName(uri.database())
else:
db = QSqlDatabase('QPSQL')
db.setHostName(uri.host())
db.setPort(int(uri.port()))
db.setDatabaseName(uri.database())
db.setUserName(uri.username())
db.setPassword(uri.password())
if not db.open():
db.close()
return ret
in_clause = ','.join(map(str, codes))
query = QSqlQuery('select code, code_name from dominios.%s where code in (%s)' % (table, in_clause), db)
while query.next():
code = str(query.value(0))
code_name = query.value(1)
ret[code_name] = code
db.close()
return ret
示例2: getEDGVDbsFromServer
# 需要导入模块: from PyQt4.QtSql import QSqlDatabase [as 别名]
# 或者: from PyQt4.QtSql.QSqlDatabase import setPort [as 别名]
def getEDGVDbsFromServer(self):
#Can only be used in postgres database.
try:
self.checkAndOpenDb()
except:
return []
query = QSqlQuery(self.gen.getDatabasesFromServer(),self.db)
dbList = []
while query.next():
dbList.append(query.value(0))
edvgDbList = []
for database in dbList:
db = None
db = QSqlDatabase("QPSQL")
db.setDatabaseName(database)
db.setHostName(self.db.hostName())
db.setPort(self.db.port())
db.setUserName(self.db.userName())
db.setPassword(self.db.password())
if not db.open():
QgsMessageLog.logMessage('DB :'+database+'| msg: '+db.lastError().databaseText(), "DSG Tools Plugin", QgsMessageLog.CRITICAL)
query2 = QSqlQuery(db)
if query2.exec_(self.gen.getEDGVVersion()):
while query2.next():
version = query2.value(0)
if version:
edvgDbList.append((database,version))
else:
QgsMessageLog.logMessage(self.tr('Problem accessing database: ') +database+'\n'+query2.lastError().text(), "DSG Tools Plugin", QgsMessageLog.CRITICAL)
return edvgDbList
示例3: getPostGISDatabase
# 需要导入模块: from PyQt4.QtSql import QSqlDatabase [as 别名]
# 或者: from PyQt4.QtSql.QSqlDatabase import setPort [as 别名]
def getPostGISDatabase(self, postGISConnection):
db = None
db = QSqlDatabase("QPSQL")
(database, host, port, user, password) = self.getPostGISConnectionParameters(postGISConnection)
db.setDatabaseName(database)
db.setHostName(host)
db.setPort(int(port))
db.setUserName(user)
db.setPassword(password)
return db
示例4: getDatabase
# 需要导入模块: from PyQt4.QtSql import QSqlDatabase [as 别名]
# 或者: from PyQt4.QtSql.QSqlDatabase import setPort [as 别名]
def getDatabase(self, database = 'postgres'):
(host, port, user, password) = self.getServerConfiguration(self.serversCombo.currentText())
db = QSqlDatabase("QPSQL")
db.setDatabaseName(database)
db.setHostName(host)
db.setPort(int(port))
db.setUserName(user)
db.setPassword(password)
if not db.open():
QgsMessageLog.logMessage(db.lastError().text(), "DSG Tools Plugin", QgsMessageLog.CRITICAL)
return db
示例5: dropDatabase
# 需要导入模块: from PyQt4.QtSql import QSqlDatabase [as 别名]
# 或者: from PyQt4.QtSql.QSqlDatabase import setPort [as 别名]
def dropDatabase(self,db):
host = db.hostName()
port = db.port()
user = db.userName()
password = db.password()
database = 'postgres'
pgDB = QSqlDatabase('QPSQL')
pgDB.setHostName(host)
pgDB.setPort(port)
pgDB.setUserName(user)
pgDB.setPassword(password)
pgDB.setDatabaseName(database)
if not pgDB.open():
return False
sql = self.gen.dropDatabase(db.databaseName())
query = QSqlQuery(pgDB)
return query.exec_(sql)
示例6: getDatabase
# 需要导入模块: from PyQt4.QtSql import QSqlDatabase [as 别名]
# 或者: from PyQt4.QtSql.QSqlDatabase import setPort [as 别名]
def getDatabase(self, database = 'postgres'):
(host, port, user, password) = self.getServerConfiguration(self.serversCombo.currentText())
db = QSqlDatabase("QPSQL")
db.setConnectOptions('connect_timeout=10')
db.setDatabaseName(database)
db.setHostName(host)
db.setPort(int(port))
db.setUserName(user)
if password == '':
conInfo = 'host='+host+' port='+port+' dbname='+database
self.setCredentials(db, conInfo, user)
else:
db.setPassword(password)
if not db.open():
QgsMessageLog.logMessage(db.lastError().text(), "DSG Tools Plugin", QgsMessageLog.CRITICAL)
return db
示例7: makeValueRelationDict
# 需要导入模块: from PyQt4.QtSql import QSqlDatabase [as 别名]
# 或者: from PyQt4.QtSql.QSqlDatabase import setPort [as 别名]
def makeValueRelationDict(self, valueDict):
"""
Gets the value relation dictionary. This is necessary for multi valued attributes.
"""
ret = dict()
codes = valueDict['FilterExpression'].replace('code in (', '').replace(')','').split(',')
in_clause = ','.join(map(str, codes))
keyColumn = valueDict['Key']
valueColumn = valueDict['Value']
table = valueDict['Layer'][:-17]#removing the date-time characters
uri = QgsDataSourceURI(self.currLayer.dataProvider().dataSourceUri())
if uri.host() == '':
db = QSqlDatabase('QSQLITE')
db.setDatabaseName(uri.database())
sql = 'select code, code_name from dominios_%s where code in (%s)' % (table, in_clause)
else:
db = QSqlDatabase('QPSQL')
db.setHostName(uri.host())
db.setPort(int(uri.port()))
db.setDatabaseName(uri.database())
db.setUserName(uri.username())
db.setPassword(uri.password())
sql = 'select code, code_name from dominios.%s where code in (%s)' % (table, in_clause)
if not db.open():
db.close()
return ret
query = QSqlQuery(sql, db)
if not query.isActive():
QMessageBox.critical(self.iface.mainWindow(), self.tr("Error!"), self.tr("Problem obtaining domain values: ")+query.lastError().text())
return ret
while query.next():
code = str(query.value(0))
code_name = query.value(1)
ret[code_name] = code
db.close()
return ret
示例8: dropDatabase
# 需要导入模块: from PyQt4.QtSql import QSqlDatabase [as 别名]
# 或者: from PyQt4.QtSql.QSqlDatabase import setPort [as 别名]
def dropDatabase(self,db):
"""
Drops the created database case a problem occurs during database creation
db: QSqlDatabase to be dropped
"""
host = db.hostName()
port = db.port()
user = db.userName()
password = db.password()
database = 'postgres'
pgDB = QSqlDatabase('QPSQL')
pgDB.setHostName(host)
pgDB.setPort(port)
pgDB.setUserName(user)
pgDB.setPassword(password)
pgDB.setDatabaseName(database)
if not pgDB.open():
return False
sql = self.gen.dropDatabase(db.databaseName())
query = QSqlQuery(pgDB)
return query.exec_(sql)
示例9: getConnection
# 需要导入模块: from PyQt4.QtSql import QSqlDatabase [as 别名]
# 或者: from PyQt4.QtSql.QSqlDatabase import setPort [as 别名]
def getConnection(self, conndb, connhost, connport, connuser, connpass):
"""
Get a QSqlDatabase for a specific set of connection parameters
"""
db = None
db = QSqlDatabase("QPSQL")
db.setDatabaseName(conndb)
db.setHostName(connhost)
db.setPort(int(connport))
db.setUserName(connuser)
if not connpass or connpass == '':
conInfo = 'host='+connhost+' port='+connport+' dbname='+conndb
self.setCredentials(db, conInfo, connuser)
else:
db.setPassword(connpass)
if not db.open():
QMessageBox.critical(self, self.tr("Critical!"), self.tr('Database connection problem: \n') + db.lastError().text())
return db
示例10: PostgisDb
# 需要导入模块: from PyQt4.QtSql import QSqlDatabase [as 别名]
# 或者: from PyQt4.QtSql.QSqlDatabase import setPort [as 别名]
class PostgisDb(AbstractDb):
def __init__(self):
super(PostgisDb,self).__init__()
self.db = QSqlDatabase('QPSQL')
self.gen = SqlGeneratorFactory().createSqlGenerator(False)
def getDatabaseName(self):
return self.db.databaseName()
def connectDatabase(self,conn=None):
if conn.split(':')[0] == 'PG':
connSplit = conn.split(' ')
parDict = dict()
for i in connSplit[1::]:
par = i.split('=')
parDict[par[0]]=par[1]
self.connectDatabaseWithParameters(parDict['host'], parDict['port'], parDict['dbname'], parDict['user'], parDict['password'])
else:
self.connectDatabaseWithQSettings(conn)
def connectDatabaseWithGui(self):
return None
def connectDatabaseWithParameters(self, host, port, database, user, password):
self.db.setHostName(host)
if type(port) != 'int':
self.db.setPort(int(port))
else:
self.db.setPort(port)
self.db.setDatabaseName(database)
self.db.setUserName(user)
if not password or password == '':
conInfo = 'host='+host+' port='+port+' dbname='+database
check = False
while not check:
try:
(success, user, password) = QgsCredentials.instance().get(conInfo, user, None)
if not success:
return
self.db.setPassword(password)
check = True
self.checkAndOpenDb()
QgsCredentials.instance().put(conInfo, user, password)
except:
pass
else:
self.db.setPassword(password)
def connectDatabaseWithQSettings(self, name):
(host, port, database, user, password) = self.getConnectionFromQSettings(name)
self.db.setHostName(host)
if type(port) != 'int':
self.db.setPort(int(port))
else:
self.db.setPort(port)
self.db.setDatabaseName(database)
self.db.setUserName(user)
if not password or password == '':
conInfo = 'host='+host+' port='+port+' dbname='+database
check = False
while not check:
try:
(success, user, password) = QgsCredentials.instance().get(conInfo, user, None)
if not success:
return
self.db.setPassword(password)
check = True
self.checkAndOpenDb()
QgsCredentials.instance().put(conInfo, user, password)
except:
pass
else:
self.db.setPassword(password)
def getDatabaseVersion(self):
try:
self.checkAndOpenDb()
except:
return '-1'
sqlVersion = self.gen.getEDGVVersion()
queryVersion = QSqlQuery(sqlVersion, self.db)
while queryVersion.next():
version = queryVersion.value(0)
return version
def listGeomClassesFromDatabase(self):
try:
self.checkAndOpenDb()
except:
return []
classList = []
sql = self.gen.getTablesFromDatabase()
query = QSqlQuery(sql, self.db)
while query.next():
tableSchema = query.value(0)
tableName = query.value(1)
layerName = tableSchema+'.'+tableName
if tableName.split("_")[-1] == "p" or tableName.split("_")[-1] == "l" \
or tableName.split("_")[-1] == "a":
#.........这里部分代码省略.........
示例11: ComplexWindow
# 需要导入模块: from PyQt4.QtSql import QSqlDatabase [as 别名]
# 或者: from PyQt4.QtSql.QSqlDatabase import setPort [as 别名]
class ComplexWindow(QtGui.QDockWidget, FORM_CLASS):
def __init__(self, iface, parent=None):
"""Constructor."""
super(ComplexWindow, self).__init__(parent)
# Set up the user interface from Designer.
# After setupUI you can access any designer object by doing
# self.<objectname>, and you can use autoconnect slots - see
# http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
# #widgets-and-dialogs-with-auto-connect
self.setupUi(self)
#self.enderecoLine.setText('186.228.51.52')
#self.portaLine.setText('2101'
self.iface = iface
QObject.connect(self.dbButton, SIGNAL(("clicked()")), self.getDataSources)
QObject.connect(self.dbCombo, SIGNAL("activated(int)"), self.updateComplexClass)
QObject.connect(self.complexCombo, SIGNAL("activated(int)"), self.loadAssociatedFeatures)
QObject.connect(self.iface, SIGNAL("newProjectCreated()"), self.clearDock)
self.db = None
self.databases = None
self.factory = SqlGeneratorFactory()
self.gen = None
def __del__(self):
if self.db:
self.db.close()
self.db = None
def clearDock(self):
self.treeWidget.clear()
self.dbCombo.clear()
self.complexCombo.clear()
def isSpatialiteDatabase(self, dbName):
(dataSourceUri, credentials) = self.databases[dbName]
if dataSourceUri.host() == "":
return True
return False
def getUserCredentials(self, lyr):
dataSourceUri = QgsDataSourceURI( lyr.dataProvider().dataSourceUri() )
if dataSourceUri.host() == "":
return (None, None)
connInfo = dataSourceUri.connectionInfo()
(success, user, passwd ) = QgsCredentials.instance().get( connInfo, None, None )
# Put the credentials back (for yourself and the provider), as QGIS removes it when you "get" it
if success:
QgsCredentials.instance().put( connInfo, user, passwd )
return (user, passwd)
def updateComplexClass(self):
if self.db:
self.db.close()
self.db = None
if self.dbCombo.currentIndex() == 0:
return
dbName = self.dbCombo.currentText()
#getting the sql generator
self.gen = self.factory.createSqlGenerator(self.isSpatialiteDatabase(dbName))
(dataSourceUri, credentials) = self.databases[dbName]
#verifying the connection type
if self.isSpatialiteDatabase(dbName):
self.db = QSqlDatabase("QSQLITE")
self.db.setDatabaseName(dbName)
else:
self.db = QSqlDatabase("QPSQL")
self.db.setDatabaseName(dbName)
self.db.setHostName(dataSourceUri.host())
self.db.setPort(int(dataSourceUri.port()))
self.db.setUserName(credentials[0])
self.db.setPassword(credentials[1])
if not self.db.open():
print self.db.lastError().text()
self.populateComboBox()
def populateComboBox(self):
#getting all complex tables
self.complexCombo.clear()
self.complexCombo.addItem(self.tr("select a complex class"))
dbName = self.dbCombo.currentText()
(dataSourceUri, credentials) = self.databases[dbName]
#getting all complex tables
query = QSqlQuery(self.gen.getComplexTablesFromDatabase(), self.db)
while query.next():
self.complexCombo.addItem(query.value(0))
def getDataSources(self):
self.dbCombo.clear()
self.dbCombo.addItem(self.tr("select a database"))
#.........这里部分代码省略.........
示例12: CreateInomDialog
# 需要导入模块: from PyQt4.QtSql import QSqlDatabase [as 别名]
# 或者: from PyQt4.QtSql.QSqlDatabase import setPort [as 别名]
#.........这里部分代码省略.........
self.comboBoxPostgis.setCurrentIndex(0)
def setCRS(self):
try:
self.epsg = self.findEPSG()
print self.epsg
if self.epsg == -1:
self.bar.pushMessage("", self.tr("Coordinate Reference System not set or invalid!"), level=QgsMessageBar.WARNING)
else:
self.crs = QgsCoordinateReferenceSystem(self.epsg, QgsCoordinateReferenceSystem.EpsgCrsId)
if self.isSpatialite:
self.spatialiteCrsEdit.setText(self.crs.description())
self.spatialiteCrsEdit.setReadOnly(True)
else:
self.postGISCrsEdit.setText(self.crs.description())
self.postGISCrsEdit.setReadOnly(True)
except:
pass
def loadDatabase(self):
self.closeDatabase()
if self.isSpatialite:
fd = QtGui.QFileDialog()
self.filename = fd.getOpenFileName(filter='*.sqlite')
if self.filename:
self.spatialiteFileEdit.setText(self.filename)
self.db = QSqlDatabase("QSQLITE")
self.db.setDatabaseName(self.filename)
else:
self.db = QSqlDatabase("QPSQL")
(database, host, port, user, password) = self.getPostGISConnectionParameters(self.comboBoxPostgis.currentText())
self.db.setDatabaseName(database)
self.db.setHostName(host)
self.db.setPort(int(port))
self.db.setUserName(user)
self.db.setPassword(password)
try:
if not self.db.open():
print self.db.lastError().text()
else:
self.dbLoaded = True
self.setCRS()
except:
pass
def getPostGISConnectionParameters(self, name):
settings = QSettings()
settings.beginGroup('PostgreSQL/connections/'+name)
database = settings.value('database')
host = settings.value('host')
port = settings.value('port')
user = settings.value('username')
password = settings.value('password')
settings.endGroup()
return (database, host, port, user, password)
def getPostGISConnections(self):
settings = QSettings()
settings.beginGroup('PostgreSQL/connections')
currentConnections = settings.childGroups()
settings.endGroup()
return currentConnections
def populatePostGISConnectionsCombo(self):
self.comboBoxPostgis.clear()
self.comboBoxPostgis.addItem(self.tr("Select Database"))