Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
let _dbfile:NSString = "sqlite.db"
let _dir:AnyObject = NSSearchPathForDirectoriesInDomains(
NSSearchPathDirectory.DocumentDirectory,
NSSearchPathDomainMask.UserDomainMask,
true)[0]
let fileManager:NSFileManager = NSFileManager.defaultManager()
let _path:String = _dir.stringByAppendingPathComponent(_dbfile)
println(_path)
if(!fileManager.fileExistsAtPath(_path)){
//ファイルがない場合はDBファイル作成(最初の押下)
let _db = FMDatabase(path: _path)
let _sql = "CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT);"
_db.open()
var _result = _db.executeStatements(_sql)
println(_result)
_db.close()
}else{
//ファイルがすでに存在する場合(2回目以降の押下)
let _db = FMDatabase(path: _path)
let _sql_insert = "insert into test (title) values (?);"
let _sql_select = "SELECT title FROM test WHERE id = ?"
_db.open()
var _result_insert = _db.executeUpdate(_sql_insert, withArgumentsInArray: ["あいうえお"])
var _rows = _db.executeQuery(_sql_select, withArgumentsInArray: [1])
while(_rows.next()){
var _title = _rows.stringForColumn("title")
println(_title)
}
_db.close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.