ここで経験豊富な開発者...しかしSQLite.swiftとややスウィフトに新しく、明確な初心者...シンプル店舗/ BLOBまたはデータの取得(多分クラスのNSKeyArchive)
私は私の手を取得しようとしていますiOSアプリケーションでSQLiteを使用しているので、基本的なことを確認するためのシンプルなアプリケーションを作成しました。データオブジェクト(クラスオブジェクト)を格納して取得するまで、すべてがうまくいっていました...
ここで私のコードです:
//**********************************************************************
//**
//** Create/Open database connection
//**
//**********************************************************************
let dbPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let db: Connection = try! Connection("\(dbPath)/stickis.sqlite3")
let dbTable = Table("testtbl")
let id = Expression<Int64>("id")
let boolfld = Expression<Bool>("boolfld")
let int64fld = Expression<Int64>("int64fld")
let stringfld = Expression<String>("stringfld")
let blobfld = Expression<SQLite.Blob?>("blobfld") // optional due to ?
//**********************************************************************
//*
//* Drop table if it exists
//*
//**********************************************************************
try! db.run(dbTable.drop(ifExists: true))
//**********************************************************************
//**
//** Create/Open Table
//**
//**********************************************************************
do
{
try db.run((dbTable.create(ifNotExists: true)
{
t in
t.column(id, primaryKey: true)
t.column(boolfld)
t.column(int64fld)
t.column(stringfld)
t.column(blobfld)
})
)
}
catch
{
print("Failed to create sticki table")
}
//**********************************************************************
//**
//** Add Record 1
//**
//**********************************************************************
do
{
let testblob: Blob = windowcontent()
let rowid = try db.run(dbTable.insert(boolfld <- true, int64fld <- 1963, stringfld <- "unknown", blobfld <- testblob))
print("inserted id: \(rowid)")
}
catch
{
print("insertion failed: \(error)")
}
//**********************************************************************
//**
//** Add Record 2
//**
//**********************************************************************
do
{
let rowid = try db.run(dbTable.insert(boolfld <- true, int64fld <- 1972, stringfld <- "David"))
print("inserted id: \(rowid)")
}
catch
{
print("insertion failed: \(error)")
}
//**********************************************************************
//**
//** Update Record 1
//**
//**********************************************************************
let rec2updt = dbTable.filter(id == 1)
do
{
if try db.run(rec2updt.update(stringfld <- "TJ")) > 0
{
print("updated to TJ")
}
else
{
print("record not found")
}
}
catch
{
print("update failed")
}
//**********************************************************************
//**
//** Query Particular Record using filter
//**
//**********************************************************************
let tjFilter = dbTable.filter(int64fld == 1964)
for dataRec in try! db.prepare(tjFilter)
{
print("id: \(dataRec[id]), stringfld: \(dataRec[stringfld])")
}
//**********************************************************************
//**
//** Query All Records
//**
//**********************************************************************
for dataRec in try! db.prepare(dbTable)
{
print("id: \(dataRec[id]), stringfld: \(dataRec[stringfld])")
}
//**********************************************************************
//**
//** Delete Records
//**
//**********************************************************************
try! db.run(dbTable.delete())
すべてがうまくFを働いていましたまたは私....私は「追加レコード1」のコメントブロックの下に次の行を追加してまで...
let testblob: Blob = windowcontent()
windowcontent()クラスである(または構造体かもしれない)私は、「アーカイブしたいこと"と保存する...私はそれを格納するためにBLOBを使用している理由は、格納されたオブジェクトの種類はいくつかの異なるクラスのことができます。
let testdata: Data = Data()
let testblob: Blob = testdata as Blob
と誰もがSQLite.Blobへ/からのデータを割り当て、取得の簡単な例を持っています
をBLOBにデータを変換することができないの予想されるエラーを取得:
は私もこれを試してみました彼らは私と共有するだろうか?
また、サイドトピックでは、一度すべての機能を実行すると、「基本」iOSサンプルコードを共有することができます。他の初心者が来るかもしれないと仮定すれば、それは人生を楽にすることができます。
FYI ...私は、「ブロブ= windowcontent()testblobを聞かせて」期待していませんでしたその行の構文をデバッグするのに巻き込まれないようにしてください。 – tj4shee
解決策の一部を見つけて.... Blob(バイト:[UInt8](xxx))を使用してデータをBLOBに変換します。 ..ここでxxxはDataオブジェクトです....今度はデータに変換する方法を理解するために... – tj4shee
Blobからデータへの変換は、Blob.bytesを使用して行われます。私は答えとして入力しているコードです。 – tj4shee