//在沙盒Document文件夹下,创建sqlit文件--- 下一步就是要在里面添加各种表了
func setupLiveDataBase(){
let doucumentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as NSString
let databasePath = doucumentPath.appendingPathComponent(liveDataBase)
DBQueue = try! DatabaseQueue(path: databasePath)
DBQueue.setupMemoryManagement(in: UIApplication.shared)
print("\(databasePath)")
DBPool = try! DatabasePool(path: databasePath) //document中创建了数据库,现在数据库里面没有一张表
DBPool.setupMemoryManagement(in: UIApplication.shared)
updateColumn()
}
//如果数据库没有axPersion这张表,就创建新的axPersion表在里面
func updateColumn() {
try! DBPool.write { db in
do{
if let _ = try String.fetchOne(db, "select sql from sqlite_master where tbl_name = 'axPersion' and type = 'table'") {
} else {
try db.execute(
"""
CREATE TABLE IF NOT EXISTS axPersion (
axPersionID integer PRIMARY KEY NOT NULL,
GlobalID text UNIQUE NOT NULL,
Name text UNIQUE NOT NULL,
Sex boolean NOT NULL DEFAULT(1),
Home text UNIQUE NOT NULL,
Age integer NOT NULL DEFAULT(0)
)
"""
)
}
}catch let error as DatabaseError {
print(error.description)
}
}
}
Swift--创建数据库文件以及创建表和字段
最新推荐文章于 2025-04-01 09:26:43 发布
本文介绍如何在Swift中使用SQLite进行数据库初始化,并详细解释了如何创建包含多个字段的axPersion表,包括主键、唯一约束和默认值设置。
3760

被折叠的 条评论
为什么被折叠?



