本文整理汇总了Python中PyQt4.QtSql.QSqlDatabase.close方法的典型用法代码示例。如果您正苦于以下问题:Python QSqlDatabase.close方法的具体用法?Python QSqlDatabase.close怎么用?Python QSqlDatabase.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt4.QtSql.QSqlDatabase
的用法示例。
在下文中一共展示了QSqlDatabase.close方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: makeValueRelationDict
# 需要导入模块: from PyQt4.QtSql import QSqlDatabase [as 别名]
# 或者: from PyQt4.QtSql.QSqlDatabase import close [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: createDatabase
# 需要导入模块: from PyQt4.QtSql import QSqlDatabase [as 别名]
# 或者: from PyQt4.QtSql.QSqlDatabase import close [as 别名]
def createDatabase(self):
"""use a temp file name. When done, rename to final file name,
thusly avoiding races if two processes want to build the same
database"""
tempName = '%s.new.%d' % (self.dbPath(), os.getpid())
self.setDatabaseName(tempName)
if not self.open():
logException('creating %s: %s' % (tempName, self.lastError()))
with Transaction(silent=True):
self.createTables()
self.__generateDbIdent()
QSqlDatabase.close(self)
newName = self.dbPath()
if os.path.exists(newName):
os.remove(tempName)
else:
os.rename(tempName, newName)
示例3: makeValueRelationDict
# 需要导入模块: from PyQt4.QtSql import QSqlDatabase [as 别名]
# 或者: from PyQt4.QtSql.QSqlDatabase import close [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
示例4: SpatialiteDb
# 需要导入模块: from PyQt4.QtSql import QSqlDatabase [as 别名]
# 或者: from PyQt4.QtSql.QSqlDatabase import close [as 别名]
class SpatialiteDb(AbstractDb):
def __init__(self):
'''
Constructor
'''
super(SpatialiteDb,self).__init__()
self.db = QSqlDatabase('QSQLITE')
self.gen = SqlGeneratorFactory().createSqlGenerator(True)
def getDatabaseName(self):
'''
Gets the database name
'''
return self.db.databaseName().split('.sqlite')[0].split('/')[-1]
def connectDatabase(self, conn = None):
'''
Connects to database
conn: Database name
'''
if conn is None:
self.connectDatabaseWithGui()
else:
self.db.setDatabaseName(conn)
def connectDatabaseWithGui(self):
'''
Connects to database using user interface dialog
'''
fd = QFileDialog()
filename = fd.getOpenFileName(caption=self.tr('Select a DSGTools Spatialite file'),filter=self.tr('Spatialite file databases (*.sqlite)'))
self.db.setDatabaseName(filename)
def listGeomClassesFromDatabase(self, primitiveFilter = []):
'''
Gets a list with geometry classes from database
'''
self.checkAndOpenDb()
classList = []
sql = self.gen.getTablesFromDatabase()
query = QSqlQuery(sql, self.db)
if not query.isActive():
self.db.close()
raise Exception(self.tr("Problem listing geom classes: ")+query.lastError().text())
while query.next():
tableName = str(query.value(0))
layerName = tableName
if tableName.split("_")[-1] == "p" or tableName.split("_")[-1] == "l" \
or tableName.split("_")[-1] == "a":
classList.append(layerName)
return classList
def listComplexClassesFromDatabase(self):
'''
Gets a list with complex classes from database
'''
self.checkAndOpenDb()
classList = []
sql = self.gen.getTablesFromDatabase()
query = QSqlQuery(sql, self.db)
if not query.isActive():
self.db.close()
raise Exception(self.tr("Problem listing complex classes: ")+query.lastError().text())
while query.next():
tableName = str(query.value(0))
layerName = tableName
tableSchema = layerName.split('_')[0]
if tableSchema == 'complexos':
classList.append(layerName)
return classList
def getStructureDict(self):
'''
Gets database structure according to the edgv version
'''
self.checkAndOpenDb()
classDict = dict()
sql = self.gen.getStructure(self.getDatabaseVersion())
query = QSqlQuery(sql, self.db)
if not query.isActive():
self.db.close()
raise Exception(self.tr("Problem getting database structure: ")+query.lastError().text())
while query.next():
className = str(query.value(0))
classSql = str(query.value(1))
if className.split('_')[0] == 'complexos' or className.split('_')[-1] in ['p','l','a']:
if className not in classDict.keys():
classDict[className]=dict()
classSql = classSql.split(className)[1]
sqlList = classSql.replace('(','').replace(')','').replace('\"','').replace('\'','').split(',')
for s in sqlList:
fieldName = str(s.strip().split(' ')[0])
classDict[className][fieldName]=fieldName
if 'GEOMETRY' in classDict[className].keys():
classDict[className]['GEOMETRY'] = 'geom'
if 'geometry' in classDict[className].keys():
classDict[className]['geometry'] = 'geom'
if 'OGC_FID' in classDict[className].keys():
#.........这里部分代码省略.........
示例5: PostgisDb
# 需要导入模块: from PyQt4.QtSql import QSqlDatabase [as 别名]
# 或者: from PyQt4.QtSql.QSqlDatabase import close [as 别名]
#.........这里部分代码省略.........
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
def getDbsFromServer(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))
return dbList
def checkSuperUser(self):
try:
self.checkAndOpenDb()
except:
return False
query = QSqlQuery(self.db)
if query.exec_(self.gen.isSuperUser(self.db.userName())):
query.next()
value = query.value(0)
return value
return False
def dropDatabase(self, candidateName):
try:
self.checkAndOpenDb()
except:
return
if self.checkSuperUser():
sql = self.gen.dropDatabase(candidateName)
query = QSqlQuery(self.db)
if not query.exec_(sql):
raise Exception(self.tr('Problem dropping database: ') + query.lastError().text())
def createResolvedDomainViews(self, createViewClause, fromClause):
try:
self.checkAndOpenDb()
except:
return
if self.checkSuperUser():
filename = self.getSqlViewFile()
if filename <> None:
file = codecs.open(filename, encoding='utf-8', mode="r")
sql = file.read()
sql = sql.replace('[VIEW]', createViewClause).replace('[FROM]', fromClause)
file.close()
commands = sql.split('#')
self.db.transaction()
query = QSqlQuery(self.db)
for command in commands:
if not query.exec_(command):
self.db.rollback()
self.db.close()
raise Exception(self.tr('Problem creating views: ') + str(query.lastError().text()))
self.db.commit()
self.db.close()
def getSqlViewFile(self):
try:
self.checkAndOpenDb()
except:
return
currentPath = os.path.dirname(__file__)
dbVersion = self.getDatabaseVersion()
file = None
if dbVersion == '2.1.3':
file = os.path.join(currentPath,'..','..','DbTools','PostGISTool', 'sqls', '213', 'views_213.sql')
if dbVersion == 'FTer_2a_Ed':
file = os.path.join(currentPath,'..','..','DbTools','PostGISTool', 'sqls', 'FTer_2a_Ed', 'views_edgvFter_2a_Ed.sql')
return file
示例6: __del__
# 需要导入模块: from PyQt4.QtSql import QSqlDatabase [as 别名]
# 或者: from PyQt4.QtSql.QSqlDatabase import close [as 别名]
def __del__(self):
"""really free the handle"""
QSqlDatabase.close(self)
if Debug.sql:
logDebug('closed DBHandle %s' % self.name)