mattn/go-sqlite3

Can i get result of PRAGMA statments? Such as PRAGMA integrity_check?

fy0 opened this issue · 4 comments

fy0 commented
Can i get result of PRAGMA statments? Such as PRAGMA integrity_check?

Yes, use Query or QueryRow as appropriate.

fy0 commented

Yes, use Query or QueryRow as appropriate.

Could you give me an example? I tried Query but cannot obtain a result like sqlite cli's output.

My Code:

	rst, err := db.Query("PRAGMA integrity_check")
	if err != nil {
		return
	}
	fmt.Println(rst.Columns())

Got the following result regardless of whether the database is corrupted or not.

[integrity_check] <nil>  

cli result:

>sqlite3 data.db "PRAGMA integrity_check"
*** in database main ***
Page 6: btreeInitPage() returns error code 11
Page 17: btreeInitPage() returns error code 11
Page 7: btreeInitPage() returns error code 11
On tree page 1808 cell 4: Rowid 1665 out of order
On tree page 1546 cell 323: Rowid 1647 out of order
On tree page 1779 cell 3: Rowid 1647 out of order
On tree page 1546 cell 321: Rowid 1629 out of order
On tree page 1786 cell 0: Rowid 1632 out of order
On tree page 1546 cell 320: Rowid 1632 out of order
On tree page 1791 cell 1: Rowid 1632 out of order
On tree page 1791 cell 0: Rowid 1632 out of order
On tree page 1546 cell 319: Rowid 1634 out of order
On tree page 1801 cell 0: Rowid 1634 out of order
On tree page 1546 cell 318: Rowid 1634 out of order
On tree page 1803 cell 0: Rowid 1634 out of order
On tree page 1546 cell 317: Rowid 1634 out of order
On tree page 1804 cell 1: Rowid 1634 out of order
On tree page 1804 cell 0: Rowid 1635 out of order
On tree page 1546 cell 316: Rowid 1635 out of order
On tree page 1812 cell 1: Rowid 1635 out of order
On tree page 1812 cell 0: Rowid 1640 out of order
....

You have to iterate through the result set by calling Next in a loop. For each row, you need to call Scan to read out the values. Something like:

rows, err := db.Query("PRAGMA integrity_check")
if err != nil {
    // ...
}
defer rows.Close()

for rows.Next() {
    var s string
    if err := rows.Scan(&s); err != nil {
        // ...
    }

    fmt.Println(s)
}

if err := rows.Err(); err != nil {
    // ...
}

This is the same as any other use of the Query method.

fy0 commented

thanks!