首页 > 解决方案 > 如何在画布上将 sqlite3 查询结果打印为矩阵?

问题描述

我想将 sqlite3 查询的结果以矩阵的形式放置并打印在 tk 画布上。该查询从数据库中返回一个表的三列。有人可以帮忙吗?

标签: tcltk

解决方案


与其使用画布,不如使用 tktable。绘制线条和将项目格式化为列的所有工作都已完成。

#!/usr/bin/tclsh
# We need these otherwise it will not work
package require sqlite3
package require Tktable

# open database - in this case stockbox.db in the current directory
sqlite3 db stockbox.db

# Set up the titles
set models(0,0) "Model"
set models(0,1) "Width"
set models(0,2) "Length"

# Start with 1 for the title
set rows 1
set cols 3

# Perform query, extract result into models
db eval {SELECT model_id,width, length FROM tbl_model} {
    set models($rows,0) $model_id
    set models($rows,1) $width
    set models($rows,2) $length
    incr rows
}
db close

# Create the table
table .table -variable models -titlerows 1 -width $cols -height 10 -rows $rows -yscrollcommand ".ys set"
scrollbar .ys -command ".table yview"
pack .table .ys -side left -fill y

推荐阅读