2012-01-04 20 views
1

以下は、画像をsqliteデータベースに保存するための私のコードです。私は値を格納するためにそれを使用すると、今私はsqliteデータベースに画像を格納しようとしています。私は何が間違っているのか分からない。私はすでに検索し、私は必要な答えを得ることができません。誰でも彼のコードを教えてください。 画像をsqliteデータベースに保存

sqlite3 *database; 
    [email protected]"dataTable.sqlite"; 
    NSArray *documentpath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentdir=[documentpath objectAtIndex:0]; 
    dbPath=[documentdir stringByAppendingPathComponent:dbName]; 
    sqlite3_stmt *compiledStmt; 
    if(sqlite3_open([dbPath UTF8String], &database)==SQLITE_OK){ 
     NSLog(@"Name:%@,Company:%@,URL:%@",model.personName,model.companyName,model.imgurl); 
     const char *insertSQL="insert into Persons(PersonName,CompanyName,ImgUrl,PersonImage)values(?,?,?,?)"; 
    if(sqlite3_prepare_v2(database,insertSQL, -1, &compiledStmt, NULL)==SQLITE_OK){ 
     sqlite3_bind_text(compiledStmt,1,[model.personName UTF8String],-1,SQLITE_TRANSIENT); 
     sqlite3_bind_text(compiledStmt,2,[model.companyName UTF8String],-1,SQLITE_TRANSIENT); 
     sqlite3_bind_text(compiledStmt,3,[model.imgurl UTF8String],-1,SQLITE_TRANSIENT); 
     NSData *imageData=UIImagePNGRepresentation(imageView.image); 
     sqlite3_bind_blob(compiledStmt, 4, [imageData bytes], [imageData length], NULL); 
     NSLog(@"Prepare"); 
     sqlite3_step(compiledStmt); 

    }sqlite3_finalize(compiledStmt); 
} 

はUPDATE:すべての人に おかげで..私はこれが他の人に役立つことstore and retrieve image into sqlite database for iphone ..ここから尋ねた別の質問でこの問題をクリア。

+0

イムを貼り付け 後藤の.mファイル.... –

+0

一度に*だけ*ご質問ください。 – Sathya

+0

@Sathya okk ...私の1番目の質問で私を助けることができます..私はそれを編集します.. –

答えて

0

ではありません私はこの答えをsimilair質問に答え:あなたの代わりにCoreDataを使用する場合、それは良いでしょう。 SQLの代わりにCoreDateを使用する方がはるかに簡単です。 CoreDataは素晴らしいラッパーのSQLデータベースです。

iOS 5を使用している場合は、「外部ストレージを許可する」にチェックを入れてBLOBS(バイナリラージオブジェクト)であることを心配することなく、データベースに簡単に画像を追加できます。

+2

私はすでに知っている..私はios4のためのアプリを開発する必要がある..私はsqlite3を使用する必要が..私はちょうど2つの写真を使用するつもりです...私は正しいコードをチェックアウトする必要があります..いつもありがとう.. –

2
const char *insertSQL="insert into Persons(PersonName,CompanyName,ImgUrl,PersonImage)values(?,?)" 

は、あなたのテーブル&パラメータのための唯一の2プレースホルダに挿入する4つの値を持っています。それらを訂正しなさい。

ヘック、私はiOSデベロッパ

+0

あなたのお返事ありがとうございます...しかしそれでもまだ動作していません.. –

+0

"not working only"は悪いコメントです。私たちはあなたの画面に何が起こっているのかを理解するためにここに読者を気にしません。いくつかのエラーログを追加し、少なくともあなた自身で*基本的な*デバッグを行います。 @ DineshRaja.MaG – Sathya

+0

あなたは無形のプレースホルダーを持っているので、順序でパラメータを渡してください。 – nawfal

0

bind_textとbind_blobの戻り値とstep-callを確認し、失敗したときにエラーメッセージを表示する必要があります。あなただけの.hファイルにリンクされたフレームワークとLilbrariesにlibSqlite3.dylibを追加し、データベースがvaribles宣言

+0

ブロ...私のコードは、sqlite_prepare_v2ステートメントの後に実行されていません...私はそれで間違っている? –

+1

'if(sqlite3_prepare_v2(database、...)のelse部分を作るNSLog(@ "準備が失敗しました(%s)"、sqlite3_errmsg(データベース)); 'と同様です。それはより多くの情報を示します。 –

1

//Database Variables 
    @property (strong, nonatomic) NSString *databasePath; 
    @property (nonatomic)sqlite3 *contactDB; 
    @property (strong, nonatomic) IBOutlet UIButton *backbtn; 
    @property (strong, nonatomic) IBOutlet UIButton *forwardbtn; 

