No such table: z_metadata IOS SQLite 3 Problem Analysis

Source: Internet
Author: User

 

This problem has been clarified.
I have discussed this issue a lot abroad. For details, refer

Http://ablogontech.wordpress.com/2009/07/13/using-a-pre-populated-sqlite-database-with-core-data-on-iphone-os-3-0/

In addition, Apple's official explanation of this: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdFAQ.html how do I initialize
Store with default data? This section.
The above articles are summarized as follows:
1. Why are there strange names starting with Z and those strange tables and fields?
This is a private format that Apple supports coredata. When coredata is used and the-(nspersistentstorecoordinator *) persistentstorecoordinator method is called, IOS coredata automatically creates a. SQLite file.
This file will be automatically stored in your app document directory with readable and writable capabilities. Check the SQLite file to find the strange names and usage.
2. How to initialize the default data in the table? Look at the link: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdFAQ.html
How do I initialize a store with default data
The basic meaning is that if you use this format, you will basically have nothing to do with yourself. Unless you store your data in a resource file, such as XML, and then initialize it.
However, after testing, I found some methods that do not use Ios to initialize the default data,
The details are as follows:
1) copy the. SQLite file automatically generated in the simulator.
2) use sqlite3 or any tool to insert the data you want to insert.
3) Put the inserted sqls file into your project as a resource. then, call the following function before persistentstorecoordinator (this function is to put your file in the system so that IOS can use your file instead of generating other files by default) this is basically the case.
-(Void) copydatabasefileifneed

{

Nsfilemanager * filemanager = [nsfilemanager defaultmanager];

Nsurl * documentsdir = [self applicationdocumentsdirectory];

Nsurl * writabledbpath = [documentsdir urlbyappendingpathcomponent: @ "Default. SQLite"];

Bool dbexists = [filemanager fileexistsatpath: [writabledbpath path];
If (! Dbexists ){

// The writable dB doesn't exist so we'll copy our default one there.

Nsurl * defaultdbpath = [[nsbundle mainbundle] urlforresource: @ "xcloth" withextension: @ "SQLite"];

Nserror * error;

Bool success = [filemanager copyitematurl: defaultdbpath tourl: writabledbpath error: & error];

If (! Success ){

Nsassert1 (0, @ "failed to create writable database file with message
'% @'. ", [Error localizeddescription]);

}
Nslog (@ "% @", @ "DB is not exist ");

}

Else

{
Nslog (@ "% @", @ "DB is exist .");

}

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.