元数据是有关数据库中数据的信息。 SQLite 中的元数据包含有关我们存储数据的表和列的信息。 SQL 语句影响的行数是元数据。 结果集中返回的行数和列数也是元数据。
可以使用PRAGMA命令获取 SQLite 中的元数据。 SQLite 对象可能具有属性,即元数据。 最后,我们还可以通过查询 SQLite 系统sqlite_master表来获取特定的元数据。
#!/usr/bin/rubyrequire 'sqlite3'begindb = SQLite3::Database.open "test.db"pst = db.prepare "SELECT * FROM Cars LIMIT 6"puts pst.columnsputs pst.typesputs pst.column_countrescue SQLite3::Exception => eputs "Exception occurred"puts eensurepst.close if pstdb.close if dbend
在上面的示例中,我们获得了预备语句的列名,列类型和列数。
puts pst.columnsputs pst.typesputs pst.column_count
这三种方法返回预备语句的列名,列类型和列数。
$ ./cols_fields.rbIdNamePriceINTEGERTEXTINT3
输出显示三个列名称:Id,Name和Price。 类型是INTEGER,TEXT和INT。
以下示例显示如何检索特定 SQL 命令产生的更改数量。
#!/usr/bin/rubyrequire 'sqlite3'begindb = SQLite3::Database.new ":memory:"db.execute "CREATE TABLE Friends(Id INTEGER PRIMARY KEY, Name TEXT)"db.execute "INSERT INTO Friends(Name) VALUES ('Tom')"db.execute "INSERT INTO Friends(Name) VALUES ('Rebecca')"db.execute "INSERT INTO Friends(Name) VALUES ('Jim')"db.execute "INSERT INTO Friends(Name) VALUES ('Robert')"db.execute "INSERT INTO Friends(Name) VALUES ('Julian')"db.execute "DELETE FROM Friends WHERE Id IN (3, 4, 5)"n = db.changesputs "There has been #{n} changes"rescue SQLite3::Exception => eputs "Exception occurred"puts eensuredb.close if dbend
我们在内存中创建一个Friends表。 在最后一个 SQL 命令中,我们删除三行。 我们使用changes方法来获取上一次 SQL 操作完成的更改数量。
db.execute "DELETE FROM Friends WHERE Id IN (3, 4, 5)"
在此 SQL 语句中,我们删除三行。
n = db.changesputs "There has been #{n} changes"
我们找出上一条 SQL 语句完成的更改数量。
$ ./changes.rbThere has been 3 changes
示例输出。
在下一个示例中,我们将找到有关Cars表的一些数据。
#!/usr/bin/rubyrequire 'sqlite3'begindb = SQLite3::Database.open "test.db"stm = db.prepare "PRAGMA table_info('Cars')"rs = stm.executers.each do |row|puts row.join "\s"endrescue SQLite3::Exception => eputs "Exception occurred"puts eensurestm.close if stmdb.close if dbend
在此示例中,我们发出PRAGMA table_info(tableName)命令以获取有关Cars表的一些元数据信息。
stm = db.prepare "PRAGMA table_info('Cars')"rs = stm.execute
PRAGMA table_info(Cars)命令为 Cars 表中的每一列返回一行。 结果集中的列包括列顺序号,列名称,数据类型,该列是否可以为NULL以及该列的默认值。
rs.each do |row|puts row.join "\s"end
我们遍历结果集并打印数据。
$ ./table_info.rb0 Id INTEGER 0 11 Name TEXT 0 02 Price INT 0 0
示例的输出。
接下来,我们将打印Cars表中的 5 行及其列名。
#!/usr/bin/rubyrequire 'sqlite3'begindb = SQLite3::Database.open "test.db"rows = db.execute2 "SELECT * FROM Cars LIMIT 5"rows.each do |row|puts "%3s %-8s %s" % [row[0], row[1], row[2]]endrescue SQLite3::Exception => eputs "Exception occurred"puts eensuredb.close if dbend
我们将Cars表的五行打印到控制台。 现在,我们也包括列的名称。 记录与列名对齐。
rows = db.execute2 "SELECT * FROM Cars LIMIT 5"
execute2方法执行给定的 SQL 语句。 返回的第一行是列的名称。
rows.each do |row|puts "%3s %-8s %s" % [row[0], row[1], row[2]]end
数据被检索,格式化并打印到终端。
$ ./column_names.rbId Name Price1 Audi 526422 Mercedes 571273 Skoda 90004 Volvo 290005 Bentley 350000
输出。
在与元数据有关的最后一个示例中,我们将列出test.db数据库中的所有表。
#!/usr/bin/rubyrequire 'sqlite3'begindb = SQLite3::Database.open "test.db"rows = db.execute <<SQLSELECT name FROM sqlite_masterWHERE type='table'ORDER BY name;"SQLrows.each do |row|puts rowendrescue SQLite3::Exception => eputs "Exception occurred"puts eensuredb.close if dbend
该代码示例将当前数据库中的所有可用表打印到终端。
rows = db.execute <<SQLSELECT name FROM sqlite_masterWHERE type='table'ORDER BY name;"SQL
表名称是从sqlite_master表中检索的。
$ ./list_tables.rbCarsFriendsImages
这些是我们系统上的表。
在 SQLite Ruby 教程的这一部分中,我们使用了数据库元数据。
