[英]add json-based data into a sqlite database using swiftyJSON and sqlite.swift
[英]Inserting initial data after database creation in SQLite.swift
我有一個要填充一些初始數據的SQLite數據庫。
使用SQLite.swift ,此方法似乎有效:
do {
let _ = try db.run( userDictionary.create(ifNotExists: false) {t in
t.column(wordId, primaryKey: true)
t.column(word, unique: true)
t.column(frequency, defaultValue: 1)
t.column(following, defaultValue: "")
})
} catch _ {
print("table already exists")
return
}
// Add some initial data for a new database
print("adding new data")
myMethodToInsertInitialData()
但是,這種方式的工作方式是我使用ifNotExists: false
來在初始數據庫創建后每次都拋出錯誤。 (將其設置為true
不會引發錯誤(或允許提早返回)。)但是,每次都有意拋出錯誤(第一次除外)似乎編程不好。 我不是真的把它當作錯誤。 我只想在新創建的數據庫中插入一些數據。 有沒有更好的方法可以做到這一點,或者這是每個人都在做的事情嗎?
檢查SQLite.swift中表中是否有數據
do
{
db = try Connection(YOUR_DB_PATH)
let intCount : Int64 = db.scalar("SELECT count(*) FROM yourTableName") as! Int64
if (intCount == 0)
{
//callWebServiceCall()
}
else
{
//getDataFromDB()
}
}
catch
{
print("error")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.