简体   繁体   English

iOS coredata使用现有的SQLite?

[英]iOS coredata using existing SQLite?

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 我想在iOS核心数据中创建一个新表,我之前使用过以下xml文件在java中创建,如果可能的话我想重新使用

sql.xml file sql.xml文件

<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 Java代码

 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 数据库是静态的,我想建议如何为iOS做同样的事情,一步一步的指示将是理想的答案

That's not how Core Data works, I'm afraid. 我担心,这不是Core Data的工作方式。 That it uses SQLite is an implementation detail. 它使用SQLite是一个实现细节。 In fact, it doesn't even have to use SQLite; 实际上,它甚至不必使用SQLite; there are other persistent store types. 还有其他持久性商店类型。

You could insert directly into the SQLite database that Core Data creates. 可以直接插入Core Data创建的SQLite数据库。 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. 更好的解决方案可能是直接使用SQLite,完全忽略Core Data。 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. 核心数据是大多数应用程序的一个很好的抽象,但不是唯一的方法,并不是所有用例的最佳方式。

You must first recognize that Core Data is not a database engine; 您必须首先认识到Core Data 不是数据库引擎; it is an object graph persistence framework. 它是一个对象图持久性框架。 One of its persistent store types happens to be sqlite store. 其持久性商店类型之一恰好是sqlite商店。 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. 因此,数据库世界中可识别的“表”之类的术语不能转移到Core Data,至少在您在应用程序中使用的抽象级别。

You could use the existing XML export to populate your Core Data persistent store; 您可以使用现有的XML导出来填充Core Data持久性存储; 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. 但是要意识到sqlite后备存储格式是不透明的 - 你必须在模拟器的文件系统上找到它,然后编写一个代码块,将现有的XML导出桥接到Core Data的sqlite持久存储。 It would be much more trouble than it's worth. 这将比它的价值更麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM