創(chuàng)建數(shù)據(jù)庫:
一:接下來如果該數(shù)據(jù)庫不存在需要創(chuàng)建這個數(shù)據(jù)庫收奔,創(chuàng)建的過程寫在viewDidLoad里面:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 一:創(chuàng)建數(shù)據(jù)庫
NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSLog(@"dirpaths = %@",dirPaths);
docsDir = [dirPaths objectAtIndex:0];
// NSLog(@"docsDir = %@",docsDir);
databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"info.db"]];
// NSLog(@"databasePath = %@",databasePath);
NSFileManager *filemanager = [NSFileManager defaultManager];
if ([filemanager fileExistsAtPath:databasePath] == NO) {
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &dataBase)==SQLITE_OK) {
char *errmsg;
const char *createsql = "CREATE TABLE IF NOT EXISTS INFO (ID INTEGER PRIMARY KEY AUTOINCREMENT, NUM TEXT, CLASSNAME TEXT,NAME TEXT)";
if (sqlite3_exec(dataBase, createsql, NULL, NULL, &errmsg)!=SQLITE_OK) {
status.text = @"create table failed.";
}
}
else {
status.text = @"create/open failed.";
}
}
}
因為SQLite數(shù)據(jù)庫是文件數(shù)據(jù)庫烙无,是保存在文件系統(tǒng)中的,ios下:
Documents:應用中用戶數(shù)據(jù)可以放在這里详拙,iTunes備份和恢復的時候會包括此目錄
tmp:存放臨時文件帝际,iTunes不會備份和恢復此目錄,此目錄下文件可能會在應用退出后刪除
Library/Caches:存放緩存文件饶辙,iTunes不會備份此目錄蹲诀,此目錄下文件不會在應用退出刪除
我們的數(shù)據(jù)庫文件是保存在Documents下的。
切記弃揽,因為用的是C語法脯爪,sqlite3_open傳入的是database的地址!
二:保存信息:
//保存信息
- (IBAction)saveinfo:(id)sender {
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &dataBase)==SQLITE_OK) {
if ([num.text isEqualToString:@""]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"SORRY!" message:@"number cannot be nil!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
else {
NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO INFO (num,classname,name) VALUES(\"%@\",\"%@\",\"%@\")",num.text,classname.text,name.text];
const char *insertsatement = [insertSql UTF8String];
sqlite3_prepare_v2(dataBase, insertsatement, -1, &statement, NULL);
if (sqlite3_step(statement)==SQLITE_DONE) {
status.text = @"save to DB.";
num.text = @"";
classname.text = @"";
name.text = @"";
}
else {
status.text = @"save failed!";
}
sqlite3_finalize(statement);
sqlite3_close(dataBase);
}
}
}
三:在往數(shù)據(jù)庫里面插入數(shù)據(jù)的時候矿微,我們需要先打開數(shù)據(jù)庫痕慢,然后執(zhí)行插入語句,結束的時候切記要關閉數(shù)據(jù)庫涌矢!
// 查詢信息
- (IBAction)searchResult:(id)sender {
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &dataBase)==SQLITE_OK) {
NSString *querySQL = [NSString stringWithFormat:@"SELECT classname,name from info where num=\"%@\"",num.text];
const char *querystatement = [querySQL UTF8String];
if (sqlite3_prepare_v2(dataBase, querystatement, -1, &statement, NULL)==SQLITE_OK) {
if (sqlite3_step(statement)==SQLITE_ROW) {
NSString *classnameField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];
classname.text = classnameField;
NSString *nameField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
name.text = nameField;
status.text = @"find~~~";
}
else {
status.text = @"did not find you need.";
}
sqlite3_finalize(statement);
}
sqlite3_close(dataBase);
}
}
四:查詢操作同樣也是需要先打開數(shù)據(jù)庫掖举,再查詢,最后關閉數(shù)據(jù)庫娜庇,在這里就指定了根據(jù)學號來查詢塔次,其他情況未涉及。
可以使用Navicat軟件打開數(shù)據(jù)包來查看數(shù)據(jù)
五:在本例中還涉及一個觸摸屏幕來關閉鍵盤:
在viewcontroller.h中添加申明代碼:
//點擊屏幕收起鍵盤
- (IBAction)backgroundTap:(id)sender {
[num resignFirstResponder];
[classname resignFirstResponder];
[name resignFirstResponder];
}