0

当我创建一个名为“X”的新表(取自现有的“Y”数据库)时,我无法将其附加到我的电子邮件中。我知道它在我的 iPhone 中(就像我一样

["SELECT * FROM sqlite_master where type='table'"]

第一的)。提前致谢。这是我的代码:

-(void)displayComposerSheet
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    //FileManager - Object allows easy access to the File System.
    NSFileManager *FileManager = [NSFileManager defaultManager];

    //Get the complete users document directory path.
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    //Get the fist path in the array.
    NSString *documentsDirectory = [paths objectAtIndex:0];

    //Create the complete path to the database file.
    NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"X.sqlite"];

    NSData *data = [NSData dataWithContentsOfFile:databasePath];
    [picker addAttachmentData:data mimeType:@"application/x-sqlite3" fileName:@"X.sqlite"];

    [picker setMessageBody:emailBody isHTML:NO];
    [self presentViewController:picker animated:YES completion:nil];

}
4

1 回答 1

0

基本上,我做了以下事情:

    //Delete Output file if it exists
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
    if(insertStmt == nil)
    {
        sqlite3_stmt *selectStatement;
        const char *sqlStatement=[[NSString stringWithFormat:
                                   @"DROP TABLE IF EXISTS fileOutput"]UTF8String];
        if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
        {
            if (sqlite3_prepare_v2(database, sqlStatement, -1, &selectStatement, NULL) != SQLITE_OK)
            {
                NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
            }
            while (sqlite3_step(selectStatement) == SQLITE_ROW)
            {

            }
            sqlite3_reset(selectStatement);
            sqlite3_finalize(selectStatement);
        }
    }
}
sqlite3_close(database);

//Add Output file
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
    if(insertStmt == nil)
    {
        sqlite3_stmt *selectStatement;
        const char *sqlStatement=[[NSString stringWithFormat:
                                   @"CREATE TABLE fileOutput AS SELECT * FROM Y WHERE 0"]UTF8String];
        if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
        {
            if (sqlite3_prepare_v2(database, sqlStatement, -1, &selectStatement, NULL) != SQLITE_OK)
            {
                NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
            }
            while (sqlite3_step(selectStatement) == SQLITE_ROW)
            {

            }
            sqlite3_reset(selectStatement);
            sqlite3_finalize(selectStatement);
        }
    }
}
sqlite3_close(database);

//Copy into table
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
    if(insertStmt == nil)
    {
        sqlite3_stmt *selectStatement;
        const char *sqlStatement=[[NSString stringWithFormat:
                                   @"INSERT INTO fileOutput SELECT * FROM Y WHERE file IN ('%@')",pickerField]UTF8String];
        if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
        {
            if (sqlite3_prepare_v2(database, sqlStatement, -1, &selectStatement, NULL) != SQLITE_OK)
            {
                NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
            }
            while (sqlite3_step(selectStatement) == SQLITE_ROW)
            {

            }
            sqlite3_reset(selectStatement);
            sqlite3_finalize(selectStatement);
        }
    }
}
sqlite3_close(database);

//Make the OUTPUT sqlite file
[self deleteEditableCopyOfDatabaseIfNeeded];

//ATTACH OUTPUT file to Y
const char *outputDBPath = [[documentsDirectory stringByAppendingPathComponent:@"X.sqlite"] UTF8String];

if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    NSString *attach = [NSString stringWithFormat: @"ATTACH DATABASE \'%s\' AS X", outputDBPath];
    const char *attachSQL = [attach UTF8String];
    char *errorMessage;


    if (sqlite3_exec(database, attachSQL, NULL, NULL, &errorMessage) == SQLITE_OK) {

//Copy the table into sqlite file
            sqlite3_stmt *selectstmt;
            const char *sqlStatement1 = "INSERT OR REPLACE INTO X (all the fields go here ) SELECT * FROM fileOutput";
            if (sqlite3_prepare_v2(database,sqlStatement1, -1, &selectstmt, NULL) == SQLITE_OK) {
                while (sqlite3_step(selectstmt) == SQLITE_ROW) {
                //if(sqlite3_step(selectstmt)==SQLITE_DONE)
            //{
                    NSLog(@"insert successfully");
                }
            }
            else
            {
                NSLog(@"insert not successfully");

            }
            sqlite3_finalize(selectstmt);
        }
    }
    sqlite3_close(database);




[self sendEmailWithFile];
于 2013-02-28T23:04:48.043 回答