2010-01-12 76 views
0

我正在開發iphone應用程序的離線實現。 我無法將pdf文件插入到sqlite數據庫中。 任何人都可以幫助我將pdf/xls插入到sqlite數據庫。 在此先感謝!在SQLite中插入PDF

+0

你是如何試圖插入呢?你有什麼問題? – 2010-01-12 12:55:54

回答

3

要做到這一點,你需要使用SQLite API的佔位符機制使用的是:

INSERT INTO my_table (my_pdf) VALUES (:_pdf); 

,做該查詢之前,您需要綁定:_pdf到PDF二進制數據(僞代碼如下):

myPdfData = //code that loads binary contents of the PDF into a variable 
myQuery.bindValue(":_pdf", myPdfData); 
myQuery.exec(); 

這可能會給一些更深入的瞭解:

6

我認爲一個更好的方法是將PDF存儲在文件系統中,並將路徑存儲在數據庫中。

0

一般而言,您希望將一個blob(a.k.a.二進制大對象)插入到數據庫中。 SQLite提供了一種方法來做到這一點,但它有點棘手。在我的Squidoo page上有一個很好的例子。其中插入和提取斑點的代碼示例(C語言)轉載於此:

/* Copyright 2008 Jay Godse 

Licensed under the Apache License, Version 2.0 (the "License") 
http://www.apache.org/licenses/LICENSE-2.0 
*/ 

/* File name - blobExampleMain.c */ 
/* Purpose - The main file to show how SQLite can be used */ 
/* to store blobs */ 
/* Assumes blobTest.db already exists with a table: */ 
/* CREATE TABLE images (name string, image blob); */ 
/* Build this file (linux/gcc with the following command - */ 
/* gcc blobExampleMain.c -lsqlite3 -o blobExample */ 

#include "stdio.h" 
#include "stdlib.h" 
#include "string.h" 
#include "sqlite3.h" 

sqlite3 *db; /* global SQLite database handle */ 

int main (int argc, char **argv) { 
int nrows, ncols, rc, i,j; 
char *zErr; 
char **startupConfig; 

/* query strings*/ 
char* sqlinsert="insert into images (name, image) values (?,?);"; 
char* sqlselect="select image from images where name=? limit 1;"; 
int f1Size; 
FILE * f1; 
FILE * f2; 
char fileToSave[200]; /* input file to save*/ 
char copiedFile[208]; /* file that is output*/ 

if (argc==2) { 
    strncpy(fileToSave, (char*)argv[1], 199); 
    printf("saving %s to database\n", fileToSave); 
} 
else { 
    printf("Usage: blobExample argc=%d\n", argc); 
return 0; 
} 

printf("Open the file %s to copy into the database\n", fileToSave); 

f1 = fopen(fileToSave, "rb"); 

if (f1==NULL) { 
    printf("%s the file does not exist\n", fileToSave); 
    return 0; 
} 

/* get the size f1Size of the input file*/ 
fseek(f1, 0, SEEK_END); 
f1Size=ftell(f1); 
fseek(f1, 0, SEEK_SET); 

char *copyBuf = (char*)malloc(f1Size+1); 

printf("The size of %s is %d\n", fileToSave, f1Size); 

if (f1Size != fread(copyBuf, sizeof(char), f1Size, f1)) { 
    free (copyBuf); 
    return -2; 
} 

fclose (f1); 

printf("Opening the database to copy the file into it\n"); 
rc = sqlite3_open("./blobExample.db", &db); 
sqlite3_stmt *insertstmt; 
sqlite3_stmt *selectstmt; 

printf ("Now doing the image insert by binding the file to the blob\n"); 
rc = sqlite3_prepare(db, sqlinsert, strlen(sqlinsert), &insertstmt, NULL); 

sqlite3_bind_text(insertstmt, 1, fileToSave, 
strlen(fileToSave), SQLITE_STATIC); 
sqlite3_bind_blob(insertstmt, 2, (const void*)copyBuf, f1Size, SQLITE_STATIC); 

sqlite3_step(insertstmt); 

sqlite3_finalize(insertstmt); 
free (copyBuf); 

printf("Now doing the select and image extraction\n"); 
rc=sqlite3_prepare(db, sqlselect, strlen(sqlselect), &selectstmt, NULL); 
sqlite3_bind_text(selectstmt, 1, fileToSave, strlen(fileToSave), SQLITE_STATIC); 

ncols=sqlite3_column_count(selectstmt); 
sqlite3_step(selectstmt); 

sprintf (copiedFile, "copyOf__%s", fileToSave); 

f2 = fopen(copiedFile, "wb"); 
fwrite (sqlite3_column_blob(selectstmt, 0), sqlite3_column_bytes(selectstmt, 0), 1, f2); 
fclose (f2); 

sqlite3_finalize(selectstmt); 
return 0; 

}