-6

在我的应用程序中,我在 SQLite 数据库中有多个图像,因为我想在单击按钮时更改为下一个图像。

这是我的代码:

 -(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];
  }

  }

它不工作。请帮我解决它。

4

4 回答 4

2

根据您的问题,我了解到您需要在每次单击按钮时更改图像。如果我是正确的,那么改变changenext方法,如:

-(IBAction)changenext
  {
    static int j = 0;
    if (j > 10)
    {
       j = 0;
    }
    [self Readthesqlitefile:j];
    j++;
  }

上述方法将在每次单击按钮时更改图像。显示最后一张图像后,它再次从第一张图像开始。

编辑:

这条线似乎也是错误的

const char *chstmt="SELECT * FROM animal where rowid= = %d",sno;.

sqlite3中没有==运算符(我认为是这样)。将该行替换为,

NSString *query    = [NSString stringWithFormat:@"SELECT * FROM animal where rowid = %d",sno];
const char *chstmt = [query UTF8String];
于 2012-10-13T11:01:45.967 回答
1
-(void)Readthesqlitefile:(NSInteger *)sno

它需要一个指针作为参数,但你传递一个值。

const char *chstmt="SELECT * FROM animal where rowid=  = %d",sno;

也许这一行应该格式化成sql语句,格式代码在哪里?

下一部分,我认为 Midhun MP 的答案是正确的解决方案

于 2012-10-16T16:44:51.323 回答
0

好吧,我们无法给出确切的答案,因为所有数据都在您身边,但您可以尝试使用此代码检查数据的完整性,

       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.");
       }
于 2012-10-16T07:14:34.767 回答
0

假设你的 UIImageView (imageView)

[imageView setImage:clsimg];

于 2012-10-22T04:34:33.617 回答