INSERT INTO `Demo_Table`(`demo_id`, `demo_name`) VALUES (1,'xiaohao');
UPDATE `Demo_Table` SET `demo_name` = 'yangyang' WHERE `demo_id`=1;
DELETE FROM `Demo_Table` WHERE `demo_id`=1;
SELECT * FROM `Demo_Table` WHERE `demo_id`=1;
#import "DataBase.h"
@implementation DataBase
+ (DataBase *)shareInstense
{
static DataBase * hand = nil;
// if (hand == nil) {
// hand = [[DataBase alloc] init];
// }
//保证线程安全的情况下只走一次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
hand = [[DataBase alloc] init];
//第一次执行的时候 , 打开数据库
[hand openDB];
// [hand closeDB];
});
return hand;
}
- (void) openDB
{
//拼接一个数据库文件的地址
NSString * dataPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
NSString * dbPath = [dataPath stringByAppendingPathComponent:@"ddd.db"];
NSLog(@"%@",dbPath);
//参数1:数据库的文件路径 参数2 : sqlite3指针的地址
int result = sqlite3_open([dbPath UTF8String] ,&dbPoint);
//对数据库结果进行判断
if (result == SQLITE_OK) {
NSLog(@"成功");
}else{
NSLog(@"失败");
}
}
- (void)closeDB
{
int result = sqlite3_close(dbPoint);
if (result == SQLITE_OK) {
NSLog(@"关闭成功");
}
}
//创建表, 增删改查数据
- (BOOL ) createTable
{
// 1、产生一个数据库的替身, 存储所有对数据库的修改
sqlite3_stmt * stmt = nil;
// 2 、检查SQL语句, 为执行sql语句做准备
//参数1: 数据库指针
//参数2:SQL 语句
//参数3 : 限制sql语句长度(-1表示不限制长度)
//参数4 : stmt的地址
int result = sqlite3_prepare(dbPoint, "create table Student (name text , age int , number int primary key )", -1, &stmt, NULL);
//判断检查结果
if (result == SQLITE_OK) {
//4、执行sql语句
int sqlResult = sqlite3_step(stmt);
//判断sql语句执行的结果
if (sqlResult == SQLITE_DONE) {
sqlite3_finalize(stmt);
return YES;
}
}
//如果检查sql语句失败, 将stmt指针释放
sqlite3_finalize(stmt);
return NO;
}
- (BOOL ) insertTableWithName : (NSString * ) name age : ( int ) age number : ( int )number
{
//1 、产生一个替身
sqlite3_stmt * stmt = nil;
//2、 准备
int result = sqlite3_prepare(dbPoint, "insert into Student values ( ? ,? ,? )", -1, &stmt, NULL);
//3、 给? 绑定数据'
//参数1、 替身
//参数2、 给哪个位置的?绑定(唯一一个从1开始计数的地方)
//参数3、 提供的数据
//参数4、限定长度
sqlite3_bind_text(stmt, 1, [name UTF8String], -1, NULL);
sqlite3_bind_int(stmt, 2, age);
sqlite3_bind_int(stmt, 3, number);
//4、sql检查无误
if (result == SQLITE_OK) {
if (sqlite3_step(stmt) == SQLITE_DONE) {
sqlite3_finalize(stmt);
return YES;
}
}
sqlite3_finalize(stmt);
return NO;
}
- ( BOOL ) deleteTableWithName : ( NSString * ) name
{
// sqlite3_stmt * stmt = nil;
// int result = sqlite3_prepare(dbPoint, "delete from Student where name = ?", -1, &stmt, NULL);
// sqlite3_bind_text(stmt, 1, [name UTF8String], -1, NULL);
// if (result == SQLITE_OK) {
// if (sqlite3_step(stmt) == SQLITE_DONE) {
// sqlite3_finalize(stmt);
// return YES;
// }
// }
// sqlite3_finalize(stmt);
// return NO;
NSString * sqlstr = [NSString stringWithFormat:@"delete from Student where name = '%@'",name];
char * error = nil;
int result = sqlite3_exec(dbPoint, [sqlstr UTF8String], NULL, NULL, &error);
if (result == SQLITE_DONE) {
return YES;
}
return NO;
}
- ( BOOL ) updateTableWithName : ( NSString * ) name number:(int ) number
{
sqlite3_stmt * stmt = nil;
int result = sqlite3_prepare(dbPoint, "update Student set name = ? where number = ?", -1, &stmt, NULL);
sqlite3_bind_text(stmt, 1, [name UTF8String], -1, NULL);
sqlite3_bind_int(stmt, 2, number);
if (result == SQLITE_OK) {
if (sqlite3_step(stmt) == SQLITE_DONE) {
sqlite3_finalize(stmt);
return YES;
}
}
sqlite3_finalize(stmt);
return NO;
}
- ( BOOL ) updateTableWithFirstName:(NSString *) name wherename:(NSString *)tagetname
{
sqlite3_stmt * stmt = nil;
int result = sqlite3_prepare(dbPoint, "update Student set name = ? where name = ?", -1, &stmt, NULL);
sqlite3_bind_text(stmt, 1, [name UTF8String], -1, NULL);
sqlite3_bind_text(stmt, 2, [tagetname UTF8String], -1, NULL);
if (result == SQLITE_OK) {
if (sqlite3_step(stmt) == SQLITE_DONE) {
sqlite3_finalize(stmt);
return YES;
}
}
sqlite3_finalize(stmt);
return NO;
}
- ( NSArray *) selectTable
{
sqlite3_stmt * stmt = nil;
int result = sqlite3_prepare(dbPoint, "select * from Student", -1, &stmt, NULL);
if (result == SQLITE_OK) {
NSMutableArray * array = [NSMutableArray array];
while (sqlite3_step(stmt) == SQLITE_ROW) {
//获取每行的数据
const unsigned char * nameChar = sqlite3_column_text(stmt, 0);
//产生了第一条数据
NSString * name = [NSString stringWithUTF8String:(const char *)nameChar];
int age = sqlite3_column_int(stmt, 1);
int number = sqlite3_column_int(stmt, 2);
NSString * str = [NSString stringWithFormat:@"%@ %d %d",name,age,number];
NSLog(@"%@", str);
[array addObject:str];
}
sqlite3_finalize(stmt);
return array;
}
sqlite3_finalize(stmt);
return [NSMutableArray array];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
DataBase * hand = [DataBase shareInstense];
BOOL b = [hand createTable ];
NSLog(@"%d",b);
BOOL c = [hand insertTableWithName:@"尚需" age:15 number:258];
[hand insertTableWithName:@"马叉" age:15 number:278];
[hand insertTableWithName:@"刘剑" age:17 number:287];
[hand insertTableWithName:@"王慧" age:18 number:214];
[hand insertTableWithName:@"震波" age:19 number:277];
[hand insertTableWithName:@"卫东" age:14 number:297];
[hand insertTableWithName:@"从雷" age:36 number:252];
NSLog(@"%d",c);
[hand selectTable];
NSLog(@"lallala");
[hand deleteTableWithName:@"马叉"];
[hand selectTable];
NSLog(@"lalallalal");
[hand updateTableWithName:@"红颜" number:258];
[hand selectTable];
NSLog(@"lalallalal");
[hand updateTableWithFirstName:@"付莉莉" wherename:@"从雷"];
[hand selectTable];