呼び出した後、次のコマンドを発行することにより、オブジェクトをsetTable
介してテーブルのフィールド数、フィールド名、およびレコード数を取得できます。QSqlQuery
SQL
model = QSqlTableModel(db=your_db)
model.setTable("your_table")
...
query = QSqlQuery("SELECT column_name FROM information_schema.columns WHERE table_name='your_table'")
query.next()
print query.numRowsAffected() # print the number of fields
query.previous()
while query.next():
print query.value(0).toString() # print the field name
テーブル「your_table」のフィールド数とフィールド名を出力します。
モデルにデータを入力せずに行数を取得するには、 COUNT
次のコマンドを使用できます。
query = QSqlQuery("SELECT COUNT(*) FROM your_table")
query.next()
print query.value(0).toString() # print the number of records
print model.rowCount() # print 0 as the model has not been populated yet
私はそれCOUNT(1)
よりもパフォーマンスが良いと思いましたCOUNT(*)
が、それは神話のようです(このSOの質問を参照してください)。