Iphone 如何从数据库中更改单击时的图像
在我的应用程序中,我在SQLite数据库中有多个图像,因为我想在单击按钮时切换到下一个图像 这是我的密码:Iphone 如何从数据库中更改单击时的图像,iphone,ios,sqlite,uiimage,Iphone,Ios,Sqlite,Uiimage,在我的应用程序中,我在SQLite数据库中有多个图像,因为我想在单击按钮时切换到下一个图像 这是我的密码: -(void)Readthesqlitefile:(NSInteger *)sno { sqlite3 *database;//database object NSString *docpath=[self doccumentspath];//get sqlite path const char *ch=[docpath UTF8String];//string to constant
-(void)Readthesqlitefile:(NSInteger *)sno
{
sqlite3 *database;//database object
NSString *docpath=[self doccumentspath];//get sqlite path
const char *ch=[docpath UTF8String];//string to constant char UTF8string main part to connect DB
if (sqlite3_open(ch, &database)==SQLITE_OK) {
const char *chstmt="SELECT * FROM animal where rowid= = %d",sno;
sqlite3_stmt *sqlstmt;//to execute the above statement
if (sqlite3_prepare_v2(database, chstmt, -1, &sqlstmt, NULL)==SQLITE_OK)
{
while (sqlite3_step(sqlstmt)==SQLITE_ROW)
{
const char *Bname=(char *)sqlite3_column_text(sqlstmt, 0);
//converting const char to nsstring
NSString *Bndname=[NSString stringWithFormat:@"%s",Bname];
NSLog(@"Brand Names=%@",Bndname);
lb1.text=[NSString stringWithFormat:Bndname];
NSUInteger legnt=sqlite3_column_bytes(sqlstmt, 1);
if (legnt>0) {
NSData *dt=[NSData dataWithBytes:sqlite3_column_blob(sqlstmt, 1) length:legnt];
clsimg=[UIImage imageWithData:dt];//converting data to image
imager.image=clsimg;
}
else {
clsimg=nil;
}
}
}
sqlite3_finalize(sqlstmt);
}
sqlite3_close(database);
}
按钮点击功能:
-(IBAction)changenext
{
int j;
for (j=1; j<10; j++)
{
[self Readthesqlitefile:j];
}
}
-(iAction)更改下一步
{
int j;
对于(j=1;j从您的问题中,我了解到您需要更改每个按钮单击时的图像。如果我正确,请更改changenext
方法,如:
-(IBAction)changenext
{
static int j = 0;
if (j > 10)
{
j = 0;
}
[self Readthesqlitefile:j];
j++;
}
上述方法将在每次单击按钮时更改图像。显示最后一个图像后,它将再次从第一个图像开始
编辑:
这一行似乎也错了
const char*chstmt=“SELECT*FROM animal,其中rowid==%d”,sno;
sqlite3中没有==
操作符(我想是的)。
将该行替换为
NSString *query = [NSString stringWithFormat:@"SELECT * FROM animal where rowid = %d",sno];
const char *chstmt = [query UTF8String];
我们无法给出确切的答案,因为所有数据都在您身边,但您可以尝试使用此代码来检查数据的完整性
NSData *dt=[NSData dataWithBytes:sqlite3_column_blob(sqlstmt, 1) length:legnt];
UIImage *clsimg=[UIImage imageWithData:dt]; //Creating new object for UIImage
if(clsimg!=nil)
{
imager.image=clsimg; //I'm assuming that imager is added in IB or you've created it before assigning clsimg.
clsimg = nil;
}
else
{
NSLog(@"No clsimg created, may problem in converting.");
}
它以指针作为参数,但传递一个值
const char *chstmt="SELECT * FROM animal where rowid= = %d",sno;
也许此行应该将其格式化为sql语句,格式代码在哪里
更改下一部分,我认为Midhun MP的答案是正确的解决方案假设您的UIImageView(imageView)
[imageView setImage:clsimg];
我不明白你的问题,有什么问题吗?我需要更改每个按钮上的图像单击@MidhunMpAh,我的头很痛(来自此代码)…一些建议:如果你不问一个特定的问题,你就不会在这里得到任何有用的答案。扔进一堆难看的代码,然后问“为什么它不起作用?”(没有具体说明什么东西不起作用)不会帮助任何人。无意冒犯。代码中最明显的问题是向方法传递一个指向整数的指针,而不是整数本身,这几乎肯定不是您想要的。设置static int j;在changeText method中,您明白我的意思,但它不起作用,我是否应该调用“[self-Readthesqlitefile:j]“还有别的地方。比如说,请帮帮我。”out@iosdev:现在问题是什么?如果问题不是在第一次加载时获取图像,则调用方法[self-changenext]
在viewDidLoadSELECT*FROM animal,其中rowid==%d,我认为应该将sno格式设置为%d,因此类似SELECT*FROM animal,其中rowid==1之类的语句,但我找不到该代码
const char *chstmt="SELECT * FROM animal where rowid= = %d",sno;