Created
April 11, 2025 21:05
-
-
Save ccgus/953d8b59e81d339a6e2762e0a23371f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SQLite3 | |
import Foundation | |
func main(dbPath: String) { | |
// Pointer for database connection | |
var db: OpaquePointer? | |
// Open database | |
guard sqlite3_open(dbPath, &db) == SQLITE_OK else { | |
print("Error opening database: \(String(cString: sqlite3_errmsg(db)))") | |
return | |
} | |
defer { sqlite3_close(db) } | |
// Prepare query | |
let query = "SELECT value FROM image_attributes WHERE name = 'composite'" | |
var statement: OpaquePointer? | |
guard sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK else { | |
print("Error preparing query: \(String(cString: sqlite3_errmsg(db)))") | |
return | |
} | |
defer { sqlite3_finalize(statement) } | |
// Execute query and get result | |
if sqlite3_step(statement) == SQLITE_ROW { | |
// Get blob data | |
if let blob = sqlite3_column_blob(statement, 0) { | |
let blobLength = sqlite3_column_bytes(statement, 0) | |
// Convert blob to Data | |
let data = Data(bytes: blob, count: Int(blobLength)) | |
// Write to file | |
let outputPath = "/tmp/foo.png" | |
do { | |
try data.write(to: URL(fileURLWithPath: outputPath)) | |
print("Successfully wrote data to \(outputPath)") | |
} catch { | |
print("Error writing to file: \(error)") | |
} | |
} else { | |
print("No blob data found") | |
} | |
} else { | |
print("No results found or query error") | |
} | |
} | |
// Check for command-line argument | |
guard CommandLine.arguments.count > 1 else { | |
print("Usage: \(CommandLine.arguments[0]) <database_path>") | |
exit(1) | |
} | |
// Run with provided database path | |
main(dbPath: CommandLine.arguments[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment