重要事项(2016-10-31日):sqliteBD库已经升级至Swift3,本文使用样例也已同步更新。
操作步骤:
2,创建连接头文件Bridging-Header.h
1
2
|
import
<time.h>
|
5,ViewController.swift代码
import
UIKit
class
ViewController
:
UIViewController
{
@IBOutlet
txtUname:
UITextField
!
txtMobile:
UITextField
!
override
func
viewDidLoad() {
super
@H_461_301@
.viewDidLoad()
db =
.shared
//打开数据库
_ = db.openDB()
//如果表还不存在则创建表(其中uid为自增主键)
let
result = db.execute(sql:
"create table if not exists t_user(uid integer primary key,uname varchar(20),mobile varchar(20))"
)
print
(result)
//如果有数据则加载
initUser()
}
//点击保存
@IBAction
saveClicked(_ sender:
AnyObject
) {
saveUser()
}
//从sqlite加载数据
initUser() {
if
data.count > 0 {
user = data[data.count - 1]
txtUname.text = user[
"uname"
]
as
?
String
txtMobile.text = user[
"mobile"
String
}
}
//保存数据到sqlite
saveUser() {
uname =
self
.txtUname.text!
mobile =
.txtMobile.text!
sql =
"insert into t_user(uname,mobile) values('\(uname)','\(mobile)')"
(result)
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}
|
老代码下载(只支持Swift1.2):MySQLite.zip
老代码下载(官方支持Swift2): @H_676_502@