[SQLite]---database

前端之家收集整理的这篇文章主要介绍了[SQLite]---database前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

注意:

1.使用该方法时注意要给项目中添加database.h和database.m文件以及PlausibleDatabase.framework框架

2.在框架中导入libsqlite3.dylib框架。

3.把你的sqlite文件添加到项目中


4.在database.m文件修改以下内容

5.正式开始--------------------------------------------------------------------------------

启动数据库并进行查询操作

-(void)refreshArray{

PLsqliteDatabase *db = [DataBasesetup];

NSString *sql = [NSStringstringWithFormat:@"select * from %@",attribute];

id<PLResultSet>rs = [dbexecuteQuery:sql];

while ([rsnext]) {

NSMutableDictionary *myDict = [[NSMutableDictionaryalloc]init];

NSString *list = [rsstringForColumn:@"list"];

[myDict setObject:listforKey:@"list"];

// NSLog(@"list ---- %@",list);

NSString *words = [rsstringForColumn:@"words"];

[myDict setObject:wordsforKey:@"words"];

// NSLog(@"words ---- %@",words);

NSData *picData = [rsdataForColumn:@"picture"];

// NSLog(@"picData ---- %@",picData);

[myDict setObject:picDataforKey:@"picture"];

[myArrayaddObject:myDict];

[myDict release];

}

}


sqlite数据库插入和读取二进制图片数据 (for ios)

假定数据库中存在表 test_table(name,image),下面代码图片文件test.png的二进制数据写到sqlite数据库

UIImage *image = [UIImageimageNamed:@"tree.png"];

NSData *data = UIImagePNGRepresentation(image);

NSString *sql = [NSStringstringWithFormat:@"insert into test_tablevalues('%@')",data];

PLsqliteDatabase *db = [DataBasesetup];

[db executeUpdate:sql];


下面代码数据库中读取图片二进制数据,然后转换成图片image格式:

NSString *sql =@"select picture from test_table where mnumber=1";

PLsqliteDatabase *db = [DataBasesetup];

id<PLResultSet>rs = [dbexecuteQuery:sql];

while ([rsnext]) {

NSData *data = [rsdataForColumn:@"picture"];

UIImage *image = [UIImageimageWithData:data];//此即为读出的图片,然后按你自己的需求使用即可

}


【以下是不需要任何第三方类的方法
下面代码图片文件test.png的二进制数据写到sqlite数据库
char *name = "test";
NSString * nameString = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
NSString * filePath = [[NSBundle mainBundle] pathForResource:nameString ofType:@"png"];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSData * imgData = UIImagePNGRepresentation([UIImage imageWithContentsOfFile:filePath]);
const char * sequel = "insert into test_table(name,image) values(?,?)";
sqlite3_stmt * update;
if (sqlite3_prepare_v2(database,sequel,-1,&update,NULL) == sqlITE_OK)
{
sqlite3_bind_text(update,1,name,NULL);
sqlite3_bind_blob(update,2,[imgData bytes],[imgData length],NULL);
if( sqlite3_step(update) == sqlITE_DONE)
{ NSLog(@"已经写入数据"); }
sqlite3_finalize(update);
}
}
else
{ System Italic 27.0 NSLog(@"文件不存在"); }
下面代码数据库中读取图片二进制数据,然后显示图片
const char * sequel = "select image from test_table where name=?";
sqlite3_stmt * getimg;
if (sqlite3_prepare_v2(database,&getimg,NULL) == sqlITE_OK)
{
char *name = "test";
sqlite3_bind_text(update,NULL);
if(sqlite3_step(getimg) == sqlITE_ROW)
{
int bytes = sqlite3_column_bytes(getimg,0);
Byte * value = (Byte*)sqlite3_column_blob(getimg,1);
if (bytes !=0 && value != NULL)
{
NSData * data = [NSData dataWithBytes:value length:bytes];
UIImage * img = [UIImage imageWithData:data];
UIImageView * aview =[[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,IMAGE_WIDTH,IMAGE_HEIGHT)];
aview.image = img;
[self.view addSubview:aview];
[aview release];
}
}

sqlite3_finalize(getimg);

}

原文链接:https://www.f2er.com/sqlite/201328.html

猜你在找的Sqlite相关文章