ドラッグとそれにUIImageViewと名前をドロップ...私はimgViewとして宣言。あなただけコピーしてアプリは私のプロジェクトに発売しながら、表示する私のappdelegateでデータベースを読み取るためのコードを持っているコード

int i=1; 
long long temp=0; 
- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 

NSString *docsDir; 
NSArray *dirPaths; 

// Get the documents directory 
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = dirPaths[0]; 

// Build the path to the database file 
_databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"images.db"]]; 
//docsDir NSPathStore2 * @"/Users/gayathiridevi/Library/Application Support/iPhone Simulator/7.0.3/Applications/B5D4D2AF-C613-45F1-B414-829F38344C2A/Documents" 0x0895e160 
NSFileManager *filemgr = [NSFileManager defaultManager]; 

if ([filemgr fileExistsAtPath: _databasePath ] == NO) 
{ 
    const char *dbpath = [_databasePath UTF8String]; 

    if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) 
    { 
     char *errMsg; 
     const char *sql_stmt = "CREATE TABLE IF NOT EXISTS IMAGETB (ID INTEGER PRIMARY KEY AUTOINCREMENT,URL TEXT, CHECKSUM TEXT,IMAGE BLOB)"; 

     if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK) 
     { 

      NSLog(@"User table Not Created Error: %s", errMsg); 
     } 
     else 
     { 
      NSLog(@"User table Created: "); 
     } 
     sqlite3_close(_contactDB); 
    } 

    else { 

     NSLog(@"DB Not Created"); 
    } 
} 
[self saveImage]; 
[self showImage]; 
} 
- (void)saveImage 
    { 

    sqlite3_stmt *statement; 
    const char *dbpath = [_databasePath UTF8String]; 

if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) 
{ 

    NSString *[email protected]"INSERT INTO IMAGETB(URL,image) VALUES(?,?)"; 

    if(sqlite3_prepare_v2(_contactDB, [insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL)== SQLITE_OK) 
    { 
     //NSString *url [email protected]"https://lh6.googleusercontent.com/-vJBBGUtpXxk/AAAAAAAAAAI/AAAAAAAAADQ/nfgVPX1n-Q8/photo.jpg"; 
     //NSString *url [email protected]"http://upload.wikimedia.org/wikipedia/commons/2/2a/Junonia_lemonias_DSF_upper_by_Kadavoor.JPG"; 
     // NSString *url [email protected]"http://upload.wikimedia.org/wikipedia/commons/8/84/Tibia_insulaechorab.jpg"; 


     NSString *url [email protected]"http://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/PNG_transparency_demonstration_2.png/280px-PNG_transparency_demonstration_2.png"; 
     UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]]; 
     NSData *imageData=UIImagePNGRepresentation(image); 
      sqlite3_bind_text(statement,1, [url UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_blob(statement, 2, [imageData bytes], [imageData length], SQLITE_TRANSIENT); 

     NSLog(@"Length from internet : %lu", (unsigned long)[imageData length]); 

    } 


    if (sqlite3_step(statement) == SQLITE_DONE) 
    { 
     NSLog(@"Insert into row id %lld",(sqlite3_last_insert_rowid(_contactDB))); 
     temp =(sqlite3_last_insert_rowid(_contactDB)); 
    } 
    else { 

     NSLog(@"Error IN INSERT"); 
    } 
    sqlite3_finalize(statement); 
    sqlite3_close(_contactDB); 
} 
} 

- (void)showImage 
{ 

sqlite3_stmt *statement; 
    const char *dbpath = [_databasePath UTF8String]; 
    if(sqlite3_open(dbpath,&_contactDB)==SQLITE_OK) 

{ 
    NSString *insertSQL = [NSString stringWithFormat:@"Select IMAGE FROM IMAGETB WHERE ID = %d",i]; 
    if(sqlite3_prepare_v2(_contactDB,[insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK) { 
     while(sqlite3_step(statement) == SQLITE_ROW) { 

      int length = sqlite3_column_bytes(statement, 0); 
      NSData *imageData = [NSData dataWithBytes:sqlite3_column_blob(statement, 0) length:length]; 

      NSLog(@"Length from db : %lu", (unsigned long)[imageData length]); 

      if(imageData == nil) 
       NSLog(@"No image found."); 
      else 
       _imgView.image = [UIImage imageWithData:imageData]; 
      NSLog(@"image found."); 
     } 
    } 
    sqlite3_finalize(statement); 
} 
sqlite3_close(_contactDB); 
} 




-(IBAction)backBtn:(id)sender { 

    if (i<=1) {} 
    else{ 
    i=i-1; 
    [self showImage]; 
    } 
} 
-(IBAction)forwardBtn:(id)sender { 
if(i==temp){} 
else{ 

    i=i+1; 
    [self showImage];   
} 
} 
+0

私のコードを掲載します。それがあなたを助けると信じています... –

関連する問題