ios sqlite3.0数据库的简单操作
下面是简单操作的代码,也是本人下载代码学习过后的积累,仅供参考,准备开发一个复杂一些的代码,全部都使用到。方便大家学习、研究。其实sqlite就是一个轻量级数据库,针对文件类型的数据库。
掌握数据库的关键还是对于sql语句的熟悉和使用。其实sqlite3.0也是对于sql语句的操作,只不过里面涉及到了一些ios操作数据库的语法而已。关键还是sql语句
.h
#import <UIKit/UIKit.h>
#import <sqlite3.h>
@interface ViewController : UIViewController
{
sqlite3 *contactDB;
UITextField *name;
UITextField *address;
UITextField *phone;
UILabel *status;
NSString *databasePath;
}
@property (retain,nonatomic)IBOutletUITextField *name;
@property (retain,nonatomic)IBOutletUITextField *address;
@property (retain,nonatomic)IBOutletUITextField *phone;
@property (retain,nonatomic)IBOutletUILabel *status;
.m
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
/*根据路径创建数据库并创建一个表contact(id nametext addresstext phonetext)*/
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
databasePath = [[NSStringalloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.db"]];
NSFileManager *filemgr = [NSFileManagerdefaultManager];
if ([filemgr fileExistsAtPath:databasePath] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB)==SQLITE_OK)
{
char *errMsg;
constchar *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT,PHONE TEXT)";
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg)!=SQLITE_OK) {
status.text = @"创建表失败\n";
}
}
else
{
status.text = @"创建/打开数据库失败";
}
}
}
- (IBAction)SaveToDataBase:(id)sender
{
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB)==SQLITE_OK) {
NSString *insertSQL = [NSStringstringWithFormat:@"INSERT INTO CONTACTS (name,address,phone) VALUES(\"%@\",\"%@\",\"%@\")",name.text,address.text,phone.text];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement)==SQLITE_DONE) {
status.text = @"已存储到数据库";
name.text = @"";
address.text = @"";
phone.text = @"";
}
else
{
status.text = @"保存失败";
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
}
- (IBAction)SearchFromDataBase:(id)sender
{
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *querySQL = [NSStringstringWithFormat:@"SELECT address,phone from contacts where name=\"%@\"",name.text];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];
address.text = addressField;
NSString *phoneField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 1 )];
phone.text = phoneField;
status.text = @"已查到结果";
[addressField release];
[phoneField release];
}
else {
status.text = @"未查到结果";
address.text = @"";
phone.text = @"";
}
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
}
}
- (void)viewDidUnload
{
[superviewDidUnload];
// Release any retained subviews of the main view.
self.name = nil;
self.address = nil;
self.phone = nil;
self.status = nil;
}