SQL读写 messsqlite数据库 在工程中导入libsqliste3.0.lib 库文件
#import "DataBase.h"
@implementation DataBase
//静态变量
staticsqlite3 * db = nil;
//打开数据库
+(sqlite3 *)openDataBase
{
//如果存在则返回
if (db ) {
return db;
}
//找到原数据库
NSString * path = [[NSBundlemainBundle]pathForResource:@"StudentDB"ofType:@"sqlite"];
//找到目标路径
NSString * dataPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)objectAtIndex:0];
NSString * dbPath = [dataPathstringByAppendingPathComponent:@"StudentDB.sqlite"];
NSLog(@"sor: %@ aim:%@",path,dbPath);
//从原来的路径复制文件到目标路径需要用到的是filemanager
NSFileManager * fileManager = [NSFileManagerdefaultManager];
//如果文件不存在,则创建
if([fileManager fileExistsAtPath:dbPath] ==NO)
{
NSLog(@"文件不存在,拷贝文件到:%@",dbPath);
NSError * erro = nil;
//文件拷贝
if([fileManager copyItemAtPath:pathtoPath:dbPath error:&erro] ==NO)
{
NSLog(@"创建文件失败");
return nil;
}
}
NSLog(@"打开文件");
sqlite3_open([dbPathUTF8String], &db); //数据库打开
return db;
}
//关闭数据库
+(void)closeDataBase
{
if(db)
{
sqlite3_close(db);//数据库关闭
}
}
@end
+(NSMutableArray *)findAll
{
sqlite3 * db = [DataBase openDataBase]; //打开数据库
sqlite3_stmt * stmt = nil;
int result = sqlite3_prepare_v2(db, "select * from classA", -1, &stmt, nil);//准备sql语句
if(result == SQLITE_OK) //准备ok之后
{
NSMutableArray * allobject = [[NSMutableArrayalloc]init];
while (SQLITE_ROW == sqlite3_step(stmt))//单步执行sql语句
{
int ID = sqlite3_column_int(stmt, 0); //获取第一列
const unsigned char * name = sqlite3_column_text(stmt,1); //获取第二列
const unsigned char * phone = sqlite3_column_text(stmt,2); //获取第三列
int age = sqlite3_column_int(stmt, 3);//获取第四列
People * p = [[People alloc]initWithID:ID name:[NSString stringWithUTF8String:(char *)name] phone:[NSString stringWithUTF8String:(char *)phone] age:age];//创建人物
[allobjectaddObject:p];//往数组里面添加人物
[p release];
}
sqlite3_finalize(stmt);//释放对象
NSLog(@"sql语句正确,返回数组为:%@",allobject);
return [allobject autorelease];
}
NSLog(@"sql语句错误");
sqlite3_finalize(stmt);
return nil;
}
//查找
+(People *)findByID:(int)ID
{
sqlite3 * db = [DataBase openDataBase]; //打开数据库
sqlite3_stmt * stmt = nil;
int result =sqlite3_prepare_v2(db,"select * from classA where ID = ? ", -1, &stmt,nil);//准备sql语句
if(result == SQLITE_OK) //准备ok之后
{
sqlite3_bind_int(stmt, 1, ID); //绑定第一个?1代表第一个
while (SQLITE_ROW == sqlite3_step(stmt))//单步执行sql语句
{
const unsigned char * name = sqlite3_column_text(stmt,1); //获取第二列
const unsigned char * phone = sqlite3_column_text(stmt,2); //获取第三列
int age = sqlite3_column_int(stmt, 3);//获取第四列
People * p = [[People alloc]initWithID:ID name:[NSString stringWithUTF8String:(char *)name] phone:[NSString stringWithUTF8String:(char *)phone] age:age];//创建人物
sqlite3_finalize(stmt);
return [p autorelease];
NSLog(@"_____");
}
}
NSLog(@"++++++");
sqlite3_finalize(stmt);
return nil;
}
//计数
+(int)count
{
sqlite3 * db = [DataBase openDataBase]; //打开数据库
sqlite3_stmt * stmt = nil;
int result = sqlite3_prepare_v2(db, "select count(*) from classA ", -1, &stmt,nil);//准备sql语句
if(result == SQLITE_OK) //准备ok之后
{
while (SQLITE_ROW == sqlite3_step(stmt))//单步执行sql语句
{
int count = sqlite3_column_int(stmt, 0); //数据数量
sqlite3_finalize(stmt);
return count;
}
}
sqlite3_finalize(stmt);
return 0;
}
+(BOOL)updateName:(NSString *)name phone:(NSString *)phone age:(int)age whereIDEqual:(int)ID
{
sqlite3 * db = [DataBase openDataBase]; //打开数据库
sqlite3_stmt * stmt = nil;
BOOL success = NO; //默认更新状态为不更新
int result =sqlite3_prepare_v2(db,"update classA set name = ?,phone = ?, age = ? where ID = ?", -1, &stmt,nil);//准备sql语句
if(result == SQLITE_OK) //准备ok之后
{
sqlite3_bind_text(stmt, 1, [name UTF8String], -1, nil);
sqlite3_bind_text(stmt, 2, [phone UTF8String], -1, nil);
sqlite3_bind_int(stmt, 3, age);
sqlite3_bind_int(stmt, 4, ID);
if(sqlite3_step(stmt) == SQLITE_DONE)
{
success =YES;
}
}
sqlite3_finalize(stmt);
return success;
}
//添加
+(BOOL)addName:(NSString *)name phone:(NSString *)phone age:(int)age
{
sqlite3 * db = [DataBase openDataBase]; //打开数据库
sqlite3_stmt * stmt = nil;
BOOL success = NO; //默认更新状态为不更新
int result =sqlite3_prepare_v2(db,"insert into classA(name,phone,age) values(?,?,?)", -1, &stmt,nil);//准备sql语句
if(SQLITE_OK == result) //准备ok之后
{
sqlite3_bind_text(stmt, 1, [name UTF8String], -1, nil);
sqlite3_bind_text(stmt, 2, [phone UTF8String], -1, nil);
sqlite3_bind_int(stmt, 3, age);
if(SQLITE_DONE == sqlite3_step(stmt) )
{
success =YES;
NSLog(@"添加完成");
}
}
else{
NSLog(@"添加失败");
}
sqlite3_finalize(stmt);
return success;
}
//删除
+(BOOL)deleteByID:(int)ID
{
sqlite3 * db = [DataBase openDataBase]; //打开数据库
sqlite3_stmt * stmt = nil;
BOOL success = NO; //默认更新状态为不更新
int result =sqlite3_prepare_v2(db,"delete from classA where ID = ? ", -1, &stmt,nil);//准备sql语句
if(result == SQLITE_OK) //准备ok之后
{
sqlite3_bind_int(stmt, 1, ID); //绑定第一个?1代表第一个
while (SQLITE_ROW == sqlite3_step(stmt))//单步执行sql语句
{
success =YES;
}
}
sqlite3_finalize(stmt);
return success;
}
把图片以nsdata的形式写入文件中保存起来
UIImage * image = [UIImageimageNamed:@"1.jpg"];
NSData * imageData = UIImageJPEGRepresentation(image,1);//把图片转换为nsdata
[imageData writeToFile:[selfgetDocumentsPathWithfileName:@"imageData.plist"] atomically:YES];//写入文件中
UIImageView * imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(20 ,20,280,420)];
NSFileManager * fileManager = [NSFileManagerdefaultManager];//文件管理
if ([fileManager fileExistsAtPath:[selfgetDocumentsPathWithfileName:@"imageData.plist" ]]) {
NSLog(@"文件存在,读取路径文件");
//读取文件
imageView.image = [UIImageimageWithContentsOfFile:[selfgetDocumentsPathWithfileName:@"imageData.plist"]];
[self.viewaddSubview:imageView];
}
else
{
NSLog(@"文件不存在!");
}