-2

I have two database(customerDB & s-customerDB) in my app. I want to transfer customerDB info to s-customerDB. I am using the below steps, 1.Open the s-customerDB. 2.The Attach and insert statement return the success resultCode.

But the "select" query returns the 0 object.

Kindly guide me what I have done wrong.

NSString *tempString = [[NSString alloc]initWithString:@" attach DATABASE 'customerDB.db' as customer "];
int resultCode = sqlite3_exec(_database, [tempString UTF8String], NULL, NULL, NULL);
[tempString release]; tempString = nil;

if (resultCode == SQLITE_OK) //Working
{
    NSString *table = [[NSString alloc]initWithString:@"Employee"];
    tempString = [[NSString alloc]initWithString:@"INSERT INTO"];
    tempString = [tempString stringByAppendingFormat:@" %@ select * from customer.%@ ",table,table];
    sqlite3_stmt *stmt_version = 0x00;
    resultCode = sqlite3_prepare_v2(_database, [tempString UTF8String], -1, &stmt_version, NULL);
    [tempString release]; tempString = nil;
    sqlite3_finalize(stmt_version);

    NSArray *tempArraty = [self executeSelectSqlQuery:@"select * from Employee" error:nil];
    if (resultCode == SQLITE_OK) //Working
    {
        return YES;
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Finder
  • 8,259
  • 8
  • 39
  • 54
  • just a question... why have not you used `CoreData` instead? the direct SQLite is officially not supported by Apple since iOS4. – holex May 28 '14 at 13:04
  • @holex As far as I can tell, sqlite is still supported since it shows up as an option in Linked Frameworks and Libraries and `#import ` works fine. – David Berry May 28 '14 at 16:12
  • My guess is it has something to do with the path and location of `customerDB.db` Is `customerDB.db` something that is packaged with the application at build time? – David Berry May 28 '14 at 16:17
  • @David, yes, it is available, because the `CoreData` build on that. on other hand the Apple encourage the developer to use `CoreData` instead of using `SQLite` directly, regarding the `CoreData`'s memory management is far more balanced in the actual iOS, than developers could achieve using `SQLite` directly. – holex May 28 '14 at 18:57
  • @holex there's a difference between "encourages" and "not supported" The presence of the .h files indicates that it is indeed, still supported. `CoreData` is great for many things. For many things it's either overkill, or not appropriate. – David Berry May 28 '14 at 19:05
  • @David, I agree the CoreData is a big monster. but the Apple official documentation says the following: _If you have an existing SQLite database, you need to import it into a Core Data store_. that is from this document: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html, and also you can find nice instruction about how you can import it properly; I would also wonder if you find any official documentation on Apple's site direclty about `SQLite`; on other hand the serialization is still an option for persistent storing. – holex May 28 '14 at 19:25
  • @holex you're taking the quote out of context. The context is "How do I use my existing SQLite database with CoreData" They're saying that in that specific case, where you want to use CoreData, you must first import the SQLite database into CoreData. – David Berry May 28 '14 at 19:28
  • @holex see [this page](https://developer.apple.com/technologies/ios/data-management.html) where Apple says "Core Data is a full-featured data modeling framework for object-oriented Cocoa Touch applications, while SQLite is perfect for low-level relational database work." – David Berry May 28 '14 at 19:30
  • @David, I leave you with this topic, because I agree with you entirely in one important thing: if you are happy to mess up a project with a non-documented, hardly implementable and maintainable service (yes, that is the definition of when something is _not supported_), that is your own decision. I'm not here arguing with you about it, because it is your call only. – holex May 28 '14 at 19:45

1 Answers1

0

I have found the problem on my code. I have to execute the insert query using sqlite3_exec instead of sqlite3_prepare_v2.

Finder
  • 8,259
  • 8
  • 39
  • 54