Reputation: 27
I want to create a new table in iOS core data, I have used the following xml file to create in java before and would like to re-use if possible
sql.xml file
<sql>
<statement>
CREATE TABLE IF NOT EXISTS place (
_id INTEGER PRIMARY KEY AUTOINCREMENT,
Name VARCHAR(50),
Location VARCHAR(50),
Description VARCHAR(300),
Type VARCHAR(50),
longitude DOUBLE(50),
latitude DOUBLE(50),
</statement>
<statement>INSERT INTO place VALUES(1,'Clare'
,'Co Clare'
,'Clare Description'
,'County'
,'52.924014'
,'-9.353399')
</statement>
<statement>INSERT INTO surfSpot VALUES(2,'etc...
Java code
public void onCreate(SQLiteDatabase db){
String s;
try{
InputStream in = context.getResources().openRawResource(R.raw.sql);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in, null);
NodeList statements = doc.getElementsByTagName("statement");
for (int i=0; i<statements.getLength(); i++) {
s = statements.item(i).getChildNodes().item(0).getNodeValue();
db.execSQL(s);
}
} catch (Throwable t) {
}
}
The database is static, I would like suggestions on how to do the same thing for iOS, step by step instructions would be the ideal answer
Upvotes: 0
Views: 272
Reputation: 9185
You must first recognize that Core Data is not a database engine; it is an object graph persistence framework. One of its persistent store types happens to be sqlite store. Therefore, terms like "table" that are recognizable in the database world are not transferable to Core Data, at least at the level of abstraction you would be working with in your application.
You could use the existing XML export to populate your Core Data persistent store; but realize that the sqlite backing store format is opaque - you would have to locate it on the simulator's file system, then write a block of code that bridges the existing XML export to Core Data's sqlite persistent store. It would be much more trouble than it's worth.
Upvotes: 1
Reputation: 52575
That's not how Core Data works, I'm afraid. That it uses SQLite is an implementation detail. In fact, it doesn't even have to use SQLite; there are other persistent store types.
You could insert directly into the SQLite database that Core Data creates. I would strong recommend against doing this. It would be very fragile and liable to fail at major version updates.
A better solution might be to use SQLite directly, ignoring Core Data entirely. Core Data is a great abstraction for most apps, but isn't the only way and isn't the best way for all use cases.
Upvotes: 3