QSQLITE ATTACH DATABASE 挂起的原因可能是什么
What could be the reason why QSQLITE ATTACH DATABASE is hanging
我正在为一个非常罕见的失败而苦苦挣扎。我有一个程序 运行ning,它将配置数据库附加到生产数据库。我使用了 sqlite. It was working fine and I was glad with that solution. From one run to the other it stopped attaching. If I use in the environment of the Sqlviewer 中的 ATTACH DATABASE
命令,我可以打开第一个数据库和 运行 一个 SQL 语句,如 ATTACH DATABASE 'c:\DATEN\temp\clcconfig.db' as 'configdb'
,我看到了所有表。
随附的片段包括我使用的命令 运行ning。我在 Windows 10 1909
下使用 Pyqt 5.15.1 和 Python 3.7
我不知道这是怎么发生的。任何人都可以使用任何数据库测试随附的代码吗?
from PyQt5.QtSql import QSqlDatabase, QSqlQuery
import os
maindb = 'C:/DATEN/temp/Test20.db'
confdb = 'c:/DATEN/temp/clcconfig.db'
if os.path.exists(maindb):
print("maindb exists")
if os.path.exists(confdb):
print("confdb exists")
db = QSqlDatabase.addDatabase ("QSQLITE")
db.setDatabaseName (maindb)
if db.open ():
print ("Main DB open")
print ("DB driver :", db.drivers ())
print ("DB Names :", db.databaseName ())
print ("db Connections:", db.connectionNames ())
print ("db Tables :", db.tables ())
sql = "ATTACH DATABASE '%s' AS %s"%(confdb,'configdb')
qry = QSqlQuery (db)
qry.exec_ (sql)
print ("sqlexec fault:", qry.lastError ().text ())
print ("DB Names :", db.databaseName ())
print ("db Connections:", db.connectionNames ())
print ("db Tables :", db.tables ())
print ("DB Error :", db.lastError ().databaseText ())
print ("driver Error :", db.lastError ().driverText ())
tables() 方法只returns 主数据库sqlite_master
的表,所以不会获取附加表的名称。如果您想从附加的数据库中获取表格,那么您必须使用 SQL 查询:
import os
import sys
from PyQt5.QtSql import QSqlDatabase, QSqlQuery
maindb = "C:/DATEN/temp/Test20.db"
confdb = "C:/DATEN/temp/clcconfig.db"
if os.path.exists(maindb):
print("maindb exists")
if os.path.exists(confdb):
print("confdb exists")
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName(maindb)
if not db.open():
print(db.lastError().text())
sys.exit(-1)
print("Main DB open")
qry = QSqlQuery(db)
qry.prepare("ATTACH ? AS ?")
for path, name in ((confdb, "configdb"),):
qry.addBindValue(path)
qry.addBindValue(name)
if not qry.exec_():
print(qry.lastError().text())
sys.exit(-1)
databases = []
qry = QSqlQuery("PRAGMA database_list", db)
if not qry.exec_():
print(qry.lastError().text())
sys.exit(-1)
rec = qry.record()
i = rec.indexOf("name")
while qry.next():
databases.append(qry.value(i))
tables = []
for database in databases:
qry = QSqlQuery(
"SELECT name FROM %s.sqlite_master WHERE type='table'" % (database,), db
)
if not qry.exec_():
print(qry.lastError().text())
while qry.next():
tables.append(qry.value(0))
print(tables)
我正在为一个非常罕见的失败而苦苦挣扎。我有一个程序 运行ning,它将配置数据库附加到生产数据库。我使用了 sqlite. It was working fine and I was glad with that solution. From one run to the other it stopped attaching. If I use in the environment of the Sqlviewer 中的 ATTACH DATABASE
命令,我可以打开第一个数据库和 运行 一个 SQL 语句,如 ATTACH DATABASE 'c:\DATEN\temp\clcconfig.db' as 'configdb'
,我看到了所有表。
随附的片段包括我使用的命令 运行ning。我在 Windows 10 1909
我不知道这是怎么发生的。任何人都可以使用任何数据库测试随附的代码吗?
from PyQt5.QtSql import QSqlDatabase, QSqlQuery
import os
maindb = 'C:/DATEN/temp/Test20.db'
confdb = 'c:/DATEN/temp/clcconfig.db'
if os.path.exists(maindb):
print("maindb exists")
if os.path.exists(confdb):
print("confdb exists")
db = QSqlDatabase.addDatabase ("QSQLITE")
db.setDatabaseName (maindb)
if db.open ():
print ("Main DB open")
print ("DB driver :", db.drivers ())
print ("DB Names :", db.databaseName ())
print ("db Connections:", db.connectionNames ())
print ("db Tables :", db.tables ())
sql = "ATTACH DATABASE '%s' AS %s"%(confdb,'configdb')
qry = QSqlQuery (db)
qry.exec_ (sql)
print ("sqlexec fault:", qry.lastError ().text ())
print ("DB Names :", db.databaseName ())
print ("db Connections:", db.connectionNames ())
print ("db Tables :", db.tables ())
print ("DB Error :", db.lastError ().databaseText ())
print ("driver Error :", db.lastError ().driverText ())
tables() 方法只returns 主数据库sqlite_master
的表,所以不会获取附加表的名称。如果您想从附加的数据库中获取表格,那么您必须使用 SQL 查询:
import os
import sys
from PyQt5.QtSql import QSqlDatabase, QSqlQuery
maindb = "C:/DATEN/temp/Test20.db"
confdb = "C:/DATEN/temp/clcconfig.db"
if os.path.exists(maindb):
print("maindb exists")
if os.path.exists(confdb):
print("confdb exists")
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName(maindb)
if not db.open():
print(db.lastError().text())
sys.exit(-1)
print("Main DB open")
qry = QSqlQuery(db)
qry.prepare("ATTACH ? AS ?")
for path, name in ((confdb, "configdb"),):
qry.addBindValue(path)
qry.addBindValue(name)
if not qry.exec_():
print(qry.lastError().text())
sys.exit(-1)
databases = []
qry = QSqlQuery("PRAGMA database_list", db)
if not qry.exec_():
print(qry.lastError().text())
sys.exit(-1)
rec = qry.record()
i = rec.indexOf("name")
while qry.next():
databases.append(qry.value(i))
tables = []
for database in databases:
qry = QSqlQuery(
"SELECT name FROM %s.sqlite_master WHERE type='table'" % (database,), db
)
if not qry.exec_():
print(qry.lastError().text())
while qry.next():
tables.append(qry.value(0))
print(tables)